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
259259 }
260260 }
261261
262−ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
262+static inline void asm_expr_cmp(TCCState *s1, ExprValue *pe)
263263 {
264+ int op;
265+ ExprValue e2;
266+
264267 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+
304+ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
305+{
306+ asm_expr_cmp(s1, pe);
265307 }
266308
267309 ST_FUNC int asm_int_expr(TCCState *s1)
@@ -391,6 +433,8 @@ static void asm_parse_directive(TCCState *s1) · 123 unmodified lines
391433 if (sec->sh_addralign < n)
392434 sec->sh_addralign = n;
393435 } else {
436+ if (n < 0)
437+ n = 0;
394438 size = n;
395439 }
396440 v = 0;
tests/asmtest.S+6−0
@@ -740,6 +740,12 @@ nop · 739 unmodified lines
740740 .word 164, 0
741741 .org 2b+32
742742 .quad 1b
743+.popsection
744+3: mov %eax,%ecx
745+4:
746+.pushsection .text.three, "ax"
747+nop
748+.skip (-((4b-3b) > 0) * 2) , 0x90
743749 .popsection
744750
745751 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