Skip to content

Commit

Permalink
Merge pull request #68 from adi-lb-phoenix/branch_3
Browse files Browse the repository at this point in the history
Introduce more types and add type conversion
  • Loading branch information
chsasank authored Jul 15, 2024
2 parents 0077368 + b056660 commit 85fbe11
Show file tree
Hide file tree
Showing 29 changed files with 367 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/backend/brilisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ def is_value(instr):
"fgt",
"fle",
"fge",
"sitofp",
"fptosi",
"sext",
"trunc",
"fptrunc",
"fpext",
"fptoui",
"uitofp",
"ptrtoint",
"inttoptr",
"zext",
"bitcast",
}
return (instr[0] == "set") and (instr[2][0] in value_op)

Expand Down
40 changes: 39 additions & 1 deletion src/backend/llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@ def gen_type(self, type):
return self.gen_type(type["ptr"]).as_pointer()
else:
raise CodegenError(f"Unknown type {type}")
elif type == "int":
elif type in ["int", "int32"]:
return ir.IntType(32)
elif type == "void":
return ir.VoidType()
elif type == "bool":
return ir.IntType(1)
elif type == "float":
return ir.FloatType()
elif type == "double":
return ir.DoubleType()
elif type == "int64":
return ir.IntType(64)
elif type == "int16":
return ir.IntType(16)
elif type == "int8":
return ir.IntType(8)
else:
raise CodegenError(f"Unknown type {type}")

Expand Down Expand Up @@ -106,6 +114,21 @@ def gen_instructions(self, instrs):
"fne": "!=",
}

cast_ops = {
"trunc": "trunc",
"zext": "zext",
"sext": "sext",
"fptrunc": "fptrunc",
"fpext": "fpext",
"fptoui": "fptoui",
"uitofp": "uitofp",
"fptosi": "fptosi",
"sitofp": "sitofp",
"ptrtoint": "ptrtoint",
"inttoptr": "inttoptr",
"bitcast": "bitcast"
}

def gen_label(instr):
old_bb = self.builder.block
new_bb = self.gen_label(instr.label)
Expand Down Expand Up @@ -216,6 +239,19 @@ def gen_ptradd(instr):
),
)

def gen_castop(instr):
"""this function creates an IR for casting types"""
self.declare_var(self.gen_type(instr.type), instr.dest)
llvm_instr = getattr(self.builder, cast_ops[instr.op])
self.gen_symbol_store(
instr.dest,
llvm_instr(
self.gen_var(instr.args[0]),
self.gen_type(instr.type),
name=instr.dest,
),
)

