1//
2// m3_module.c
3//
4// Created by Steven Massey on 5/7/19.
5// Copyright © 2019 Steven Massey. All rights reserved.
6//
7
8#include "m3_env.h"
9#include "m3_exception.h"
10
11
12void Module_FreeFunctions (IM3Module i_module)
13{
14 for (u32 i = 0; i < i_module->numFunctions; ++i)
15 {
16 IM3Function func = & i_module->functions [i];
17 Function_Release (func);
18 }
19}
20
21
22void m3_FreeModule (IM3Module i_module)
23{
24 if (i_module)
25 {
26 m3log (module, "freeing module: %s (funcs: %d; segments: %d)",
27 i_module->name, i_module->numFunctions, i_module->numDataSegments);
28
29 Module_FreeFunctions (i_module);
30
31 m3_Free (i_module->functions);
32 //m3_Free (i_module->imports);
33 m3_Free (i_module->funcTypes);
34 m3_Free (i_module->dataSegments);
35 m3_Free (i_module->table0);
36
37 for (u32 i = 0; i < i_module->numGlobals; ++i)
38 {
39 m3_Free (i_module->globals[i].name);
40 FreeImportInfo(&(i_module->globals[i].import));
41 }
42 m3_Free (i_module->globals);
43
44 m3_Free (i_module);
45 }
46}
47
48
49M3Result Module_AddGlobal (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported)
50{
51_try {
52 u32 index = io_module->numGlobals++;
53 io_module->globals = m3_ReallocArray (M3Global, io_module->globals, io_module->numGlobals, index);
54 _throwifnull (io_module->globals);
55 M3Global * global = & io_module->globals [index];
56
57 global->type = i_type;
58 global->imported = i_isImported;
59 global->isMutable = i_mutable;
60
61 if (o_global)
62 * o_global = global;
63
64} _catch:
65 return result;
66}
67
68M3Result Module_PreallocFunctions (IM3Module io_module, u32 i_totalFunctions)
69{
70_try {
71 if (i_totalFunctions > io_module->allFunctions) {
72 io_module->functions = m3_ReallocArray (M3Function, io_module->functions, i_totalFunctions, io_module->allFunctions);
73 io_module->allFunctions = i_totalFunctions;
74 _throwifnull (io_module->functions);
75 }
76} _catch:
77 return result;
78}
79
80M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo)
81{
82_try {
83
84 u32 index = io_module->numFunctions++;
85_ (Module_PreallocFunctions(io_module, io_module->numFunctions));
86
87 _throwif ("type sig index out of bounds", i_typeIndex >= io_module->numFuncTypes);
88
89 IM3FuncType ft = io_module->funcTypes [i_typeIndex];
90
91 IM3Function func = Module_GetFunction (io_module, index);
92 func->funcType = ft;
93
94# ifdef DEBUG
95 func->index = index;
96# endif
97
98 if (i_importInfo and func->numNames == 0)
99 {
100 func->import = * i_importInfo;
101 func->names[0] = i_importInfo->fieldUtf8;
102 func->numNames = 1;
103 }
104
105 m3log (module, " added function: %3d; sig: %d", index, i_typeIndex);
106
107} _catch:
108 return result;
109}
110
111#ifdef DEBUG
112void Module_GenerateNames (IM3Module i_module)
113{
114 for (u32 i = 0; i < i_module->numFunctions; ++i)
115 {
116 IM3Function func = & i_module->functions [i];
117
118 if (func->numNames == 0)
119 {
120 char* buff = m3_AllocArray(char, 16);
121 snprintf(buff, 16, "$func%d", i);
122 func->names[0] = buff;
123 func->numNames = 1;
124 }
125 }
126 for (u32 i = 0; i < i_module->numGlobals; ++i)
127 {
128 IM3Global global = & i_module->globals [i];
129
130 if (global->name == NULL)
131 {
132 char* buff = m3_AllocArray(char, 16);
133 snprintf(buff, 16, "$global%d", i);
134 global->name = buff;
135 }
136 }
137}
138#endif
139
140IM3Function Module_GetFunction (IM3Module i_module, u32 i_functionIndex)
141{
142 IM3Function func = NULL;
143
144 if (i_functionIndex < i_module->numFunctions)
145 {
146 func = & i_module->functions [i_functionIndex];
147 //func->module = i_module;
148 }
149
150 return func;
151}
152
153
154const char* m3_GetModuleName (IM3Module i_module)
155{
156 if (!i_module || !i_module->name)
157 return ".unnamed";
158
159 return i_module->name;
160}
161
162void m3_SetModuleName (IM3Module i_module, const char* name)
163{
164 if (i_module) i_module->name = name;
165}
166
167IM3Runtime m3_GetModuleRuntime (IM3Module i_module)
168{
169 return i_module ? i_module->runtime : NULL;
170}
171
172