buildModule builds the output module definition.
()
| 27 | |
| 28 | // buildModule builds the output module definition. |
| 29 | func (c *C) buildModule() { |
| 30 | apiModuleTy := c.T.Struct("module_api", |
| 31 | codegen.Field{Name: "globals_offset", Type: c.T.Uint64}, |
| 32 | codegen.Field{Name: "globals_size", Type: c.T.Uint64}, |
| 33 | codegen.Field{Name: "num_cmds", Type: c.T.Uint32}, |
| 34 | codegen.Field{Name: "cmds", Type: c.T.VoidPtrPtr}, |
| 35 | ) |
| 36 | symbolTy := c.T.Struct("symbol", |
| 37 | codegen.Field{Name: "name", Type: c.T.Pointer(c.T.Uint8)}, |
| 38 | codegen.Field{Name: "addr", Type: c.T.VoidPtr}, |
| 39 | ) |
| 40 | moduleTy := c.T.Struct("module", |
| 41 | codegen.Field{Name: "create_context", Type: c.T.Pointer(c.ctx.create.Type)}, |
| 42 | codegen.Field{Name: "destroy_context", Type: c.T.Pointer(c.ctx.destroy.Type)}, |
| 43 | codegen.Field{Name: "globals_size", Type: c.T.Uint64}, |
| 44 | codegen.Field{Name: "num_apis", Type: c.T.Uint32}, |
| 45 | codegen.Field{Name: "apis", Type: c.T.Pointer(apiModuleTy)}, |
| 46 | codegen.Field{Name: "num_symbols", Type: c.T.Uint32}, |
| 47 | codegen.Field{Name: "symbols", Type: c.T.Pointer(symbolTy)}, |
| 48 | ) |
| 49 | |
| 50 | apiMaxIndex := 0 |
| 51 | for _, api := range c.APIs { |
| 52 | if apiMaxIndex < int(api.Index) { |
| 53 | apiMaxIndex = int(api.Index) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | apis := make([]codegen.Const, apiMaxIndex+1) |
| 58 | for i := range apis { |
| 59 | apis[i] = c.M.Zero(apiModuleTy) |
| 60 | } |
| 61 | |
| 62 | for _, api := range c.APIs { |
| 63 | commands := make([]*codegen.Function, len(api.Functions)) |
| 64 | |
| 65 | for i, f := range api.Functions { |
| 66 | commands[i] = c.commands[f] |
| 67 | } |
| 68 | |
| 69 | cmdTbl := c.M. |
| 70 | Global(api.Name()+"-cmd-tbl", c.M.Array(commands, c.T.VoidPtr)). |
| 71 | SetConstant(true). |
| 72 | Cast(c.T.VoidPtr) |
| 73 | |
| 74 | apis[api.Index] = c.M.ConstStruct( |
| 75 | apiModuleTy, map[string]interface{}{ |
| 76 | "globals_offset": c.M.OffsetOf(c.T.Globals, api.Name()), |
| 77 | "globals_size": c.M.SizeOf(c.T.Globals.Field(api.Name()).Type), |
| 78 | "num_cmds": len(commands), |
| 79 | "cmds": cmdTbl.Cast(c.T.VoidPtrPtr), |
| 80 | }, |
| 81 | ) |
| 82 | } |
| 83 | |
| 84 | apiTbl := c.M. |
| 85 | Global("api-tbl", c.M.Array(apis, apiModuleTy)). |
| 86 | SetConstant(true) |
no test coverage detected