def gen_id(instr):
self.declare_var(self.gen_type(instr.type), instr.dest)
self.gen_symbol_store(instr.dest, self.gen_symbol_load(instr.args[0]))
Expand Down Expand Up @@ -254,6 +290,8 @@ def gen_id(instr):
gen_comp(instr)
elif instr.op in fcmp_ops:
gen_fcomp(instr)
elif instr.op in cast_ops:
gen_castop(instr)
else:
raise CodegenError(f"Unknown op in the instruction: {dict(instr)}")
except Exception as e:
Expand Down
4 changes: 4 additions & 0 deletions src/backend/tests/brilisp/castop_bitcast.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
64
226
1
0
41 changes: 41 additions & 0 deletions src/backend/tests/brilisp/castop_bitcast.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
;not verified as llvm is not being emitted when doing bitcast from same number of bits to same number of bits.
(brilisp
(define ((print int32) (n int32)))

(define ((custom_print int32) (n int8))
(set (n_int32 int) (zext n int))
(set (tmp int) (call print n_int32))
(ret tmp))

(define ((main void))
(set (size int) (const 1))
(set (int32_arr (ptr int)) (alloc size))

(set (value int) (const 123456))
(store int32_arr value)

(set (int8_arr (ptr int8)) (bitcast int32_arr int8))

; int8_arr[0]
(set (out_val int8) (load int8_arr))
(set (tmp int) (call custom_print out_val))

; int8_arr[1]
(set (array_idx int) (const 1))
(set (int8_arr_idx (ptr int8)) (ptradd int8_arr array_idx))
(set (out_val int8) (load int8_arr_idx))
(set (tmp int) (call custom_print out_val))

; int8_arr[2]
(set (array_idx int) (const 2))
(set (int8_arr_idx (ptr int8)) (ptradd int8_arr array_idx))
(set (out_val int8) (load int8_arr_idx))
(set (tmp int) (call custom_print out_val))

; int8_arr[3]
(set (array_idx int) (const 3))
(set (int8_arr_idx (ptr int8)) (ptradd int8_arr array_idx))
(set (out_val int8) (load int8_arr_idx))
(set (tmp int) (call custom_print out_val))

(ret)))
1 change: 1 addition & 0 deletions src/backend/tests/brilisp/castop_fpext.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.125000
10 changes: 10 additions & 0 deletions src/backend/tests/brilisp/castop_fpext.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
;%X = fpext float 3.125 to double ; yields double:3.125000e+00
; %Y = fpext double %X to fp128; yields fp128:0xL00000000000000004000900000000000 llvmlit for fp128 not found
(brilisp
(define ((dprint double) (n double)))

(define ((main void))
(set (a float) (const 3.125))
(set (b double) (fpext a double))
(set (tmp double) (call dprint b))
(ret)))
2 changes: 2 additions & 0 deletions src/backend/tests/brilisp/castop_fptosi.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-4
14
18 changes: 18 additions & 0 deletions src/backend/tests/brilisp/castop_fptosi.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(brilisp
(define ((print int) (n int)))

(define ((main int))
;testing fptosi on a floating point number
(set (five int) (const 5))
(set (a float) (const -9.7123))
(set (f_n int) (fptosi a int))
(set (sum int) (add f_n five))
(set (tmp int) (call print sum))

;testing fptosi on a integer number
(set (c int) (const 9))
(set (f_n int) (fptosi c int))
(set (sum int) (add f_n five))
(set (tmp int) (call print sum))

(ret tmp)))
1 change: 1 addition & 0 deletions src/backend/tests/brilisp/castop_fptoui.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
11 changes: 11 additions & 0 deletions src/backend/tests/brilisp/castop_fptoui.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;%Y = fptoui float 1.0E+300 to i1 ; yields undefined:1
;%Z = fptoui float 1.04E+17 to i8 ; yields undefined:1 left to implement

