Skip to content

Commit

Permalink
gear3: mangle array types based on length (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Jan 7, 2025
1 parent e361ece commit 4eed499
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/gear3/expander.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ proc mangleImpl(b: var Mangler; c: var Cursor) =
mangleImpl b, c # type is interesting
skip c # value
inc c # ParRi
elif tag == "array":
b.addTree tag
inc c
mangleImpl b, c # type is interesting
if c.kind == ParLe and c.typeKind == RangeT:
inc c # RangeT
skip c # type is irrelevant, we care about the length
assert c.kind == IntLit
let first = pool.integers[c.intId]
inc c
assert c.kind == IntLit
let last = pool.integers[c.intId]
inc c
inc c # ParRi
b.addIntLit(last - first + 1)
else:
mangleImpl b, c
inc nested
else:
b.addTree(tag)
inc nested
Expand Down
1 change: 1 addition & 0 deletions src/nimony/typenav.nim
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ proc getTypeImpl(c: var TypeCache; n: Cursor): Cursor =
buf.addSubtree elemType
var n = n
var arrayLen = 0
inc n # skips AconstrX
while n.kind != ParRi:
skip n
inc arrayLen
Expand Down
18 changes: 17 additions & 1 deletion tests/nimony/sysbasics/tbasics.nif
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@
(i -1)
(rangetype
(i -1) +0 +4)) 12
(arr 1 +1 4 +2 7 +3 10 +4 13 +5)))
(arr 1 +1 4 +2 7 +3 10 +4 13 +5)) ,7
(proc 5 :foo.0.tbawx6nu81 . . . 9
(params) . . . 2,1
(stmts 4
(var :x.0 . . ~2,~5
(array
(i -1)
(rangetype
(i -1) +0 +2)) 19
(arr 1 +5 4 +6 7 +7)) 4,1
(var :m.0 . .
(array
(i -1)
(rangetype
(i -1) +0 +3)) 4
(arr 1 +5 4 +6 7 +7 10 +8)))) 3,11
(call ~3 foo.0.tbawx6nu81))
8 changes: 7 additions & 1 deletion tests/nimony/sysbasics/tbasics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ type

var s = [1, 2, 3]
var s2: Array
var s3: Array = [1, 2, 3, 4, 5]
var s3: Array = [1, 2, 3, 4, 5]

proc foo =
var x: array[3, int] = [5, 6, 7]
var m = [5, 6, 7, 8]

foo()

0 comments on commit 4eed499

Please sign in to comment.