aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/tacky.clj
diff options
context:
space:
mode:
authorShagun Agrawal <agrawalshagun07@gmail.com>2025-03-13 23:43:02 +0530
committerShagun Agrawal <agrawalshagun07@gmail.com>2025-03-13 23:43:02 +0530
commite68884c7cd010cedf354312c3756dbbdc1a56129 (patch)
tree3b1ff402b1c8ef21dd66b7fcd3bd109a0dc3b4fe /src/cljcc/tacky.clj
parent24850daacdce70b94b089e80c40a44be0df34881 (diff)
Complete tacky generation phase for doubles
Diffstat (limited to 'src/cljcc/tacky.clj')
-rw-r--r--src/cljcc/tacky.clj44
1 files changed, 36 insertions, 8 deletions
diff --git a/src/cljcc/tacky.clj b/src/cljcc/tacky.clj
index 9e1961a..be60841 100644
--- a/src/cljcc/tacky.clj
+++ b/src/cljcc/tacky.clj
@@ -124,6 +124,26 @@
:src src
:dst dst})
+(defn- double-to-int-instruction [src dst]
+ {:type :double-to-int
+ :src src
+ :dst dst})
+
+(defn- double-to-uint-instruction [src dst]
+ {:type :double-to-uint
+ :src src
+ :dst dst})
+
+(defn- int-to-double-instruction [src dst]
+ {:type :int-to-double
+ :src src
+ :dst dst})
+
+(defn- uint-to-double-instruction [src dst]
+ {:type :uint-to-double
+ :src src
+ :dst dst})
+
(defn- copy-instruction [src dst]
{:type :copy
:src src
@@ -196,15 +216,21 @@
inner-type (tc/get-type typed-inner)
{res :val
insts :instructions} value
- cast-i (cond
- (= (u/get-type-size target-type)
- (u/get-type-size inner-type)) (copy-instruction res dst)
- (< (u/get-type-size target-type)
- (u/get-type-size inner-type)) (truncate-instruction res dst)
- (u/type-signed? inner-type) (sign-extend-instruction res dst)
- :else (zero-extend-instruction res dst))]
+ cast-inst (cond
+ (u/type-double? target-type) (if (u/type-signed? inner-type)
+ (int-to-double-instruction res dst)
+ (uint-to-double-instruction res dst))
+ (u/type-double? inner-type) (if (u/type-signed? target-type)
+ (double-to-int-instruction res dst)
+ (double-to-uint-instruction res dst))
+ (= (u/get-type-size target-type)
+ (u/get-type-size inner-type)) (copy-instruction res dst)
+ (< (u/get-type-size target-type)
+ (u/get-type-size inner-type)) (truncate-instruction res dst)
+ (u/type-signed? inner-type) (sign-extend-instruction res dst)
+ :else (zero-extend-instruction res dst))]
{:val dst
- :instructions (flatten [insts cast-i])})))
+ :instructions (flatten [insts cast-inst])})))
(defmethod exp-handler :unary-exp
[exp symbols]
@@ -599,6 +625,8 @@ int foo;
int foo;
int main(void) {
+ double x = 1000;
+
for (int i = 0; i < 5; i = i + 1)
foo = foo + 1;
return foo;