(brilisp
(define ((print int32) (n int32)))

(define ((main void))
(set (a double) (const 123.0))
(set (f_n int32) (fptoui a int32))
(set (tmp int32) (call print f_n))
(ret)))
1 change: 1 addition & 0 deletions src/backend/tests/brilisp/castop_fptrunc.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16777216.000000
9 changes: 9 additions & 0 deletions src/backend/tests/brilisp/castop_fptrunc.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;test for fptrunc double 1.0E+300 to half not done
(brilisp
(define ((fprint float) (n float)))

(define ((main void))
(set (a double) (const 16777217.0))
(set (f_n float) (fptrunc a float))
(set (tmp float) (call fprint f_n))
(ret)))
4 changes: 4 additions & 0 deletions src/backend/tests/brilisp/castop_inttoptr_ptrtoint.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0xffffff01
-255
0xff
255
27 changes: 27 additions & 0 deletions src/backend/tests/brilisp/castop_inttoptr_ptrtoint.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(brilisp
(define ((print int) (n int)))

(define ((ptr_print (ptr int)) (n (ptr int))))

(define ((main void))
(set (a int) (const -255))

; we will convert an integer to pointer value.
(set (pointer_returned (ptr int)) (inttoptr a (ptr int)))
(set (return_print_1 (ptr int)) (call ptr_print pointer_returned))

; convert the returned pointer value to int.
(set (return_value int) (ptrtoint pointer_returned int))
(set (return_print_2 int) (call print return_value))

; repeating the above process for a +ve int.
(set (d int) (const 255))

; we will convert an integer to pointer value.
(set (pointer_returned_1 (ptr int)) (inttoptr d (ptr int)))
(set (return_print_3 (ptr int)) (call ptr_print pointer_returned_1))

; convert the returned pointer value to int.
(set (return_value_1 int) (ptrtoint pointer_returned_1 int))
(set (return_print_4 int) (call print return_value_1))
(ret)))
3 changes: 3 additions & 0 deletions src/backend/tests/brilisp/castop_sext.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-1
127
255
17 changes: 17 additions & 0 deletions src/backend/tests/brilisp/castop_sext.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
;test for sext i1 true to i32 not completed
(brilisp
(define ((print int) (n int)))

(define ((main void))
(set (a int8) (const -1))
(set (f_n int) (sext a int))
(set (tmp int) (call print f_n))

(set (b int8) (const 127))
(set (f_n int) (sext b int))
(set (tmp int) (call print f_n))

; test zext too
(set (f_n int) (zext a int))
(set (tmp int) (call print f_n))
(ret)))
2 changes: 2 additions & 0 deletions src/backend/tests/brilisp/castop_sitofp.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-3.920000
14.130000
20 changes: 20 additions & 0 deletions src/backend/tests/brilisp/castop_sitofp.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(brilisp
(define ((fprint float) (n float)))
(define ((dprint double) (n double)))

(define ((main void))
;testing for float to float conversion with a negative float value.
(set (a float) (const -9.12))
(set (five float) (const 5.2))
(set (f_n float) (sitofp a float))
(set (sum float) (fadd f_n five))
(set (tmp float) (call fprint sum))

;testing for int to float conversion with a postive int
(set (c int) (const 9))
(set (five double) (const 5.13))
(set (f_n double) (sitofp c double))
(set (sum double) (fadd f_n five))
(set (tmp double) (call dprint sum))
(ret)))

1 change: 1 addition & 0 deletions src/backend/tests/brilisp/castop_sxregression.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127
9 changes: 9 additions & 0 deletions src/backend/tests/brilisp/castop_sxregression.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(brilisp
(define ((print int) (n int)))

(define (( main void))
(set (a int) (const 127))
(set (f_n int8) (trunc a int8))
(set (a_n int) (sext f_n int))
(set (tmp int) (call print a_n))
(ret)))
1 change: 1 addition & 0 deletions src/backend/tests/brilisp/castop_trunc.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
8 changes: 8 additions & 0 deletions src/backend/tests/brilisp/castop_trunc.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(brilisp
(define ((print int8) (n int8)))

(define ((main void))
(set (a int32) (const 257))
(set (f_n int8) (trunc a int8))
(set (tmp int8) (call print f_n))
(ret)))
2 changes: 2 additions & 0 deletions src/backend/tests/brilisp/castop_uitofp.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
257.000000
255.000000
13 changes: 13 additions & 0 deletions src/backend/tests/brilisp/castop_uitofp.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(brilisp
(define ((fprint float) (n float)))
(define ((dprint double) (n double)))

(define ((main void))
(set (a int32) (const 257))
(set (b float) (uitofp a float))
(set (tmp float) (call fprint b))

(set (a int8) (const -1))
(set (b double) (uitofp a double))
(set (tmp double) (call dprint b))
(ret)))
2 changes: 2 additions & 0 deletions src/backend/tests/brilisp/castop_zext.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
257
65535
13 changes: 13 additions & 0 deletions src/backend/tests/brilisp/castop_zext.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;test fo zext i1 true to i32 not completed.
(brilisp
(define ((print int) (n int)))

(define ((main void))
(set (a int16) (const 257))
(set (b int) (zext a int))
(set (tmp int) (call print b))

(set (a int16) (const -1))
(set (b int) (zext a int))
(set (tmp int) (call print b))
(ret)))
16 changes: 16 additions & 0 deletions src/backend/tests/brilisp/printf_int_range.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-128
127
127
-128
-32768
32767
32767
-32768
-2147483648
2147483647
2147483647
-2147483648
-9223372036854775808
9223372036854775807
9223372036854775807
-9223372036854775808
Loading

0 comments on commit 85fbe11

Please sign in to comment.