Skip to content

Commit 06ed034

Browse files
committed
Add module suppor to cnex
1 parent 13cd2ad commit 06ed034

18 files changed

+563
-218
lines changed

exec/cnex/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_executable(cnex
1818
dictionary.c
1919
framestack.c
2020
global.c
21+
module.c
2122
nstring.c
2223
number.c
2324
object.c

exec/cnex/SConstruct

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ cnex = envcnex.Program("cnex", [
4444
"dictionary.c",
4545
"framestack.c",
4646
"global.c",
47+
"module.c",
4748
"nstring.c",
4849
"number.c",
4950
"object.c",

exec/cnex/bytecode.c

+75-64
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,23 @@ void bytecode_freeBytecode(TBytecode *b)
7070
return;
7171
}
7272

73+
free(b->variables);
7374
free(b->functions);
7475
free(b->imports);
7576
free(b->export_functions);
7677
free(b->exceptions);
7778
free(b->export_types);
79+
free(b->export_exceptions);
80+
81+
for (i = 0; i < b->constantsize; i++) {
82+
free(b->export_constants[i].value);
83+
}
84+
free(b->export_constants);
85+
for (i = 0; i < b->interfaceexportsize; i++) {
86+
free(b->export_interfaces[i].method_descriptors);
87+
}
88+
free(b->export_interfaces);
89+
7890
for (i = 0; i < b->strtablelen; i++) {
7991
string_freeString(b->strings[i]);
8092
}
@@ -104,53 +116,46 @@ void bytecode_loadBytecode(TBytecode *b, const uint8_t *bytecode, unsigned int l
104116
b->strings = getstrtable(bytecode, b->strtablesize + i, &i, &b->strtablelen);
105117

106118
b->typesize = get_vint(bytecode, len, &i);
107-
/*
108-
typesize = struct.unpack(">H", bytecode[i:i+2])[0]
109-
i += 2
110-
self.export_types = []
111-
while typesize > 0:
112-
t = Type()
113-
t.name = struct.unpack(">H", bytecode[i:i+2])[0]
114-
i += 2
115-
t.descriptor = struct.unpack(">H", bytecode[i:i+2])[0]
116-
i += 2
117-
self.export_types.append(t)
118-
typesize -= 1
119-
*/
119+
b->export_types = malloc(sizeof(ExportType) * b->typesize);
120+
if (b->export_types == NULL) {
121+
fatal_error("Could not allocate memory for exported type info.");
122+
}
123+
for (uint32_t f = 0; f < b->typesize; f++) {
124+
b->export_types[f].name = get_vint(bytecode, len, &i);
125+
b->export_types[f].descriptor = get_vint(bytecode, len, &i);
126+
}
127+
120128
b->constantsize = get_vint(bytecode, len, &i);
121-
/*
122-
constantsize = struct.unpack(">H", bytecode[i:i+2])[0]
123-
i += 2
124-
self.export_constants = []
125-
while constantsize > 0:
126-
c = Constant()
127-
c.name = struct.unpack(">H", bytecode[i:i+2])[0]
128-
i += 2
129-
c.type = struct.unpack(">H", bytecode[i:i+2])[0]
130-
i += 2
131-
size = struct.unpack(">H", bytecode[i:i+2])[0]
132-
i += 2
133-
c.value = bytecode[i:i+size]
134-
i += size
135-
self.export_constants.append(c)
136-
constantsize -= 1;
137-
*/
129+
b->export_constants = malloc(sizeof(Constant) * b->constantsize);
130+
if (b->export_constants == NULL) {
131+
fatal_error("Could not allocate memory for constants.");
132+
}
133+
for (uint32_t c = 0; c < b->constantsize; c++) {
134+
b->export_constants[c].name = get_vint(bytecode, len, &i);
135+
b->export_constants[c].vtype = get_vint(bytecode, len, &i);
136+
unsigned int datasize = get_vint(bytecode, len, &i);
137+
if (i+datasize > len) {
138+
fatal_error("Invalid constant data size.");
139+
}
140+
b->export_constants[c].value = malloc(sizeof(unsigned char) * datasize);
141+
if (b->export_constants[c].value == NULL) {
142+
fatal_error("Could not allocate memory for value of constant \"%s\".", b->strings[b->export_constants[c].name]->data);
143+
}
144+
memcpy(b->export_constants[c].value, &bytecode[i], datasize);
145+
i += datasize;
146+
}
147+
138148
b->variablesize = get_vint(bytecode, len, &i);
139-
/*
140-
variablesize = struct.unpack(">H", bytecode[i:i+2])[0]
141-
i += 2
142-
self.export_variables = []
143-
while variablesize > 0:
144-
v = Variable()
145-
v.name = struct.unpack(">H", bytecode[i:i+2])[0]
146-
i += 2
147-
v.type = struct.unpack(">H", bytecode[i:i+2])[0]
148-
i += 2
149-
v.index = struct.unpack(">H", bytecode[i:i+2])[0]
150-
i += 2
151-
self.export_variables.append(v)
152-
variablesize -= 1
153-
*/
149+
b->variables = malloc(sizeof(Variable) * b->variablesize);
150+
if (b->variables == NULL) {
151+
fatal_error("Could not allocate memory %d for variables.", b->variablesize);
152+
}
153+
for (uint32_t v = 0; v < b->variablesize; v++) {
154+
b->variables[v].name = get_vint(bytecode, len, &i);
155+
b->variables[v].type = get_vint(bytecode, len, &i);
156+
b->variables[v].index = get_vint(bytecode, len, &i);
157+
}
158+
154159
b->export_functionsize = get_vint(bytecode, len, &i);
155160
b->export_functions = malloc(sizeof(ExportFunction) * b->export_functionsize);
156161
if (b->export_functions == NULL) {
@@ -161,29 +166,35 @@ void bytecode_loadBytecode(TBytecode *b, const uint8_t *bytecode, unsigned int l
161166
b->export_functions[f].descriptor = get_vint(bytecode, len, &i);
162167
b->export_functions[f].index = get_vint(bytecode, len, &i);
163168
}
169+
164170
b->exceptionexportsize = get_vint(bytecode, len, &i);
165-
/*
166-
exceptionexportsize = struct.unpack(">H", bytecode[i:i+2])[0]
167-
i += 2
168-
self.export_exceptions = []
169-
while exceptionexportsize > 0:
170-
e = ExceptionExport()
171-
e.name = struct.unpack(">H", bytecode[i:i+2])[0]
172-
i += 2
173-
self.export_exceptions.append(e)
174-
exceptionexportsize -= 1
175-
*/
171+
b->export_exceptions = malloc(sizeof(ExportException) * b->exceptionexportsize);
172+
if (b->export_exceptions == NULL) {
173+
fatal_error("Could not allocate memory for exported exceptions.");
174+
}
175+
for (uint32_t e = 0; e < b->exceptionexportsize; e++) {
176+
b->export_exceptions[e].name = get_vint(bytecode, len, &i);
177+
}
178+
176179
b->interfaceexportsize = get_vint(bytecode, len, &i);
177-
/*
178-
interfaceexportsize = struct.unpack(">H", bytecode[i:i+2])[0]
179-
i += 2
180-
while interfaceexportsize > 0:
181-
assert False, interfaceexportsize
182-
*/
180+
b->export_interfaces = malloc(sizeof(ExportInterface) * b->interfaceexportsize);
181+
for (uint32_t e = 0; e < b->interfaceexportsize; e++) {
182+
b->export_interfaces[e].name = get_vint(bytecode, len, &i);
183+
unsigned int methoddescriptorsize = get_vint(bytecode, len, &i);
184+
b->export_interfaces[e].method_descriptors = malloc(sizeof(MethodDescriptor) * methoddescriptorsize);
185+
if (b->export_interfaces[e].method_descriptors == NULL) {
186+
fatal_error("Could not allocate memory for method descriptors for exported interface: %s", b->strings[b->export_interfaces[e].name]->data);
187+
}
188+
for (uint32_t m = 0; m < methoddescriptorsize; m++) {
189+
b->export_interfaces[e].method_descriptors[m].first = get_vint(bytecode, len, &i);
190+
b->export_interfaces[e].method_descriptors[m].second = get_vint(bytecode, len, &i);
191+
}
192+
}
193+
183194
b->importsize = get_vint(bytecode, len, &i);
184195
b->imports = malloc(sizeof(Import) * b->importsize);
185196
if (b->imports == NULL) {
186-
fatal_error("Could not allocate memory for exported function info.");
197+
fatal_error("Could not allocate memory for imported module info.");
187198
}
188199
for (uint32_t f = 0; f < b->importsize; f++) {
189200
b->imports[f].name = get_vint(bytecode, len, &i);
@@ -227,7 +238,7 @@ void bytecode_loadBytecode(TBytecode *b, const uint8_t *bytecode, unsigned int l
227238
b->classes[c].interfacesize = get_vint(bytecode, len, &i);
228239
b->classes[c].interfaces = malloc(sizeof(Interface) * b->classes[c].interfacesize);
229240
if (b->classes[c].interfaces == NULL) {
230-
fatal_error("Could not allocate memory for (%d) interfaces.", b->classes[c].interfacesize);
241+
fatal_error("Could not allocate memory for (%d) interfaces of class \"%s\".", b->classes[c].interfacesize, b->strings[b->classes[c].name]->data);
231242
}
232243
for (uint32_t in = 0; in < b->classes[c].interfacesize; in++) {
233244
b->classes[c].interfaces[in].methodsize = get_vint(bytecode, len, &i);

exec/cnex/bytecode.h

+39-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#define BYTECODE_H
33
#include <stdint.h>
44

5-
typedef struct tagTType {
5+
typedef struct tagTExportType {
66
unsigned int name;
77
unsigned int descriptor;
8-
} Type;
8+
} ExportType;
99

1010
typedef struct tagTFunction {
1111
unsigned int name;
@@ -15,6 +15,38 @@ typedef struct tagTFunction {
1515
unsigned int entry;
1616
} Function;
1717

18+
typedef struct tagTExportConstant {
19+
unsigned int name;
20+
unsigned int vtype;
21+
unsigned char *value;
22+
} Constant;
23+
24+
typedef struct tagTVariable {
25+
unsigned int name;
26+
unsigned int type;
27+
unsigned int index;
28+
} Variable;
29+
30+
//type exportfunction struct {
31+
//name int
32+
//descriptor int
33+
//index int
34+
//}
35+
36+
typedef struct tagTExportException {
37+
unsigned int name;
38+
} ExportException;
39+
40+
typedef struct tagTMethodDescriptor {
41+
unsigned int first;
42+
unsigned int second;
43+
} MethodDescriptor;
44+
45+
typedef struct tagTExportInterface {
46+
unsigned int name;
47+
MethodDescriptor *method_descriptors;
48+
} ExportInterface;
49+
1850
typedef struct tagTExportFunction {
1951
unsigned int name;
2052
unsigned int descriptor;
@@ -65,12 +97,16 @@ typedef struct tagTBytecode {
6597
const uint8_t *code;
6698
unsigned int codelen;
6799

68-
struct tagTType *export_types;
100+
struct tagTExportType *export_types;
101+
struct tagTExportConstant *export_constants;
69102
struct tagTExportFunction *export_functions;
103+
struct tagTExportException *export_exceptions;
104+
struct tagTExportInterface *export_interfaces;
70105
struct tagTFunction *functions;
71106
struct tagTImport *imports;
72107
struct tagTException *exceptions;
73108
struct tagTClass *classes;
109+
struct tagTVariable *variables;
74110
} TBytecode;
75111

76112

exec/cnex/cell.c

+3
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ Cell *cell_arrayIndexForWrite(Cell *c, size_t i)
508508
c->type = cArray;
509509
}
510510
assert(c->type == cArray);
511+
if (c->array == NULL) {
512+
c->array = array_createArray();
513+
}
511514
if (i >= c->array->size) {
512515
c->array->data = realloc(c->array->data, sizeof(Cell) * (i+1));
513516
if (c->array->data == NULL) {

0 commit comments

Comments
 (0)