Merge pull request #1014 from skyfex/devel

Packed pragma and tests for packed and union pragma
This commit is contained in:
Andreas Rumpf 2014-03-19 22:29:37 +01:00
commit a66d059acc
8 changed files with 96 additions and 13 deletions

View file

@ -450,6 +450,7 @@ type
tfHasMeta, # type contains "wildcard" sub-types such as generic params tfHasMeta, # type contains "wildcard" sub-types such as generic params
# or other type classes # or other type classes
tfHasGCedMem, # type contains GC'ed memory tfHasGCedMem, # type contains GC'ed memory
tfPacked
tfHasStatic tfHasStatic
tfGenericTypeParam tfGenericTypeParam
tfImplicitTypeParam tfImplicitTypeParam

View file

@ -464,24 +464,33 @@ proc getRecordDesc(m: BModule, typ: PType, name: PRope,
check: var TIntSet): PRope = check: var TIntSet): PRope =
# declare the record: # declare the record:
var hasField = false var hasField = false
let aStruct = structOrUnion(typ)
var attribute: PRope =
if tfPacked in typ.flags: toRope(CC[ccompiler].packedPragma)
else: nil
result = ropecg(m, CC[ccompiler].structStmtFmt,
[structOrUnion(typ), name, attribute])
if typ.kind == tyObject: if typ.kind == tyObject:
if typ.sons[0] == nil: if typ.sons[0] == nil:
if (typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags: if (typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags:
result = ropecg(m, "$1 $2 {$n", [aStruct, name]) appcg(m, result, " {$n", [])
else: else:
result = ropecg(m, "$1 $2 {$n#TNimType* m_type;$n", [aStruct, name]) appcg(m, result, " {$n#TNimType* m_type;$n", [name, attribute])
hasField = true hasField = true
elif gCmd == cmdCompileToCpp: elif gCmd == cmdCompileToCpp:
result = ropecg(m, "$1 $2 : public $3 {$n", appcg(m, result, " : public $1 {$n",
[aStruct, name, getTypeDescAux(m, typ.sons[0], check)]) [getTypeDescAux(m, typ.sons[0], check)])
hasField = true hasField = true
else: else:
result = ropecg(m, "$1 $2 {$n $3 Sup;$n", appcg(m, result, " {$n $1 Sup;$n",
[aStruct, name, getTypeDescAux(m, typ.sons[0], check)]) [getTypeDescAux(m, typ.sons[0], check)])
hasField = true hasField = true
else: else:
result = ropef("$1 $2 {$n", [aStruct, name]) appf(result, " {$n", [name])
var desc = getRecordFields(m, typ, check) var desc = getRecordFields(m, typ, check)
if (desc == nil) and not hasField: if (desc == nil) and not hasField:
appf(result, "char dummy;$n", []) appf(result, "char dummy;$n", [])

View file

@ -47,6 +47,8 @@ type
pic: string, # command for position independent code pic: string, # command for position independent code
# used on some platforms # used on some platforms
asmStmtFrmt: string, # format of ASM statement asmStmtFrmt: string, # format of ASM statement
structStmtFmt: string, # Format for struct statement
packedPragma: string, # Attribute/pragma to make struct packed (1-byte aligned)
props: TInfoCCProps] # properties of the C compiler props: TInfoCCProps] # properties of the C compiler
@ -77,6 +79,8 @@ compiler gcc:
debug: "", debug: "",
pic: "-fPIC", pic: "-fPIC",
asmStmtFrmt: "asm($1);$n", asmStmtFrmt: "asm($1);$n",
structStmtFmt: "$1 $3 $2 ", # struct|union [packed] $name
packedPragma: "__attribute__((__packed__))",
props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm, props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm,
hasNakedAttribute}) hasNakedAttribute})
@ -115,6 +119,8 @@ compiler vcc:
debug: " /GZ /Zi ", debug: " /GZ /Zi ",
pic: "", pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n", asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$3$n$1 $2",
packedPragma: "#pragma pack(1)",
props: {hasCpp, hasAssume, hasNakedDeclspec}) props: {hasCpp, hasAssume, hasNakedDeclspec})
compiler icl: compiler icl:
@ -148,6 +154,8 @@ compiler lcc:
debug: " -g5 ", debug: " -g5 ",
pic: "", pic: "",
asmStmtFrmt: "_asm{$n$1$n}$n", asmStmtFrmt: "_asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {}) props: {})
compiler bcc: compiler bcc:
@ -170,6 +178,8 @@ compiler bcc:
debug: "", debug: "",
pic: "", pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n", asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {hasCpp}) props: {hasCpp})
compiler dmc: compiler dmc:
@ -192,6 +202,8 @@ compiler dmc:
debug: " -g ", debug: " -g ",
pic: "", pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n", asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$3$n$1 $2",
packedPragma: "#pragma pack(1)",
props: {hasCpp}) props: {hasCpp})
compiler wcc: compiler wcc:
@ -214,6 +226,8 @@ compiler wcc:
debug: " -d2 ", debug: " -d2 ",
pic: "", pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n", asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {hasCpp}) props: {hasCpp})
compiler tcc: compiler tcc:
@ -236,6 +250,8 @@ compiler tcc:
debug: " -g ", debug: " -g ",
pic: "", pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n", asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {hasSwitchRange, hasComputedGoto}) props: {hasSwitchRange, hasComputedGoto})
compiler pcc: compiler pcc:
@ -259,6 +275,8 @@ compiler pcc:
debug: " -Zi ", debug: " -Zi ",
pic: "", pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n", asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {}) props: {})
compiler ucc: compiler ucc:
@ -281,6 +299,8 @@ compiler ucc:
debug: "", debug: "",
pic: "", pic: "",
asmStmtFrmt: "__asm{$n$1$n}$n", asmStmtFrmt: "__asm{$n$1$n}$n",
structStmtFmt: "$1 $2",
packedPragma: "", # XXX: not supported yet
props: {}) props: {})
const const

