diff --git a/src/terralib.lua b/src/terralib.lua index cc6b2d60..77181729 100644 --- a/src/terralib.lua +++ b/src/terralib.lua @@ -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 diff --git a/tests/addressspace.t b/tests/addressspace.t new file mode 100644 index 00000000..78653fee --- /dev/null +++ b/tests/addressspace.t @@ -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)