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
40 ;; the standard says it should be int
41 (define (type-rule-int-comp-op2 ast)
44 (define (type-rule-bool-op2 ast)
48 (define (constant-fold-op1 op)
50 (let* ((x (subast1 ast))
51 (lx (cond ((literal? x)
55 (let ((val ((op-constant-fold op) x)))
56 (if (literal? val) val #f))))
59 (new-literal (expr-type ast) (op lx))
62 (define (constant-fold-op2 op)
64 (let* ((x (subast1 ast))
66 (lx (cond ((literal? x)
70 (let ((val ((op-constant-fold op) x)))
71 (if (literal? val) val #f))))
73 (ly (cond ((literal? y)
77 (let ((val ((op-constant-fold op) y)))
78 (if (literal? val) val #f))))
81 (new-literal (expr-type ast) (op lx ly))
84 (define-op1 'six.!x '!x
92 (define-op1 'six.++x '++x
99 (define-op1 'six.x++ 'x++
106 (define-op1 'six.--x '--x
113 (define-op1 'six.x-- 'x--
120 (define-op1 'six.~x '~x
127 (define-op2 'six.x%y 'x%y
129 (constant-fold-op2 modulo)
133 (define-op2 'six.x*y 'x*y
135 (constant-fold-op2 *)
139 (define-op1 'six.*x '*x
141 'byte) ; we only have byte arrays
147 (define-op2 'six.index 'index
149 'byte) ; we only have byte arrays
155 (define-op2 'six.x/y 'x/y
157 (constant-fold-op2 /)
161 (define-op2 'six.x+y 'x+y
163 (constant-fold-op2 +)
167 (define-op1 'six.+x '+x
174 (define-op2 'six.x-y 'x-y
176 (constant-fold-op2 -)
180 (define-op1 'six.-x '-x
182 (constant-fold-op1 (lambda (x) (- x)))
186 (define-op2 'six.x<<y 'x<<y
188 (constant-fold-op2 arithmetic-shift)
192 (define-op2 'six.x>>y 'x>>y
194 (constant-fold-op2 (lambda (x y) (arithmetic-shift x (- y))))
198 (define-op2 'six.x<y 'x<y
199 type-rule-int-comp-op2
200 (constant-fold-op2 (lambda (x y) (if (< x y) 1 0)))
204 (define-op2 'six.x<=y 'x<=y
205 type-rule-int-comp-op2
206 (constant-fold-op2 (lambda (x y) (if (<= x y) 1 0)))
210 (define-op2 'six.x>y 'x>y
211 type-rule-int-comp-op2
212 (constant-fold-op2 (lambda (x y) (if (> x y) 1 0)))
216 (define-op2 'six.x>=y 'x>=y
217 type-rule-int-comp-op2
218 (constant-fold-op2 (lambda (x y) (if (>= x y) 1 0)))
222 (define-op2 'six.x!=y 'x!=y
223 type-rule-int-comp-op2
224 (constant-fold-op2 (lambda (x y) (if (not (= x y)) 1 0)))
228 (define-op2 'six.x==y 'x==y
229 type-rule-int-comp-op2
230 (constant-fold-op2 (lambda (x y) (if (= x y) 1 0)))
234 (define-op2 'six.x&y 'x&y
236 (constant-fold-op2 bitwise-and)
240 (define-op1 'six.&x '&x
248 (define-op2 'six.x^y 'x^y
250 (constant-fold-op2 bitwise-xor)
254 (define-op2 '|six.x\|y| '|x\|y|
256 (constant-fold-op2 bitwise-ior)
260 (define-op2 'six.x&&y 'x&&y
262 (constant-fold-op2 (lambda (x y) (if (and (not (= x 0)) (not (= y 0))) 1 0)))
266 (define-op2 '|six.x\|\|y| '|x\|\|y|
268 (constant-fold-op2 (lambda (x y) (if (or (not (= x 0)) (not (= y 0))) 1 0)))
272 (define-op3 'six.x?y:z 'x?y:z
274 ;; largest of the 2 branches
275 (let ((t1 (expr-type (subast2 ast)))
276 (t2 (expr-type (subast3 ast))))
283 (define-op2 'six.x:y 'x:y
291 (define-op2 'six.x%=y 'x%=y
298 (define-op2 'six.x&=y 'x&=y
305 (define-op2 'six.x*=y 'x*=y
312 (define-op2 'six.x+=y 'x+=y
319 (define-op2 'six.x-=y 'x-=y
326 (define-op2 'six.x/=y 'x/=y
333 (define-op2 'six.x<<=y 'x<<=y
340 (define-op2 'six.x=y 'x=y
347 (define-op2 'six.x>>=y 'x>>=y
354 (define-op2 'six.x^=y 'x^=y
361 (define-op2 '|six.x\|=y| '|x\|=y|
368 (define-op2 'six.x:=y 'x:=y
376 (define-op2 '|six.x,y| '|x,y|
384 (define-op2 'six.x:-y 'x:-y
392 (define (operation? source)
394 (let ((x (car source)))
395 (let loop ((lst operators))
398 ((eq? (op-six-id (car lst)) x)
401 (loop (cdr lst))))))))