commit 3315705
ricmaps
·
2026-11-26 20:59:24 +0000 UTC
parent ccf617b
add bitwise operators
M
ops.h
+6,
-0
1@@ -27,3 +27,9 @@ OP(ILOAD, "loadl")
2 OP(ISTORE, "storel")
3 OP(ICALL, "call")
4 OP(IARG, "arg")
5+OP(IAND, "and")
6+OP(IOR, "or")
7+OP(IXOR, "xor")
8+OP(ISHL, "shl")
9+OP(ISHR, "shr")
10+OP(ISAR, "sar")
M
parse.c
+46,
-12
1@@ -10,6 +10,7 @@ static struct decl *curfn;
2 static struct decl *lambdas;
3 static struct decl **lambdaend;
4 static usize nlambdas;
5+static int inarm;
6
7 static struct expr *expr(void);
8 static struct expr *block(void);
9@@ -18,6 +19,28 @@ static struct expr *pipe(void);
10 static struct expr *lambda(void);
11 static struct param *sortparams(void);
12
13+static struct expr *
14+armbody(void) {
15+ struct expr *e;
16+ int saved = inarm;
17+
18+ inarm = 1;
19+ e = expr();
20+ inarm = saved;
21+ return e;
22+}
23+
24+static struct expr *
25+freeexpr(void) {
26+ struct expr *e;
27+ int saved = inarm;
28+
29+ inarm = 0;
30+ e = expr();
31+ inarm = saved;
32+ return e;
33+}
34+
35 static void
36 next(void) {
37 /* skip doc comments for now */
38@@ -159,7 +182,7 @@ primary(void) {
39 case TLPAREN:
40 opener = tok;
41 next();
42- e = expr();
43+ e = freeexpr();
44 if(tok.kind != TCOMMA) {
45 expect(TRPAREN);
46 return e;
47@@ -167,7 +190,7 @@ primary(void) {
48 *end = e;
49 end = &e->next;
50 while(consume(TCOMMA)) {
51- a = expr();
52+ a = freeexpr();
53 *end = a;
54 end = &a->next;
55 }
56@@ -210,7 +233,7 @@ call(void) {
57 next();
58 if(tok.kind != closer) {
59 do {
60- a = expr();
61+ a = freeexpr();
62 *end = a;
63 end = &a->next;
64 } while(consume(TCOMMA));
65@@ -225,7 +248,7 @@ static struct expr *
66 unary(void) {
67 struct expr *e;
68
69- if(tok.kind == TSUB || tok.kind == TBANG) {
70+ if(tok.kind == TSUB || tok.kind == TBANG || tok.kind == TTILDE) {
71 e = mkexpr(ECALL, tok);
72 next();
73 e->args = unary();
74@@ -266,10 +289,15 @@ prec(enum tokenkind k) {
75 return 3;
76 case TADD:
77 case TSUB:
78+ case TBAR:
79+ case TBXOR:
80 return 4;
81 case TMUL:
82 case TDIV:
83 case TMOD:
84+ case TBAND:
85+ case TBSHL:
86+ case TBSHR:
87 return 5;
88 default:
89 return 0;
90@@ -283,7 +311,7 @@ binary(int min) {
91 int p;
92
93 l = cast();
94- while((p = prec(tok.kind)) >= min) {
95+ while((p = (tok.kind == TBAR && inarm) ? 0 : prec(tok.kind)) >= min) {
96 op = tok;
97 next();
98 l = mkbinary(op, l, binary(p + 1));
99@@ -363,11 +391,14 @@ expr(void) {
100 e = mkexpr(EGUARD, tok);
101 next();
102 a = mkarm(tok.loc);
103- a->cond = expr();
104+ a->cond = freeexpr();
105 expect(TTHEN);
106- a->body = expr();
107+ a->body = freeexpr();
108 a->next = mkarm(tok.loc);
109 expect(TELSE);
110+ // cannot be freeexpr, consider this example:
111+ // case list of { | Cons(x, _) => if x > 3 then 1 else 2 | _ => 0 }
112+ // with freeexpr the second | would be interpreted as the bit-or of 2 and _
113 a->next->body = expr();
114 e->arm = a;
115 return e;
116@@ -381,9 +412,9 @@ expr(void) {
117 a = mkarm(tok.loc);
118 next();
119 if(!consume(TUS))
120- a->cond = expr();
121+ a->cond = freeexpr();
122 expect(TEQARROW);
123- a->body = expr();
124+ a->body = armbody();
125 *end = a;
126 end = &a->next;
127 }
128@@ -397,7 +428,7 @@ expr(void) {
129 if(tok.kind == TCASE) {
130 e = mkexpr(ECASE, tok);
131 next();
132- e->scrut = expr();
133+ e->scrut = freeexpr();
134 expect(TOF);
135 expect(TLBRACE);
136 while(tok.kind == TBAR) {
137@@ -405,7 +436,7 @@ expr(void) {
138 next();
139 a->pat = pattern();
140 expect(TEQARROW);
141- a->body = expr();
142+ a->body = armbody();
143 *end = a;
144 end = &a->next;
145 }
146@@ -424,7 +455,9 @@ block(void) {
147 struct val *v;
148 struct val *head = nil;
149 struct val **end = &head;
150+ int saved = inarm;
151
152+ inarm = 0;
153 e = mkexpr(EBLOCK, tok);
154 expect(TLBRACE);
155 while(tok.kind == TVAL) {
156@@ -443,6 +476,7 @@ block(void) {
157 e->body = expr(); /*final expression, the return value */
158 expect(TRBRACE);
159 e->vals = head;
160+ inarm = saved;
161 return e;
162 }
163
164@@ -471,7 +505,7 @@ sortparam(void) {
165
166 p = param();
167 if(consume(TBAR))
168- p->cond = expr();
169+ p->cond = freeexpr();
170 return p;
171 }
172
M
punc.h
+4,
-0
1@@ -28,3 +28,7 @@ TOKEN(TBANG, "!")
2 TOKEN(TPIPE, "|>")
3 TOKEN(TAND, "&&")
4 TOKEN(TOR, "||")
5+TOKEN(TBAND, "&")
6+TOKEN(TBXOR, "^")
7+TOKEN(TBSHL, "<<")
8+TOKEN(TBSHR, ">>")
M
qbe.c
+39,
-2
1@@ -436,6 +436,31 @@ funcshort(struct func *f, struct expr *e, int tail) {
2 return funcjoin(f, r, join, tail);
3 }
4
5+static struct operand *
6+funcshift(struct func *f, enum instkind op, struct operand *l, struct operand *n) {
7+ struct operand *c, *k, *m, *s;
8+
9+ if(n->kind == VCONST) {
10+ if(n->i < 0 || n->i > 63) {
11+ if(op != ISAR)
12+ return mkconst(0);
13+ n = mkconst(63);
14+ }
15+ return funcinst(f, op, 'l', l, n);
16+ }
17+ c = funcinst(f, ICULT, 'l', n, mkconst(64));
18+ k = funcinst(f, IAND, 'l', n, mkconst(63));
19+ if(op == ISAR) {
20+ s = funcinst(f, IXOR, 'l', c, mkconst(1));
21+ s = funcinst(f, IMUL, 'l', s, mkconst(63));
22+ k = funcinst(f, IOR, 'l', k, s);
23+ return funcinst(f, op, 'l', l, k);
24+ }
25+ s = funcinst(f, op, 'l', l, k);
26+ m = funcinst(f, INEG, 'l', c, nil);
27+ return funcinst(f, IAND, 'l', s, m);
28+}
29+
30 static struct operand *
31 funcop(struct func *f, struct expr *e) {
32 struct operand *l;
33@@ -453,9 +478,18 @@ funcop(struct func *f, struct expr *e) {
34 t = e->args->type;
35 u = isu64(t);
36 l = funcexpr(f, e->args);
37- if(!e->args->next)
38- return funcwrap(f, t, funcinst(f, INEG, 'l', l, nil));
39+ if(!e->args->next) {
40+ if(e->tok.kind == TTILDE)
41+ res = funcinst(f, IXOR, 'l', l, mkconst(-1));
42+ else
43+ res = funcinst(f, INEG, 'l', l, nil);
44+ return funcwrap(f, t, res);
45+ }
46 r = funcexpr(f, e->args->next);
47+ if(e->tok.kind == TBSHL)
48+ return funcwrap(f, t, funcshift(f, ISHL, l, r));
49+ if(e->tok.kind == TBSHR)
50+ return funcwrap(f, t, funcshift(f, u ? ISHR : ISAR, l, r));
51 switch(e->tok.kind) {
52 case TADD: op = IADD; break;
53 case TSUB: op = ISUB; break;
54@@ -468,6 +502,9 @@ funcop(struct func *f, struct expr *e) {
55 case TMORE: op = u ? ICUGT : ICGT; cmp = 1; break;
56 case TLEQ: op = u ? ICULE : ICLE; cmp = 1; break;
57 case TMEQ: op = u ? ICUGE : ICGE; cmp = 1; break;
58+ case TBAND: op = IAND; break;
59+ case TBXOR: op = IXOR; break;
60+ case TBAR: op = IOR; break;
61 default:
62 errorat(&e->tok.loc, "sorry, no idea how to compile this operator");
63 }