View file

@ -52,7 +52,7 @@ const
typePragmas* = {wImportc, wExportc, wDeprecated, wMagic, wAcyclic, wNodecl, typePragmas* = {wImportc, wExportc, wDeprecated, wMagic, wAcyclic, wNodecl,
wPure, wHeader, wCompilerproc, wFinal, wSize, wExtern, wShallow, wPure, wHeader, wCompilerproc, wFinal, wSize, wExtern, wShallow,
wImportCpp, wImportObjC, wError, wIncompleteStruct, wByCopy, wByRef, wImportCpp, wImportObjC, wError, wIncompleteStruct, wByCopy, wByRef,
wInheritable, wGensym, wInject, wRequiresInit, wUnchecked, wUnion} wInheritable, wGensym, wInject, wRequiresInit, wUnchecked, wUnion, wPacked}
fieldPragmas* = {wImportc, wExportc, wDeprecated, wExtern, fieldPragmas* = {wImportc, wExportc, wDeprecated, wExtern,
wImportCpp, wImportObjC, wError} wImportCpp, wImportObjC, wError}
varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl, varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl,
@ -655,6 +655,10 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
incl(sym.flags, sfThread) incl(sym.flags, sfThread)
incl(sym.flags, sfProcvar) incl(sym.flags, sfProcvar)
if sym.typ != nil: incl(sym.typ.flags, tfThread) if sym.typ != nil: incl(sym.typ.flags, tfThread)
of wPacked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfPacked)
of wHint: message(it.info, hintUser, expectStrLit(c, it)) of wHint: message(it.info, hintUser, expectStrLit(c, it))
of wWarning: message(it.info, warnUser, expectStrLit(c, it)) of wWarning: message(it.info, warnUser, expectStrLit(c, it))
of wError: of wError:

View file

