Skip to content

Commit

Permalink
Fixes for typing arithmetic on address space pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter committed Feb 2, 2024
1 parent d824ce7 commit c0c750a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/terralib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2455,15 +2455,15 @@ function typecheck(topexp,luaenv,simultaneousdefinitions)
end
local function ascompletepointer(exp) --convert pointer like things into pointers to _complete_ types
exp.type.type:tcomplete(exp)
return (insertcast(exp,terra.types.pointer(exp.type.type))) --parens are to truncate to 1 argument
return (insertcast(exp,terra.types.pointer(exp.type.type, exp.type.addressspace))) --parens are to truncate to 1 argument
end
-- subtracting 2 pointers
if pointerlike(l.type) and pointerlike(r.type) and l.type.type == r.type.type and e.operator == tokens["-"] then
return e:copy { operands = List {ascompletepointer(l),ascompletepointer(r)} }:withtype(terra.types.ptrdiff)
elseif pointerlike(l.type) and r.type:isintegral() then -- adding or subtracting a int to a pointer
return e:copy {operands = List {ascompletepointer(l),r} }:withtype(terra.types.pointer(l.type.type))
return e:copy {operands = List {ascompletepointer(l),r} }:withtype(terra.types.pointer(l.type.type, l.type.addressspace))
elseif l.type:isintegral() and pointerlike(r.type) then
return e:copy {operands = List {ascompletepointer(r),l} }:withtype(terra.types.pointer(r.type.type))
return e:copy {operands = List {ascompletepointer(r),l} }:withtype(terra.types.pointer(r.type.type, r.type.addressspace))
else
return meetbinary(e,"isarithmeticorvector",l,r)
end
Expand Down
26 changes: 26 additions & 0 deletions tests/addressspace.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Tests of pointers with address spaces.

-- The exact meaning of this depends on the target, but at least basic
-- code compilation should work.

local function ptr1(ty)
-- A pointer in address space 1.
return terralib.types.pointer(ty, 1)
end

terra test(x : &int, y : ptr1(int))
-- Should be able to do math on pointers with non-zero address spaces:
var a = [ptr1(int8)](y)
var b = a + 8
var c = [ptr1(int)](b)
var d = c - y
y = c

-- Casts should work:
y = [ptr1(int)](x)
x = [&int](y)

return d
end
test:compile()
print(test)

0 comments on commit c0c750a

Please sign in to comment.