5 (define (define-op constructor six-id id type-rule constant-fold code-gen)
7 (cons (constructor six-id id type-rule constant-fold code-gen)
9 (define (define-op1 six-id id type-rule constant-fold code-gen)
10 (define-op make-op1 six-id id type-rule constant-fold code-gen))
11 (define (define-op2 six-id id type-rule constant-fold code-gen)
12 (define-op make-op2 six-id id type-rule constant-fold code-gen))
13 (define (define-op3 six-id id type-rule constant-fold code-gen)
14 (define-op make-op3 six-id id type-rule constant-fold code-gen))
16 ;; no need for type checks, every type sixpic supports can be casted to / from
17 ;; ints (except void, but this is a non-issue) and promotion (by padding) and
18 ;; truncation is done at the cfg level
19 (define (type-rule-int-op1 ast)
20 (expr-type (subast1 ast)))
22 (define (largest t1 t2)
23 (if (> (type->bytes t1) (type->bytes t2))
27 (define (type-rule-int-op2 ast)
28 ;; used for any binary operation involving two integers where the result is
29 ;; of the size of the biggest operand (subtraction, bitwise operations, ...)
30 (let ((t1 (expr-type (subast1 ast)))
31 (t2 (expr-type (subast2 ast))))
34 (define (type-rule-assign ast)
35 (let ((t1 (expr-type (subast1 ast))))
36 ;; the type of the rhs is irrelevant, since it will be promoted
37 ;; or truncated at the cfg level
38 ;; TODO not sure it's true anymore
41 ;; the standard says it should be int
42 (define (type-rule-int-comp-op2 ast)
45 (define (type-rule-bool-op2 ast)
48 (define (constant-fold-op2 op)
50 (let ((x (subast1 ast))
52 (if (and (literal? x) (literal? y))
53 (new-literal (expr-type ast) (op (literal-val x) (literal-val y)))
56 (define-op1 'six.!x '!x
59 ast) ;; TODO also call this when testing expressions
64 (define-op1 'six.++x '++x
71 (define-op1 'six.x++ 'x++
78 (define-op1 'six.--x '--x
85 (define-op1 'six.x-- 'x--
92 (define-op1 'six.~x '~x
99 (define-op2 'six.x%y 'x%y
101 (constant-fold-op2 modulo)
105 (define-op2 'six.x*y 'x*y
107 (constant-fold-op2 *)
111 (define-op1 'six.*x '*x
113 'byte) ; we only have byte arrays
119 (define-op2 'six.index 'index
121 'byte) ; we only have byte arrays
127 (define-op2 'six.x/y 'x/y
129 (constant-fold-op2 /)
133 (define-op2 'six.x+y 'x+y
135 (constant-fold-op2 +)
139 (define-op1 'six.+x '+x
146 (define-op2 'six.x-y 'x-y
148 (constant-fold-op2 -)
152 (define-op1 'six.-x '-x
159 ;; TODO check with the C standard for the next 2
160 (define-op2 'six.x<<y 'x<<y
162 (constant-fold-op2 arithmetic-shift)
166 (define-op2 'six.x>>y 'x>>y
168 (constant-fold-op2 (lambda (x y) (arithmetic-shift x (- y))))
172 (define-op2 'six.x<y 'x<y
173 type-rule-int-comp-op2
174 (constant-fold-op2 (lambda (x y) (if (< x y) 1 0)))
178 (define-op2 'six.x<=y 'x<=y
179 type-rule-int-comp-op2
180 (constant-fold-op2 (lambda (x y) (if (<= x y) 1 0)))
184 (define-op2 'six.x>y 'x>y
185 type-rule-int-comp-op2
186 (constant-fold-op2 (lambda (x y) (if (> x y) 1 0)))
190 (define-op2 'six.x>=y 'x>=y
191 type-rule-int-comp-op2
192 (constant-fold-op2 (lambda (x y) (if (>= x y) 1 0)))
196 (define-op2 'six.x!=y 'x!=y
197 type-rule-int-comp-op2
198 (constant-fold-op2 (lambda (x y) (if (not (= x y)) 1 0)))
202 (define-op2 'six.x==y 'x==y
203 type-rule-int-comp-op2
204 (constant-fold-op2 (lambda (x y) (if (= x y) 1 0)))
208 (define-op2 'six.x&y 'x&y
210 (constant-fold-op2 bitwise-and)
214 (define-op1 'six.&x '&x
222 (define-op2 'six.x^y 'x^y
224 (constant-fold-op2 bitwise-xor)
228 (define-op2 '|six.x\|y| '|x\|y|
230 (constant-fold-op2 bitwise-ior)
234 (define-op2 'six.x&&y 'x&&y
236 (constant-fold-op2 (lambda (x y) (if (and (not (= x 0)) (not (= y 0))) 1 0)))
240 (define-op2 '|six.x\|\|y| '|x\|\|y|
242 (constant-fold-op2 (lambda (x y) (if (or (not (= x 0)) (not (= y 0))) 1 0)))
246 (define-op3 'six.x?y:z 'x?y:z
248 ;; largest of the 2 branches
249 (let ((t1 (expr-type (subast2 ast)))
250 (t2 (expr-type (subast3 ast))))
257 (define-op2 'six.x:y 'x:y
265 (define-op2 'six.x%=y 'x%=y
272 (define-op2 'six.x&=y 'x&=y
279 (define-op2 'six.x*=y 'x*=y
286 (define-op2 'six.x+=y 'x+=y
293 (define-op2 'six.x-=y 'x-=y
300 (define-op2 'six.x/=y 'x/=y
307 (define-op2 'six.x<<=y 'x<<=y
314 (define-op2 'six.x=y 'x=y
321 (define-op2 'six.x>>=y 'x>>=y
328 (define-op2 'six.x^=y 'x^=y
335 (define-op2 '|six.x\|=y| '|x\|=y|
342 (define-op2 'six.x:=y 'x:=y
350 (define-op2 '|six.x,y| '|x,y|
358 (define-op2 'six.x:-y 'x:-y
366 (define (operation? source)
368 (let ((x (car source)))
369 (let loop ((lst operators))
372 ((eq? (op-six-id (car lst)) x)
375 (loop (cdr lst))))))))