@ -921,7 +921,7 @@ const tyTypeParamsHolders = {tyGenericInst, tyCompositeTypeClass}
proc readTypeParameter(c: PContext, typ: PType, proc readTypeParameter(c: PContext, typ: PType,
paramName: PIdent, info: TLineInfo): PNode = paramName: PIdent, info: TLineInfo): PNode =
let ty = if typ.kind == tyGenericInst: typ.skipGenericAlias let ty = if typ.kind == tyGenericInst: typ.skipGenericAlias
else: (internalAssert typ.kind == tyCompositeTypeClass; typ.sons[1]) else: (internalAssert(typ.kind == tyCompositeTypeClass); typ.sons[1])
let tbody = ty.sons[0] let tbody = ty.sons[0]
for s in countup(0, tbody.len-2): for s in countup(0, tbody.len-2):

View file

@ -204,7 +204,7 @@ proc nMinusOne(n: PNode): PNode =
n]) n])
proc makeRangeWithStaticExpr(c: PContext, n: PNode): PType = proc makeRangeWithStaticExpr(c: PContext, n: PNode): PType =
let intType = getSysType tyInt let intType = getSysType(tyInt)
result = newTypeS(tyRange, c) result = newTypeS(tyRange, c)
result.sons = @[intType] result.sons = @[intType]
result.n = newNode(nkRange, n.info, @[ result.n = newNode(nkRange, n.info, @[

View file

@ -72,7 +72,7 @@ type
wPrivate, wProtected, wPublic, wRegister, wReinterpret_cast, wPrivate, wProtected, wPublic, wRegister, wReinterpret_cast,
wShort, wSigned, wSizeof, wStatic_cast, wStruct, wSwitch, wShort, wSigned, wSizeof, wStatic_cast, wStruct, wSwitch,
wThis, wThrow, wTrue, wTypedef, wTypeid, wTypename, wThis, wThrow, wTrue, wTypedef, wTypeid, wTypename,
wUnion, wUnsigned, wVirtual, wVoid, wVolatile, wWchar_t, wUnion, wPacked, wUnsigned, wVirtual, wVoid, wVolatile, wWchar_t,
wAlignas, wAlignof, wConstexpr, wDecltype, wNullptr, wNoexcept, wAlignas, wAlignof, wConstexpr, wDecltype, wNullptr, wNoexcept,
wThread_local, wStatic_assert, wChar16_t, wChar32_t, wThread_local, wStatic_assert, wChar16_t, wChar32_t,
@ -155,7 +155,7 @@ const
"private", "protected", "public", "register", "reinterpret_cast", "private", "protected", "public", "register", "reinterpret_cast",
"short", "signed", "sizeof", "static_cast", "struct", "switch", "short", "signed", "sizeof", "static_cast", "struct", "switch",
"this", "throw", "true", "typedef", "typeid", "this", "throw", "true", "typedef", "typeid",
"typename", "union", "unsigned", "virtual", "void", "volatile", "typename", "union", "packed", "unsigned", "virtual", "void", "volatile",
"wchar_t", "wchar_t",
"alignas", "alignof", "constexpr", "decltype", "nullptr", "noexcept", "alignas", "alignof", "constexpr", "decltype", "nullptr", "noexcept",

View file

@ -0,0 +1,49 @@
discard """
file: "tobjpragma.nim"
output: '''2
3
9
257
1
2
3'''
"""
# Test
type
Foo {.packed.} = object
a: int8
b: int8
Bar {.packed.} = object
a: int8
b: int16
Daz {.packed.} = object
a: int32
b: int8
c: int32
var f = Foo(a: 1, b: 1)
var b: Bar
var d: Daz
echo sizeof(f)
echo sizeof(b)
echo sizeof(d)
echo (cast[ptr int16](f.addr)[])
type
Union {.union.} = object
a: int8
b: int8
var u: Union
u.a = 1
echo u.b
u.a = 2
echo u.b
u.b = 3
echo u.a