Skip to content

Commit 929a2aa

Browse files
Aarav SethiAarav Sethi
authored andcommitted
Integer overflow
1 parent 3d48a64 commit 929a2aa

6 files changed

Lines changed: 82 additions & 22 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ reasm
55
main.luau
66
bench
77
sourcemap.json
8+
*.c
9+
main
10+
platform.info

compiler/binary.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ func sra(w *OutputWriter, command AssemblyCommand) {
1313

1414
/* Comparision */
1515
func slt(w *OutputWriter, command AssemblyCommand) { /* sltu & sltui instructions */
16-
WriteIndentedString(w, "%s = if (%s < %s) then 1 else 0\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
16+
if command.Name == "sltu" || command.Name == "sltiu" {
17+
WriteIndentedString(w, "%s = if (u32(%s) < u32(%s)) then 1 else 0\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
18+
} else {
19+
WriteIndentedString(w, "%s = if (i32(%s) < i32(%s)) then 1 else 0\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
20+
}
1721
}
1822
func seqz(w *OutputWriter, command AssemblyCommand) {
1923
WriteIndentedString(w, "%s = if (%s == 0) then 1 else 0\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))

compiler/boilerplate.luau

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ local mallocDepth = 0
1313
local stdout_cache = ""
1414

1515
-- Utility
16+
--- 32-bit helpers
17+
local function mask32(v)
18+
return bit32.band(v, 0xFFFFFFFF)
19+
end
20+
local function u32(v)
21+
return mask32(v)
22+
end
23+
local function i32(v)
24+
local n = mask32(v)
25+
if n >= 0x80000000 then
26+
return n - 0x100000000
27+
end
28+
return n
29+
end
30+
1631
--- Base (can be found in generated code)
1732
local function idiv_trunc(a, b)
1833
if b == 0 then error("division by zero") end
@@ -124,9 +139,9 @@ function format_string(fmt, args)
124139
arg_index += 1
125140

126141
if spec:sub(-1) == "d" then
127-
return tostring(val)
142+
return tostring(i32(val))
128143
elseif spec:sub(-1) == "X" then
129-
return string.format("%X", val)
144+
return string.format("%X", u32(val))
130145
elseif spec:sub(-1) == "f" then
131146
local low = args[arg_index]; arg_index+=1;
132147
local high = args[arg_index]; arg_index+=1;

compiler/branches.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ func jr(w *OutputWriter, command AssemblyCommand) {
5252

5353
/** Branching */
5454
func blt(w *OutputWriter, command AssemblyCommand) {
55-
WriteIndentedString(w, "if %s < %s then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
55+
if command.Name == "bltu" {
56+
WriteIndentedString(w, "if u32(%s) < u32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
57+
} else {
58+
WriteIndentedString(w, "if i32(%s) < i32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
59+
}
5660
w.Depth++
5761
JumpTo(w, command.Arguments[2].Source, false)
5862
w.Depth--
@@ -73,7 +77,11 @@ func bne(w *OutputWriter, command AssemblyCommand) {
7377
WriteIndentedString(w, "end\n")
7478
}
7579
func bge(w *OutputWriter, command AssemblyCommand) {
76-
WriteIndentedString(w, "if %s >= %s then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
80+
if command.Name == "bgeu" {
81+
WriteIndentedString(w, "if u32(%s) >= u32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
82+
} else {
83+
WriteIndentedString(w, "if i32(%s) >= i32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
84+
}
7785
w.Depth++
7886
JumpTo(w, command.Arguments[2].Source, false)
7987
w.Depth--
@@ -94,14 +102,22 @@ func beq(w *OutputWriter, command AssemblyCommand) {
94102
WriteIndentedString(w, "end\n")
95103
}
96104
func bgt(w *OutputWriter, command AssemblyCommand) {
97-
WriteIndentedString(w, "if %s > %s then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
105+
if command.Name == "bgtu" {
106+
WriteIndentedString(w, "if u32(%s) > u32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
107+
} else {
108+
WriteIndentedString(w, "if i32(%s) > i32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
109+
}
98110
w.Depth++
99111
JumpTo(w, command.Arguments[2].Source, false)
100112
w.Depth--
101113
WriteIndentedString(w, "end\n")
102114
}
103115
func ble(w *OutputWriter, command AssemblyCommand) {
104-
WriteIndentedString(w, "if %s <= %s then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
116+
if command.Name == "bleu" {
117+
WriteIndentedString(w, "if u32(%s) <= u32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
118+
} else {
119+
WriteIndentedString(w, "if i32(%s) <= i32(%s) then\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
120+
}
105121
w.Depth++
106122
JumpTo(w, command.Arguments[2].Source, false)
107123
w.Depth--

compiler/compilation.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ import (
1010
//go:embed boilerplate.luau
1111
var luau_boilerplate string
1212

13+
/* Float arithmetic helpers to avoid integer wrapping */
14+
func fadd(w *OutputWriter, command AssemblyCommand) {
15+
WriteIndentedString(w, "%s = %s + %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
16+
}
17+
func fsub(w *OutputWriter, command AssemblyCommand) {
18+
WriteIndentedString(w, "%s = %s - %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
19+
}
20+
func fmul(w *OutputWriter, command AssemblyCommand) {
21+
WriteIndentedString(w, "%s = %s * %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
22+
}
23+
func fdiv(w *OutputWriter, command AssemblyCommand) {
24+
WriteIndentedString(w, "%s = %s / %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
25+
}
26+
1327
var instructions = map[string]func(*OutputWriter, AssemblyCommand){
1428
/* bit shifts */
1529
"sll": sll,
@@ -113,19 +127,19 @@ var instructions = map[string]func(*OutputWriter, AssemblyCommand){
113127
"fclass.d": fclass,
114128

115129
/** Arithmetic */
116-
"fadd.s": add,
117-
"fsub.s": sub,
118-
"fdiv.s": div,
119-
"fmul.s": mul,
120-
"fadd.d": add,
121-
"fsub.d": sub,
122-
"fdiv.d": div,
123-
"fmul.d": mul,
130+
"fadd.s": fadd,
131+
"fsub.s": fsub,
132+
"fdiv.s": fdiv,
133+
"fmul.s": fmul,
134+
"fadd.d": fadd,
135+
"fsub.d": fsub,
136+
"fdiv.d": fdiv,
137+
"fmul.d": fmul,
124138
"fneg.s": fneg,
125139
"fneg.d": fneg,
126140

127141
/** More advanced */
128-
"fabs.s": fneg,
142+
"fabs.s": fabs,
129143
"fabs.d": fabs,
130144
"fsqrt.s": fsqrt,
131145
"fmin.s": fmin,

compiler/math.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ package compiler
22

33
/* Math */
44
func add(w *OutputWriter, command AssemblyCommand) { /* add & addi instructions */
5-
WriteIndentedString(w, "%s = %s + %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
5+
WriteIndentedString(w, "%s = i32(%s + %s)\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
66
}
77
func sub(w *OutputWriter, command AssemblyCommand) { /* sub & subi instructions */
8-
WriteIndentedString(w, "%s = %s - %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
8+
WriteIndentedString(w, "%s = i32(%s - %s)\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
99
}
1010
func mul(w *OutputWriter, command AssemblyCommand) { /* mul & muli instructions */
11-
WriteIndentedString(w, "%s = %s * %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
11+
WriteIndentedString(w, "%s = i32(%s * %s)\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
1212
}
1313
func div(w *OutputWriter, command AssemblyCommand) { /* div & divi instructions */
14-
WriteIndentedString(w, "%s = idiv_trunc(%s, %s)\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
14+
if command.Name == "divu" {
15+
WriteIndentedString(w, "%s = u32(idiv_trunc(u32(%s), u32(%s)))\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
16+
} else {
17+
WriteIndentedString(w, "%s = i32(idiv_trunc(i32(%s), i32(%s)))\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
18+
}
1519
}
1620
func rem(w *OutputWriter, command AssemblyCommand) { /* rem & remi instructions */
17-
WriteIndentedString(w, "%s = %s %% %s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
21+
if command.Name == "remu" {
22+
WriteIndentedString(w, "%s = u32(u32(%s) %% u32(%s))\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
23+
} else {
24+
WriteIndentedString(w, "%s = i32(i32(%s) %% i32(%s))\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]), CompileRegister(w, command.Arguments[2]))
25+
}
1826
}
1927
func neg(w *OutputWriter, command AssemblyCommand) { /* neg & negi instructions */
20-
WriteIndentedString(w, "%s = -%s\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
28+
WriteIndentedString(w, "%s = i32(-i32(%s))\n", CompileRegister(w, command.Arguments[0]), CompileRegister(w, command.Arguments[1]))
2129
}
2230

2331
/** Math Descendants */

0 commit comments

Comments
 (0)