@ -20,21 +20,33 @@ function first_of_type(node,labelname)
end
end
end
end
end
end
function padAddress ( curoff , sizetoadd ) --return new offset to place things
xtypes = { } -- list of all types prototypes (e.g. enum-type -> announcement_type)
-- type must have size, new and makewrap (that makes a wrapper around ptr)
if WINDOWS then
dofile ( " dfusion/xml_types_windows.lua " )
elseif LINUX then
dofile ( " dfusion/xml_types_linux.lua " )
end
function padAddress ( curoff , sizetoadd ) --return new offset to place things... Maybe linux is different?
--windows -> sizeof(x)==alignof(x)
--windows -> sizeof(x)==alignof(x)
--[=[
if sizetoadd > 8 then sizetoadd = 8 end
if sizetoadd > 8 then sizetoadd = 8 end
if ( math.mod ( curoff , sizetoadd ) == 0 ) then
if ( math.mod ( curoff , sizetoadd ) == 0 ) then
return curoff
return curoff
else
else
return curoff + ( sizetoadd - math.mod ( curoff , sizetoadd ) )
return curoff + ( sizetoadd - math.mod ( curoff , sizetoadd ) )
end
end
--]=]
return curoff
end
end
xtypes = { } -- list of all types prototypes (e.g. enum-type -> announcement_type)
-- type must have new and makewrap (that makes a wrapper around ptr)
local sarr = { }
local sarr = { }
sarr.__index = sarr
sarr.__index = sarr
function sarr . new ( node )
function sarr . new ( node ,obj )
local o = { }
local o = obj or { }
setmetatable ( o , sarr )
setmetatable ( o , sarr )
--print("Making array.")
--print("Making array.")
o.count = tonumber ( node.xarg . count )
o.count = tonumber ( node.xarg . count )
@ -89,9 +101,9 @@ simpletypes.int16_t={WORD,2}
simpletypes.int8_t = { BYTE , 1 }
simpletypes.int8_t = { BYTE , 1 }
simpletypes.bool = { BYTE , 1 }
simpletypes.bool = { BYTE , 1 }
simpletypes [ " stl-string " ] = { STD_STRING , 24 }
simpletypes [ " stl-string " ] = { STD_STRING , 24 }
function getSimpleType ( typename )
function getSimpleType ( typename ,obj )
if simpletypes [ typename ] == nil then return end
if simpletypes [ typename ] == nil then return end
local o = { }
local o = obj or { }
o.ctype = simpletypes [ typename ] [ 1 ]
o.ctype = simpletypes [ typename ] [ 1 ]
o.size = simpletypes [ typename ] [ 2 ]
o.size = simpletypes [ typename ] [ 2 ]
o.issimple = true
o.issimple = true
@ -99,8 +111,8 @@ function getSimpleType(typename)
end
end
local type_enum = { }
local type_enum = { }
type_enum.__index = type_enum
type_enum.__index = type_enum
function type_enum . new ( node )
function type_enum . new ( node ,obj )
local o = { }
local o = obj or { }
setmetatable ( o , type_enum )
setmetatable ( o , type_enum )
for k , v in pairs ( node ) do
for k , v in pairs ( node ) do
if type ( v ) == " table " and v.xarg ~= nil then
if type ( v ) == " table " and v.xarg ~= nil then
@ -118,11 +130,12 @@ xtypes["enum-type"]=type_enum
local type_bitfield = { } --bitfield can be accessed by number (bf[1]=true) or by name bf.DO_MEGA=true
local type_bitfield = { } --bitfield can be accessed by number (bf[1]=true) or by name bf.DO_MEGA=true
type_bitfield.__index = type_bitfield
type_bitfield.__index = type_bitfield
function type_bitfield . new ( node )
function type_bitfield . new ( node ,obj )
local o = { }
local o = obj or { }
setmetatable ( o , type_bitfield )
setmetatable ( o , type_bitfield )
o.size = 0
o.size = 0
o.fields = { }
o.fields_named = { }
o.fields_numed = { }
for k , v in pairs ( node ) do
for k , v in pairs ( node ) do
if type ( v ) == " table " and v.xarg ~= nil then
if type ( v ) == " table " and v.xarg ~= nil then
@ -131,8 +144,8 @@ function type_bitfield.new(node)
name = " anon_ " .. tostring ( k )
name = " anon_ " .. tostring ( k )
end
end
--print("\t"..k.." "..name)
--print("\t"..k.." "..name)
o.fields [ k ] = name
o.fields _numed [ k ] = name
o.fields [ name ] = k
o.fields _named [ name ] = k
o.size = o.size + 1
o.size = o.size + 1
end
end
end
end
@ -163,18 +176,27 @@ function type_bitfield:makewrap(address)
setmetatable ( o , self.wrap )
setmetatable ( o , self.wrap )
return o
return o
end
end
function type_bitfield . wrap . __next ( tbl , key )
local kk , vv = next ( rawget ( tbl , " mtype " ) . fields_named , key )
if kk ~= nil then
return kk , tbl [ kk ]
else
return nil
end
end
function type_bitfield . wrap : __index ( key )
function type_bitfield . wrap : __index ( key )
local myptr = rawget ( self , " ptr " )
local myptr = rawget ( self , " ptr " )
local mytype = rawget ( self , " mtype " )
local mytype = rawget ( self , " mtype " )
local num = tonumber ( key )
local num = tonumber ( key )
if num ~= nil then
if num ~= nil then
if mytype.fields [ num ] ~= nil then
if mytype.fields _numed [ num ] ~= nil then
return mytype : bitread ( myptr , num - 1 )
return mytype : bitread ( myptr , num - 1 )
else
else
error ( " No bit with index: " .. tostring ( num ) )
error ( " No bit with index: " .. tostring ( num ) )
end
end
elseif mytype.fields [ key ] ~= nil then
elseif mytype.fields _named [ key ] ~= nil then
return mytype : bitread ( myptr , mytype.fields [ key ] - 1 )
return mytype : bitread ( myptr , mytype.fields _named [ key ] - 1 )
else
else
error ( " No such field exists " )
error ( " No such field exists " )
end
end
@ -184,13 +206,13 @@ function type_bitfield.wrap:__newindex(key,value)
local mytype = rawget ( self , " mtype " )
local mytype = rawget ( self , " mtype " )
local num = tonumber ( key )
local num = tonumber ( key )
if num ~= nil then
if num ~= nil then
if mytype.fields [ num ] ~= nil then
if mytype.fields _numed [ num ] ~= nil then
return mytype : bitwrite ( myptr , num - 1 , value )
return mytype : bitwrite ( myptr , num - 1 , value )
else
else
error ( " No bit with index: " .. tostring ( num ) )
error ( " No bit with index: " .. tostring ( num ) )
end
end
elseif mytype.fields [ key ] ~= nil then
elseif mytype.fields _named [ key ] ~= nil then
return mytype : bitwrite ( myptr , mytype.fields [ key ] - 1 , value )
return mytype : bitwrite ( myptr , mytype.fields _named [ key ] - 1 , value )
else
else
error ( " No such field exists " )
error ( " No such field exists " )
end
end
@ -200,8 +222,8 @@ xtypes["bitfield-type"]=type_bitfield
local type_class = { }
local type_class = { }
type_class.__index = type_class
type_class.__index = type_class
function type_class . new ( node )
function type_class . new ( node ,obj )
local o = { }
local o = obj or { }
setmetatable ( o , type_class )
setmetatable ( o , type_class )
o.types = { }
o.types = { }
o.base = { }
o.base = { }
@ -224,11 +246,12 @@ function type_class.new(node)
--print("\t"..k.." "..name.."->"..v.xarg.meta.." ttype:"..v.label)
--print("\t"..k.." "..name.."->"..v.xarg.meta.." ttype:"..v.label)
local ttype = makeType ( v )
local ttype = makeType ( v )
if ttype.size == 0 then error ( " Field with 0 size! type= " .. node.xarg [ " type-name " ] .. " . " .. name ) end
--for k,v in pairs(ttype) do
--for k,v in pairs(ttype) do
-- print(k..tostring(v))
-- print(k..tostring(v))
--end
--end
local off = padAddress ( o.size , ttype.size )
local off = padAddress ( o.size , ttype.size )
--print("var:"..name.." ->"..tostring(off).. " :"..ttype.size)
o.size = off
o.size = off
o.types [ name ] = { ttype , o.size }
o.types [ name ] = { ttype , o.size }
o.size = o.size + ttype.size
o.size = o.size + ttype.size
@ -267,26 +290,31 @@ xtypes["struct-type"]=type_class
xtypes [ " class-type " ] = type_class
xtypes [ " class-type " ] = type_class
local type_pointer = { }
local type_pointer = { }
type_pointer.__index = type_pointer
type_pointer.__index = type_pointer
function type_pointer . new ( node )
function type_pointer . new ( node ,obj )
local o = { }
local o = obj or { }
setmetatable ( o , type_pointer )
setmetatable ( o , type_pointer )
o.ptype = makeType ( first_of_type ( node , " ld:item " ) )
local subnode = first_of_type ( node , " ld:item " )
if subnode ~= nil then
o.ptype = makeType ( subnode , nil , true )
end
o.size = 4
o.size = 4
return o
return o
end
end
type_pointer.wrap = { }
type_pointer.wrap = { }
type_pointer.wrap . __index = type_pointer.wrap
function type_pointer . wrap : tonumber ( )
function type_pointer . wrap : tonumber ( )
local myptr = rawget ( self , " ptr " )
local myptr = rawget ( self , " ptr " )
return type_read( myptr , DWORD )
return engine.peekd( myptr ) --type_read(DWORD,myptr )
end
end
function type_pointer . wrap : fromnumber ( num )
function type_pointer . wrap : fromnumber ( num )
local myptr = rawget ( self , " ptr " )
local myptr = rawget ( self , " ptr " )
return type_write( myptr , DWORD , num )
return engine.poked( myptr , num ) --type_write(DWORD,myptr,num )
end
end
function type_pointer . wrap : deref ( )
function type_pointer . wrap : deref ( )
local myptr = rawget ( self , " ptr " )
local myptr = rawget ( self , " ptr " )
local mytype = rawget ( self , " p type" )
local mytype = rawget ( self , " m type" )
return type_read ( my ptr, mytype )
return type_read ( my type.ptype, engine.peekd ( myptr ) )
end
end
function type_pointer : makewrap ( ptr )
function type_pointer : makewrap ( ptr )
local o = { }
local o = { }
@ -295,15 +323,15 @@ function type_pointer:makewrap(ptr)
setmetatable ( o , self.wrap )
setmetatable ( o , self.wrap )
return o
return o
end
end
xtypes [ " pointer " ] = type_ class
xtypes [ " pointer " ] = type_ pointer
--------------------------------------------
--------------------------------------------
--stl-vector (beginptr,endptr,allocptr)
--stl-vector (beginptr,endptr,allocptr)
--df-flagarray (ptr,size)
--df-flagarray (ptr,size)
xtypes.containers = { }
xtypes.containers = xtypes.containers or { }
local dfarr = { }
local dfarr = { }
dfarr.__index = dfarr
dfarr.__index = dfarr
function dfarr . new ( node )
function dfarr . new ( node ,obj )
local o = { }
local o = obj or { }
setmetatable ( o , dfarr )
setmetatable ( o , dfarr )
o.size = 8
o.size = 8
return o
return o
@ -340,8 +368,8 @@ end
xtypes.containers [ " df-array " ] = dfarr
xtypes.containers [ " df-array " ] = dfarr
local farr = { }
local farr = { }
farr.__index = farr
farr.__index = farr
function farr . new ( node )
function farr . new ( node ,obj )
local o = { }
local o = obj or { }
setmetatable ( o , farr )
setmetatable ( o , farr )
o.size = 8
o.size = 8
return o
return o
@ -381,139 +409,60 @@ end
xtypes.containers [ " df-flagarray " ] = farr
xtypes.containers [ " df-flagarray " ] = farr
local stl_vec = { }
stl_vec.__index = stl_vec
function stl_vec . new ( node )
local o = { }
o.size = 16
local titem = first_of_type ( node , " ld:item " )
if titem ~= nil then
o.item_type = makeType ( titem )
else
o.item_type = getSimpleType ( " uint32_t " )
end
setmetatable ( o , stl_vec )
return o
end
function stl_vec : makewrap ( address )
local o = { }
o.mtype = self
o.ptr = address
setmetatable ( o , self.wrap )
return o
end
stl_vec.wrap = { }
function stl_vec . wrap : __index ( key )
local num = tonumber ( key )
local mtype = rawget ( self , " mtype " )
local ptr = rawget ( self , " ptr " )
local p_begin = engine.peek ( ptr , DWORD )
local p_end = engine.peek ( ptr + 4 , DWORD )
if key == " size " then
return ( p_end - p_begin ) / mtype.item_type . size ;
end
--allocend=type_read(ptr+8,DWORD)
error ( " TODO make __index for stl_vec " )
if num ~= nil and num < sizethen then
return type_read ( mtype.ctype , num * mtype.ctype . size + rawget ( self , " ptr " ) )
else
error ( " invalid key to df-flagarray " )
end
end
function stl_vec . wrap : __newindex ( key , val )
local num = tonumber ( key )
error ( " TODO make __index for stl_vec " )
if num ~= nil and num < rawget ( self , " mtype " ) . count then
return type_write ( mtype.ctype , num * mtype.ctype . size + rawget ( self , " ptr " ) , val )
else
error ( " invalid key to static-array " )
end
end
xtypes.containers [ " stl-vector " ] = stl_vec
local stl_vec_bit = { }
stl_vec_bit.__index = stl_vec_bit
function stl_vec_bit . new ( node )
local o = { }
setmetatable ( o , stl_vec_bit )
o.size = 20
return o
end
function stl_vec_bit : makewrap ( address )
local o = { }
o.mtype = self
o.ptr = address
setmetatable ( o , self.wrap )
return o
end
stl_vec_bit.wrap = { }
function stl_vec_bit . wrap : __index ( key )
local num = tonumber ( key )
local mtype = rawget ( self , " item_type " )
local ptr = rawget ( self , " ptr " )
local p_begin = type_read ( ptr , DWORD )
local p_end = type_read ( ptr + 4 , DWORD )
--allocend=type_read(ptr+8,DWORD)
error ( " TODO make __index for stl_vec_bit " )
if num ~= nil and num < sizethen then
return type_read ( mtype.ctype , num * mtype.ctype . size + rawget ( self , " ptr " ) )
else
error ( " invalid key to df-flagarray " )
end
end
function stl_vec_bit . wrap : __newindex ( key , val )
local num = tonumber ( key )
error ( " TODO make __index for stl_vec_bit " )
if num ~= nil and num < rawget ( self , " mtype " ) . count then
return type_write ( mtype.ctype , num * mtype.ctype . size + rawget ( self , " ptr " ) , val )
else
error ( " invalid key to static-array " )
end
end
xtypes.containers [ " stl-bit-vector " ] = stl_vec_bit
--------------------------------------------
--------------------------------------------
local bytes_pad = { }
local bytes_pad = { }
bytes_pad.__index = bytes_pad
bytes_pad.__index = bytes_pad
function bytes_pad . new ( node )
function bytes_pad . new ( node , obj )
local o = { }
local o = obj or { }
setmetatable ( o , bytes_pad )
setmetatable ( o , bytes_pad )
o.size = tonumber ( node.xarg . size )
o.size = tonumber ( node.xarg . size )
return o
return o
end
end
xtypes [ " bytes " ] = bytes_pad
xtypes [ " bytes " ] = bytes_pad
--------------------------------------------
--------------------------------------------
function getGlobal ( name )
function getGlobal ( name , canDelay )
if types [ name ] == nil then
if types [ name ] == nil then
if canDelay then
types [ name ] = { }
return types [ name ]
end
findAndParse ( name )
findAndParse ( name )
if types [ name ] == nil then
if types [ name ] == nil then
error ( " type: " .. name .. " failed find-and-parse " )
error ( " type: " .. name .. " failed find-and-parse " )
end
end
--error("type:"..node.xarg["type-name"].." should already be ready")
--error("type:"..node.xarg["type-name"].." should already be ready")
end
end
if types [ name ] . size == nil and canDelay == nil then --was delayed, now need real type
findAndParse ( name )
if types [ name ] == nil then
error ( " type: " .. name .. " failed find-and-parse " )
end
end
return types [ name ]
return types [ name ]
end
end
parser = { }
parser = { }
parser [ " ld:global-type " ] = function ( node )
parser [ " ld:global-type " ] = function ( node , obj )
return xtypes [ node.xarg . meta ] . new ( node )
return xtypes [ node.xarg . meta ] . new ( node , obj )
end
end
parser [ " ld:global-object " ] = function ( node )
parser [ " ld:global-object " ] = function ( node )
end
end
parser [ " ld:field " ] = function ( node )
parser [ " ld:field " ] = function ( node , obj , canDelay )
local meta = node.xarg . meta
local meta = node.xarg . meta
if meta == " number " or ( meta == " primitive " and node.xarg . subtype == " stl-string " ) then
if meta == " number " or ( meta == " primitive " and node.xarg . subtype == " stl-string " ) then
return getSimpleType ( node.xarg . subtype )
return getSimpleType ( node.xarg . subtype , obj )
elseif meta == " static-array " then
elseif meta == " static-array " then
return xtypes [ " static-array " ] . new ( node )
return xtypes [ " static-array " ] . new ( node , obj )
elseif meta == " global " then
elseif meta == " global " then
return getGlobal ( node.xarg [ " type-name " ] )
return getGlobal ( node.xarg [ " type-name " ] , canDelay )
elseif meta == " compound " then
elseif meta == " compound " then
if node.xarg . subtype == nil then
if node.xarg . subtype == nil then
return xtypes [ " struct-type " ] . new ( node )
return xtypes [ " struct-type " ] . new ( node ,obj )
else
else
return xtypes [ node.xarg . subtype .. " -type " ] . new ( node )
return xtypes [ node.xarg . subtype .. " -type " ] . new ( node ,obj )
end
end
elseif meta == " container " then
elseif meta == " container " then
local subtype = node.xarg . subtype
local subtype = node.xarg . subtype
@ -521,22 +470,22 @@ parser["ld:field"]=function (node)
if xtypes.containers [ subtype ] == nil then
if xtypes.containers [ subtype ] == nil then
error ( subtype .. " not implemented... (container) " )
error ( subtype .. " not implemented... (container) " )
else
else
return xtypes.containers [ subtype ] . new ( node )
return xtypes.containers [ subtype ] . new ( node ,obj )
end
end
elseif meta == " pointer " then
elseif meta == " pointer " then
return xtypes [ " pointer " ] . new ( node )
return xtypes [ " pointer " ] . new ( node ,obj )
elseif meta == " bytes " then
elseif meta == " bytes " then
return xtypes [ " bytes " ] . new ( node )
return xtypes [ " bytes " ] . new ( node ,obj )
else
else
error ( " Unknown meta: " .. meta )
error ( " Unknown meta: " .. meta )
end
end
end
end
parser [ " ld:item " ] = parser [ " ld:field " ]
parser [ " ld:item " ] = parser [ " ld:field " ]
function makeType ( node , o verwrite )
function makeType ( node , o bj, canDelay )
local label = overwrite or node.label
local label = node.label
if parser [ label ] ~= nil then
if parser [ label ] ~= nil then
--print("Make Type with:"..label)
--print("Make Type with:"..label)
local ret = parser [ label ] ( node )
local ret = parser [ label ] ( node ,obj , canDelay )
if ret == nil then
if ret == nil then
error ( " Error parsing: " .. label .. " nil returned! " )
error ( " Error parsing: " .. label .. " nil returned! " )
else
else
@ -554,7 +503,7 @@ function makeType(node,overwrite)
end
end
print ( " Trying to make: " .. node.xarg . meta )
print ( " Trying to make: " .. node.xarg . meta )
if xtypes [ node.xarg . meta ] ~= nil then
if xtypes [ node.xarg . meta ] ~= nil then
return xtypes [ node.xarg . meta ] . new ( node )
return xtypes [ node.xarg . meta ] . new ( node ,obj )
end
end
if node.xarg . meta == " global " then
if node.xarg . meta == " global " then