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 ;; TODO really ignore the void issue ? assigning the "result" of a void function to an int variable should be an error
20 (define (type-rule-int-op1 ast)
21 (expr-type (subast1 ast)))
23 (define (largest t1 t2)
24 (if (> (type->bytes t1) (type->bytes t2))
28 (define (type-rule-int-op2 ast)
29 ;; used for any binary operation involving two integers where the result is
30 ;; of the size of the biggest operand (subtraction, bitwise operations, ...)
31 (let ((t1 (expr-type (subast1 ast)))
32 (t2 (expr-type (subast2 ast))))
35 (define (type-rule-assign ast)
36 (let ((t1 (expr-type (subast1 ast))))
37 ;; the type of the rhs is irrelevant, since it will be promoted
38 ;; or truncated at the cfg level
41 (define (type-rule-int-comp-op2 ast)
42 'bool) ;; TODO why even bother ? anything can be casted to int to be used as argument here, old version is in garbage (and in version control) if needed
44 (define (type-rule-bool-op2 ast)
45 'bool) ;; TODO same here
47 (define-op1 'six.!x '!x
49 (lambda (ast) ;; TODO implement these ?
55 (define-op1 'six.++x '++x
62 (define-op1 'six.x++ 'x++
69 (define-op1 'six.--x '--x
76 (define-op1 'six.x-- 'x--
83 (define-op1 'six.~x '~x
90 (define-op2 'six.x%y 'x%y
92 ;; if we know the second operand, we can have an upper bound on the size
94 (if (literal? (subast1 ast))
95 ;; the number of bits needed by the result is lg(y)
96 (bytes->type (ceiling (/ (log (literal-val (subast1 ast))) (log 2) 8)))
97 ;; fall back to the general case
98 (type-rule-int-op2 ast))) ;; TODO is this optimization worth it, or does it break the samentics of C ?
104 (define-op2 'six.x*y 'x*y
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 ;; if we know the second operand, we can have an upper bound on the size
131 (if (literal? (subast1 ast))
132 ;; for every byte over 1 in the length of y, we can remove a byte from
134 ;; ex : the smallest value which needs 2 bytes to encode is 256, and
135 ;; dividing by 256 is equivalent to truncating the 8 lowest bits, and
137 (let ((l1 (type->bytes (expr-type (subast1 ast))))
138 (l2 (ceiling (/ (log y) (log 2) 8))))
139 (bytes->type (- (max l1 l2) (- l2 1))))
140 ;; fall back to the general case
141 (type-rule-int-op2 ast))) ;; TODO as for modulo, is this optimisation worth it ? if so, & could have a similar or, by being the size of the smaller operand
147 (define-op2 'six.x+y 'x+y
154 (define-op1 'six.+x '+x
161 (define-op2 'six.x-y 'x-y
168 (define-op1 'six.-x '-x
175 (define-op2 'six.x<<y 'x<<y ;; TODO for the general case, would give scary results (a single byte for y can still mean a shift by 255)
177 (if (not (literal? (subast2 ast)))
178 (error "only shifting by literals is supported"))
179 (let ((l1 (type->bytes (expr-type (subast1 ast))))
180 (v2 (literal-val (subast2 ast))))
181 ;; we might have to add some bytes to the result
182 (bytes->type (+ l1 (ceiling (/ v2 8))))))
188 (define-op2 'six.x>>y 'x>>y
190 (if (not (literal? (subast2 ast)))
191 (error "only shifting by literals is supported"))
192 (let ((l1 (type->bytes (expr-type (subast1 ast))))
193 (v2 (literal-val (subast2 ast))))
194 ;; we might be able to shave some bytes off
195 (bytes->type (- l1 (floor (/ v2 8))))))
201 (define-op2 'six.x<y 'x<y
202 type-rule-int-comp-op2
208 (define-op2 'six.x<=y 'x<=y
209 type-rule-int-comp-op2
215 (define-op2 'six.x>y 'x>y
216 type-rule-int-comp-op2
222 (define-op2 'six.x>=y 'x>=y
223 type-rule-int-comp-op2
229 (define-op2 'six.x!=y 'x!=y
230 type-rule-int-comp-op2
236 (define-op2 'six.x==y 'x==y
237 type-rule-int-comp-op2
243 (define-op2 'six.x&y 'x&y
250 (define-op1 'six.&x '&x
258 (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-op3 'six.x?y:z 'x?y:z
288 ;; largest of the 2 branches
289 (let ((t1 (expr-type (subast2 ast)))
290 (t2 (expr-type (subast3 ast))))
297 (define-op2 'six.x:y 'x:y
305 (define-op2 'six.x%=y 'x%=y ;; TODO these don't work
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
375 (define-op2 '|six.x\|=y| '|x\|=y|
382 (define-op2 'six.x:=y 'x:=y
390 (define-op2 '|six.x,y| '|x,y|
398 (define-op2 'six.x:-y 'x:-y
406 (define (operation? source)
408 (let ((x (car source)))
409 (let loop ((lst operators))
412 ((eq? (op-six-id (car lst)) x)
415 (loop (cdr lst))))))))