From 70a5348f430a08b5131c7ce45ab2d065c9953da8 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Wed, 11 Apr 2012 05:32:53 -0700 Subject: [PATCH] Add size_t, ptrdiff_t, and [u]intptr_t types. --- docs/ispc.rst | 13 +++++++++++-- module.cpp | 20 ++++++++++++++++++++ tests/intptr.ispc | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/intptr.ispc diff --git a/docs/ispc.rst b/docs/ispc.rst index ffc39490..7668e5e9 100644 --- a/docs/ispc.rst +++ b/docs/ispc.rst @@ -1394,8 +1394,8 @@ Types Basic Types and Type Qualifiers ------------------------------- -``ispc`` is a statically-typed language. It supports a variety of basic -types. +``ispc`` is a statically-typed language. It supports a variety of core +basic types: * ``void``: "empty" type representing no value. * ``bool``: boolean value; may be assigned ``true``, ``false``, or the @@ -1412,6 +1412,15 @@ types. * ``unsigned int64``: 64-bit unsigned integer. * ``double``: 64-bit double-precision floating point value. +There are also a few built-in types related to pointers and memory: + +* ``size_t``: the maximum size of any object (structure or array) +* ``ptrdiff_t``: an integer type large enough to represent the difference + between two pointers +* ``intptr_t``: signed integer type that is large enough to represent + a pointer value +* ``uintptr_t``: unsigned integer type large enough to represent a pointer + Implicit type conversion between values of different types is done automatically by the ``ispc`` compiler. Thus, a value of ``float`` type can be assigned to a variable of ``int`` type directly. In binary diff --git a/module.cpp b/module.cpp index 58782d0f..914a9bd2 100644 --- a/module.cpp +++ b/module.cpp @@ -88,6 +88,24 @@ #include #include +static void +lDeclareSizeAndPtrIntTypes(SymbolTable *symbolTable) { + const Type *ptrIntType = (g->target.is32Bit) ? AtomicType::VaryingInt32 : + AtomicType::VaryingInt64; + ptrIntType = ptrIntType->GetAsUnboundVariabilityType(); + + symbolTable->AddType("intptr_t", ptrIntType, SourcePos()); + symbolTable->AddType("uintptr_t", ptrIntType->GetAsUnsignedType(), + SourcePos()); + symbolTable->AddType("ptrdiff_t", ptrIntType, SourcePos()); + + const Type *sizeType = (g->target.is32Bit || g->opt.force32BitAddressing) ? + AtomicType::VaryingInt32 : AtomicType::VaryingInt64; + sizeType = sizeType->GetAsUnboundVariabilityType(); + symbolTable->AddType("size_t", sizeType, SourcePos()); +} + + /////////////////////////////////////////////////////////////////////////// // Module @@ -103,6 +121,8 @@ Module::Module(const char *fn) { symbolTable = new SymbolTable; ast = new AST; + lDeclareSizeAndPtrIntTypes(symbolTable); + module = new llvm::Module(filename ? filename : "", *g->ctx); module->setTargetTriple(g->target.GetTripleString()); diff --git a/tests/intptr.ispc b/tests/intptr.ispc new file mode 100644 index 00000000..7eb9eef7 --- /dev/null +++ b/tests/intptr.ispc @@ -0,0 +1,19 @@ + +export uniform int width() { return programCount; } + + +export void f_v(uniform float RET[]) { + RET[programIndex] = sizeof(uniform intptr_t); +} + +export void result(uniform float RET[]) { + RET[programIndex] = +#if (ISPC_POINTER_SIZE==32) + 4 +#elif (ISPC_POINTER_SIZE==64) + 8 +#else +#error Unknown pointer size +#endif + ; +}