Skip to content

Commit 4888d86

Browse files
committed
Fixes bug when creating instances.
1 parent 970580d commit 4888d86

File tree

178 files changed

+1937
-3914
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+1937
-3914
lines changed

src/core/elaboration.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ let rec unlink (t : type_) =
4949

5050
let checkMemExists (env : env) name =
5151
let f = Env.getCurrentFunction env in
52-
match Env.lookVarInScopes f.locals name with
53-
| Some { kind = Mem _ | Inst; _ } -> true
52+
(* Check context record where Inst/Mem variables are stored by addVar *)
53+
match f.context with
54+
| Some (_, { descr = Record members; _ }) -> (
55+
match Env.Map.find name members with
56+
| Some { kind = Mem _ | Inst; _ } -> true
57+
| _ -> false)
5458
| _ -> false
5559

5660

src/generators/js.ml

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,9 @@ open Core.Prog
2626

2727
let runtime =
2828
{%pla|
29+
// Runtime functions (simple builtins like eps, pi, clip, real, int_, sin, cos, etc. are inlined)
2930
this.random = function() { return Math.random(); };
3031
this.irandom = function() { return Math.floor(Math.random() * 4294967296); };
31-
this.eps = function() { return 1e-18 };
32-
this.pi = function() { return 3.1415926535897932384; }
33-
this.clip = function(x,low,high) { return x<low?low:(x>high?high:x); };
34-
this.not = function(x) { return x==0?1:0; };
35-
this.real = function(x) { return x; };
36-
this.int_ = function(x) { return x|0; };
37-
this.sin = function(x) { return Math.sin(x); };
38-
this.cos = function(x) { return Math.cos(x); };
39-
this.abs = function(x) { return Math.abs(x); };
40-
this.exp = function(x) { return Math.exp(x); };
41-
this.floor = function(x) { return Math.floor(x); };
42-
this.tan = function(x) { return Math.tan(x); };
43-
this.tanh = function(x) { return Math.tanh(x); };
44-
this.pow = function(a,b) { return Math.pow(a,b); };
45-
this.sqrt = function(x) { return x; };
4632
this.int_to_float = function(i) { return i; };
4733
this.float_to_int = function(i) { return Math.floor(i); };
4834
this.initializeArray = function(v, size){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };
@@ -142,6 +128,53 @@ let rec print_exp e =
142128
let i = print_exp i in
143129
let v = print_exp v in
144130
{%pla|(<#l#>[<#i#>] = <#v#>)|}
131+
(* Inline simple builtins to avoid function call overhead *)
132+
| ECall { path = "eps"; args = [] } -> {%pla|1e-18|}
133+
| ECall { path = "pi"; args = [] } -> {%pla|3.1415926535897932384|}
134+
| ECall { path = "real"; args = [ x ] } ->
135+
let x = print_exp x in
136+
{%pla|(<#x#>)|}
137+
| ECall { path = "int_"; args = [ x ] } ->
138+
(* Bitwise OR with 0 truncates to 32-bit integer *)
139+
let x = print_exp x in
140+
{%pla|((<#x#>)|0)|}
141+
| ECall { path = "not_"; args = [ x ] } ->
142+
let x = print_exp x in
143+
{%pla|((<#x#>) == 0 ? 1 : 0)|}
144+
| ECall { path = "clip"; args = [ x; low; high ] } ->
145+
let x = print_exp x in
146+
let low = print_exp low in
147+
let high = print_exp high in
148+
{%pla|((<#x#>) < (<#low#>) ? (<#low#>) : ((<#x#>) > (<#high#>) ? (<#high#>) : (<#x#>)))|}
149+
(* Inline Math functions *)
150+
| ECall { path = "sin"; args = [ x ] } ->
151+
let x = print_exp x in
152+
{%pla|Math.sin(<#x#>)|}
153+
| ECall { path = "cos"; args = [ x ] } ->
154+
let x = print_exp x in
155+
{%pla|Math.cos(<#x#>)|}
156+
| ECall { path = "abs"; args = [ x ] } ->
157+
let x = print_exp x in
158+
{%pla|Math.abs(<#x#>)|}
159+
| ECall { path = "exp"; args = [ x ] } ->
160+
let x = print_exp x in
161+
{%pla|Math.exp(<#x#>)|}
162+
| ECall { path = "floor"; args = [ x ] } ->
163+
let x = print_exp x in
164+
{%pla|Math.floor(<#x#>)|}
165+
| ECall { path = "tan"; args = [ x ] } ->
166+
let x = print_exp x in
167+
{%pla|Math.tan(<#x#>)|}
168+
| ECall { path = "tanh"; args = [ x ] } ->
169+
let x = print_exp x in
170+
{%pla|Math.tanh(<#x#>)|}
171+
| ECall { path = "sqrt"; args = [ x ] } ->
172+
let x = print_exp x in
173+
{%pla|Math.sqrt(<#x#>)|}
174+
| ECall { path = "pow"; args = [ a; b ] } ->
175+
let a = print_exp a in
176+
let b = print_exp b in
177+
{%pla|Math.pow(<#a#>, <#b#>)|}
145178
| ECall { path; args } ->
146179
let args = Pla.map_sep Pla.commaspace print_exp args in
147180
{%pla|this.<#path#s>(<#args#>)|}

src/generators/lua.ml

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,12 @@ else
104104
function rshift(a, b) return math.floor(a / (2 ^ b)) end
105105
end
106106

107-
-- Core runtime functions
107+
-- Core runtime functions (simple builtins like eps, pi, clip, real, int, bool are inlined)
108108
function ifExpressionValue(cond,then_,else_) if cond then return then_ else return else_ end end
109109
function ifExpression(cond,then_,else_) if cond then return then_() else return else_() end end
110-
function eps() return 1e-18 end
111-
function pi() return 3.1415926535897932384 end
112110
function random() return math.random() end
113111
function irandom() return math.floor(math.random() * 4294967296) end
114-
function clip(x,low,high) if x > high then return high else if x < low then return low else return x end end end
115-
function real(x) return x end
116-
function int(x) if type(x) == "boolean" then x = x and 1 or 0 end local int_part,_ = math.modf(x) return int_part end
117-
function int16(x) if type(x) == "boolean" then x = x and 1 or 0 end local int_part,_ = math.modf(x) return math.max(-32768, math.min(32767, int_part)) end
118-
function bool(x) return x ~= 0 and x ~= false end
119-
function set(a, i, v) a[i+1]=v end
120-
function get(a, i) return a[i+1] end
112+
function int16(x) local int_part,_ = math.modf(x) return math.max(-32768, math.min(32767, int_part)) end
121113
function intDiv(a, b) return math.floor(a / b) end
122114
function list_clear(t) for k in pairs(t) do t[k] = nil end end
123115

@@ -182,9 +174,10 @@ let rec print_exp e =
182174
let index = i + 1 in
183175
{%pla|<#e#>[<#index#i>]|}
184176
| EIndex { e; index } ->
177+
(* Inline index+1 to avoid function call overhead in standard Lua *)
185178
let e = print_exp e in
186179
let index = print_exp index in
187-
{%pla|get(<#e#>, <#index#>)|}
180+
{%pla|<#e#>[(<#index#>) + 1]|}
188181
| EArray l -> Pla.wrap (Pla.string "{") (Pla.string "}") (Pla.map_sep Pla.commaspace print_exp l)
189182
| ECall { path; args } -> (
190183
(* Use optimized functions when available *)
@@ -227,14 +220,38 @@ let rec print_exp e =
227220
| "list_get", [ l; i ] ->
228221
let l = print_exp l in
229222
let i = print_exp i in
230-
(* Use get() helper for 1-based indexing *)
231-
{%pla|get(<#l#>, <#i#>)|}
223+
(* Inline index+1 to avoid function call overhead *)
224+
{%pla|<#l#>[(<#i#>) + 1]|}
232225
| "list_set", [ l; i; v ] ->
233226
let l = print_exp l in
234227
let i = print_exp i in
235228
let v = print_exp v in
236229
(* Lua is 1-based *)
237230
{%pla|<#l#>[<#i#> + 1] = <#v#>|}
231+
(* Inline simple builtins to avoid function call overhead *)
232+
| "eps", [] -> {%pla|1e-18|}
233+
| "pi", [] -> {%pla|3.1415926535897932384|}
234+
| "real", [ x ] ->
235+
let x = print_exp x in
236+
{%pla|(<#x#>)|}
237+
| "int", [ x ] ->
238+
(* Inline int conversion using math.modf (truncates towards zero) *)
239+
let x = print_exp x in
240+
{%pla|(math.modf(<#x#>))|}
241+
| "bool", [ x ] ->
242+
(* Inline bool conversion *)
243+
let x = print_exp x in
244+
{%pla|((<#x#>) ~= 0 and (<#x#>) ~= false)|}
245+
| "not_", [ x ] ->
246+
(* Inline logical not *)
247+
let x = print_exp x in
248+
{%pla|(not (<#x#>))|}
249+
| "clip", [ x; low; high ] ->
250+
(* Inline clip as: (x > high) and high or ((x < low) and low or x) *)
251+
let x = print_exp x in
252+
let low = print_exp low in
253+
let high = print_exp high in
254+
{%pla|((<#x#>) > (<#high#>) and (<#high#>) or ((<#x#>) < (<#low#>) and (<#low#>) or (<#x#>)))|}
238255
| _ ->
239256
let args = Pla.map_sep Pla.commaspace print_exp args in
240257
{%pla|<#path#s>(<#args#>)|})

src/generators/python.ml

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,14 @@ let runtime =
2828
{%pla|import math
2929
import random as random_module
3030

31-
def eps():
32-
return 1e-18
33-
34-
def pi():
35-
return 3.1415926535897932384
31+
# Runtime functions (simple builtins like eps, pi, clip, sin, cos, etc. are inlined)
3632

3733
def random():
3834
return random_module.random()
3935

4036
def irandom():
4137
return int(random_module.random() * 4294967296)
4238

43-
def clip(x, low, high):
44-
return low if x < low else (high if x > high else x)
45-
46-
def not_(x):
47-
return 0 if x != 0 else 1
48-
49-
def real(x):
50-
return float(x)
51-
52-
def int_(x):
53-
return int(x)
54-
5539
def int_to_float(i):
5640
return float(i)
5741

@@ -149,6 +133,52 @@ let rec print_exp (e : exp) =
149133
let i = print_exp i in
150134
let v = print_exp v in
151135
{%pla|<#l#>.__setitem__(<#i#>, <#v#>)|}
136+
(* Inline simple builtins to avoid function call overhead *)
137+
| ECall { path = "eps"; args = [] } -> {%pla|1e-18|}
138+
| ECall { path = "pi"; args = [] } -> {%pla|3.1415926535897932384|}
139+
| ECall { path = "real"; args = [ x ] } ->
140+
let x = print_exp x in
141+
{%pla|float(<#x#>)|}
142+
| ECall { path = "int_"; args = [ x ] } ->
143+
let x = print_exp x in
144+
{%pla|int(<#x#>)|}
145+
| ECall { path = "not_"; args = [ x ] } ->
146+
let x = print_exp x in
147+
{%pla|(0 if (<#x#>) != 0 else 1)|}
148+
| ECall { path = "clip"; args = [ x; low; high ] } ->
149+
let x = print_exp x in
150+
let low = print_exp low in
151+
let high = print_exp high in
152+
{%pla|((<#low#>) if (<#x#>) < (<#low#>) else ((<#high#>) if (<#x#>) > (<#high#>) else (<#x#>)))|}
153+
(* Inline math functions *)
154+
| ECall { path = "sin"; args = [ x ] } ->
155+
let x = print_exp x in
156+
{%pla|math.sin(<#x#>)|}
157+
| ECall { path = "cos"; args = [ x ] } ->
158+
let x = print_exp x in
159+
{%pla|math.cos(<#x#>)|}
160+
| ECall { path = "abs"; args = [ x ] } ->
161+
let x = print_exp x in
162+
{%pla|abs(<#x#>)|}
163+
| ECall { path = "exp"; args = [ x ] } ->
164+
let x = print_exp x in
165+
{%pla|math.exp(<#x#>)|}
166+
| ECall { path = "floor"; args = [ x ] } ->
167+
let x = print_exp x in
168+
{%pla|math.floor(<#x#>)|}
169+
| ECall { path = "tan"; args = [ x ] } ->
170+
let x = print_exp x in
171+
{%pla|math.tan(<#x#>)|}
172+
| ECall { path = "tanh"; args = [ x ] } ->
173+
let x = print_exp x in
174+
{%pla|math.tanh(<#x#>)|}
175+
| ECall { path = "sqrt"; args = [ x ] } ->
176+
let x = print_exp x in
177+
{%pla|math.sqrt(<#x#>)|}
178+
| ECall { path = "pow"; args = [ a; b ] } ->
179+
let a = print_exp a in
180+
let b = print_exp b in
181+
{%pla|math.pow(<#a#>, <#b#>)|}
152182
| ECall { path; args } ->
153183
let args = Pla.map_sep Pla.commaspace print_exp args in
154184
{%pla|<#path#s>(<#args#>)|}

test/code/ConstSpecialization.js.base

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11

2+
// Runtime functions (simple builtins like eps, pi, clip, real, int_, sin, cos, etc. are inlined)
23
this.random = function() { return Math.random(); };
34
this.irandom = function() { return Math.floor(Math.random() * 4294967296); };
4-
this.eps = function() { return 1e-18 };
5-
this.pi = function() { return 3.1415926535897932384; }
6-
this.clip = function(x,low,high) { return x<low?low:(x>high?high:x); };
7-
this.not = function(x) { return x==0?1:0; };
8-
this.real = function(x) { return x; };
9-
this.int_ = function(x) { return x|0; };
10-
this.sin = function(x) { return Math.sin(x); };
11-
this.cos = function(x) { return Math.cos(x); };
12-
this.abs = function(x) { return Math.abs(x); };
13-
this.exp = function(x) { return Math.exp(x); };
14-
this.floor = function(x) { return Math.floor(x); };
15-
this.tan = function(x) { return Math.tan(x); };
16-
this.tanh = function(x) { return Math.tanh(x); };
17-
this.pow = function(a,b) { return Math.pow(a,b); };
18-
this.sqrt = function(x) { return x; };
195
this.int_to_float = function(i) { return i; };
206
this.float_to_int = function(i) { return Math.floor(i); };
217
this.initializeArray = function(v, size){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };

test/code/ConstSpecialization.lua.base

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,12 @@ else
7272
function rshift(a, b) return math.floor(a / (2 ^ b)) end
7373
end
7474

75-
-- Core runtime functions
75+
-- Core runtime functions (simple builtins like eps, pi, clip, real, int, bool are inlined)
7676
function ifExpressionValue(cond,then_,else_) if cond then return then_ else return else_ end end
7777
function ifExpression(cond,then_,else_) if cond then return then_() else return else_() end end
78-
function eps() return 1e-18 end
79-
function pi() return 3.1415926535897932384 end
8078
function random() return math.random() end
8179
function irandom() return math.floor(math.random() * 4294967296) end
82-
function clip(x,low,high) if x > high then return high else if x < low then return low else return x end end end
83-
function real(x) return x end
84-
function int(x) if type(x) == "boolean" then x = x and 1 or 0 end local int_part,_ = math.modf(x) return int_part end
85-
function int16(x) if type(x) == "boolean" then x = x and 1 or 0 end local int_part,_ = math.modf(x) return math.max(-32768, math.min(32767, int_part)) end
86-
function bool(x) return x ~= 0 and x ~= false end
87-
function set(a, i, v) a[i+1]=v end
88-
function get(a, i) return a[i+1] end
80+
function int16(x) local int_part,_ = math.modf(x) return math.max(-32768, math.min(32767, int_part)) end
8981
function intDiv(a, b) return math.floor(a / b) end
9082
function list_clear(t) for k in pairs(t) do t[k] = nil end end
9183

@@ -264,7 +256,7 @@ function ConstSpecialization_setMatrix2(m, row, col, value)
264256
end
265257

266258
function ConstSpecialization_getMatrix2(m, row, col)
267-
return get(m, (row + lshift(col, 1)))
259+
return m[((row + lshift(col, 1))) + 1]
268260
end
269261

270262
function ConstSpecialization_test_matrix_mutation(m, x)

test/code/ConstSpecialization.py.base

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
import math
22
import random as random_module
33

4-
def eps():
5-
return 1e-18
6-
7-
def pi():
8-
return 3.1415926535897932384
4+
# Runtime functions (simple builtins like eps, pi, clip, sin, cos, etc. are inlined)
95

106
def random():
117
return random_module.random()
128

139
def irandom():
1410
return int(random_module.random() * 4294967296)
1511

16-
def clip(x, low, high):
17-
return low if x < low else (high if x > high else x)
18-
19-
def not_(x):
20-
return 0 if x != 0 else 1
21-
22-
def real(x):
23-
return float(x)
24-
25-
def int_(x):
26-
return int(x)
27-
2812
def int_to_float(i):
2913
return float(i)
3014

test/code/Constants.js.base

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11

2+
// Runtime functions (simple builtins like eps, pi, clip, real, int_, sin, cos, etc. are inlined)
23
this.random = function() { return Math.random(); };
34
this.irandom = function() { return Math.floor(Math.random() * 4294967296); };
4-
this.eps = function() { return 1e-18 };
5-
this.pi = function() { return 3.1415926535897932384; }
6-
this.clip = function(x,low,high) { return x<low?low:(x>high?high:x); };
7-
this.not = function(x) { return x==0?1:0; };
8-
this.real = function(x) { return x; };
9-
this.int_ = function(x) { return x|0; };
10-
this.sin = function(x) { return Math.sin(x); };
11-
this.cos = function(x) { return Math.cos(x); };
12-
this.abs = function(x) { return Math.abs(x); };
13-
this.exp = function(x) { return Math.exp(x); };
14-
this.floor = function(x) { return Math.floor(x); };
15-
this.tan = function(x) { return Math.tan(x); };
16-
this.tanh = function(x) { return Math.tanh(x); };
17-
this.pow = function(a,b) { return Math.pow(a,b); };
18-
this.sqrt = function(x) { return x; };
195
this.int_to_float = function(i) { return i; };
206
this.float_to_int = function(i) { return Math.floor(i); };
217
this.initializeArray = function(v, size){ var a = new Array(size); for(var i=0;i<size;i++) a[i]=v; return a; };

test/code/Constants.lua.base

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,12 @@ else
7272
function rshift(a, b) return math.floor(a / (2 ^ b)) end
7373
end
7474

75-
-- Core runtime functions
75+
-- Core runtime functions (simple builtins like eps, pi, clip, real, int, bool are inlined)
7676
function ifExpressionValue(cond,then_,else_) if cond then return then_ else return else_ end end
7777
function ifExpression(cond,then_,else_) if cond then return then_() else return else_() end end
78-
function eps() return 1e-18 end
79-
function pi() return 3.1415926535897932384 end
8078
function random() return math.random() end
8179
function irandom() return math.floor(math.random() * 4294967296) end
82-
function clip(x,low,high) if x > high then return high else if x < low then return low else return x end end end
83-
function real(x) return x end
84-
function int(x) if type(x) == "boolean" then x = x and 1 or 0 end local int_part,_ = math.modf(x) return int_part end
85-
function int16(x) if type(x) == "boolean" then x = x and 1 or 0 end local int_part,_ = math.modf(x) return math.max(-32768, math.min(32767, int_part)) end
86-
function bool(x) return x ~= 0 and x ~= false end
87-
function set(a, i, v) a[i+1]=v end
88-
function get(a, i) return a[i+1] end
80+
function int16(x) local int_part,_ = math.modf(x) return math.max(-32768, math.min(32767, int_part)) end
8981
function intDiv(a, b) return math.floor(a / b) end
9082
function list_clear(t) for k in pairs(t) do t[k] = nil end end
9183

0 commit comments

Comments
 (0)