History

tccasm: Implement compare expressions

I.e. implement < > <= >= == !=. Comparisons are signed and result is -1 if true, 0 if false.

Michael Matz committed 10 years ago

Commit 1a5eacb1 parent ff5561f2 files · +51 −1Browse files

Unified · Split

2 files
  1. tccasm.c +45 −1
  2. tests/asmtest.S +6 −0
tccasm.c+45−1
@@ -259,9 +259,51 @@ static inline void asm_expr_sum(TCCState *s1, ExprValue *pe) · 258 unmodified lines
259 }259 }
260}260}
261 261
262ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)262static inline void asm_expr_cmp(TCCState *s1, ExprValue *pe)
263{263{
264 int op;
265 ExprValue e2;
266
264 asm_expr_sum(s1, pe);267 asm_expr_sum(s1, pe);
268 for(;;) {
269 op = tok;
270 if (op != TOK_EQ && op != TOK_NE
271 && (op > TOK_GT || op < TOK_ULE))
272 break;
273 next();
274 asm_expr_sum(s1, &e2);
275 if (pe->sym || e2.sym)
276 tcc_error("invalid operation with label");
277 switch(op) {
278 case TOK_EQ:
279 pe->v = pe->v == e2.v;
280 break;
281 case TOK_NE:
282 pe->v = pe->v != e2.v;
283 break;
284 case TOK_LT:
285 pe->v = (int64_t)pe->v < (int64_t)e2.v;
286 break;
287 case TOK_GE:
288 pe->v = (int64_t)pe->v >= (int64_t)e2.v;
289 break;
290 case TOK_LE:
291 pe->v = (int64_t)pe->v <= (int64_t)e2.v;
292 break;
293 case TOK_GT:
294 pe->v = (int64_t)pe->v > (int64_t)e2.v;
295 break;
296 default:
297 break;
298 }
299 /* GAS compare results are -1/0 not 1/0. */
300 pe->v = -(int64_t)pe->v;
301 }
302}
303
304ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
305{
306 asm_expr_cmp(s1, pe);
265}307}
266 308
267ST_FUNC int asm_int_expr(TCCState *s1)309ST_FUNC int asm_int_expr(TCCState *s1)
@@ -391,6 +433,8 @@ static void asm_parse_directive(TCCState *s1) · 123 unmodified lines
391 if (sec->sh_addralign < n)433 if (sec->sh_addralign < n)
392 sec->sh_addralign = n;434 sec->sh_addralign = n;
393 } else {435 } else {
436 if (n < 0)
437 n = 0;
394 size = n;438 size = n;
395 }439 }
396 v = 0;440 v = 0;
tests/asmtest.S+6−0
@@ -740,6 +740,12 @@ nop · 739 unmodified lines
740 .word 164, 0740 .word 164, 0
741 .org 2b+32741 .org 2b+32
742 .quad 1b742 .quad 1b
743.popsection
7443: mov %eax,%ecx
7454:
746.pushsection .text.three, "ax"
747nop
748.skip (-((4b-3b) > 0) * 2) , 0x90
743.popsection749.popsection
744 750
745 movd %esi, %mm1751 movd %esi, %mm1

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t