-void code_test() {
- prog_section_def d1 = { TYPE_VOID, 28, 1 };
- prog_section_def d2 = { TYPE_FUNCTION, 29, 8 };
- prog_section_def d3 = { TYPE_STRING, 30, 14};
- prog_section_function f1 = { 1, 0, 0, 0, 1, 0,0, {0}};
- prog_section_function f2 = {-4, 0, 0, 0, 8, 0,0, {0}};
- prog_section_function f3 = { 0, 0, 0, 0, 14+13, 0,0, {0}};
- prog_section_function f4 = { 0, 0, 0, 0, 14+13+10, 0,0, {0}};
- prog_section_function f5 = { 0, 0, 0, 0, 14+13+10+7, 0,0, {0}};
- prog_section_function f6 = { 0, 0, 0, 0, 14+13+10+7+9, 0,0, {0}};
- prog_section_statement s1 = { INSTR_STORE_F, {30}, {OFS_PARM0}, {0}};
- prog_section_statement s2 = { INSTR_CALL1, {29}, {0}, {0}};
- prog_section_statement s3 = { INSTR_RETURN, {0}, {0}, {0}};
-
- code_chars_put("m_init", 0x6);
- code_chars_put("print", 0x5);
- code_chars_put("hello world\n", 0xC);
- code_chars_put("m_keydown", 0x9);
- code_chars_put("m_draw", 0x6);
- code_chars_put("m_toggle", 0x8);
- code_chars_put("m_shutdown", 0xA);
-
- code_globals_add(1); /* m_init */
- code_globals_add(2); /* print */
- code_globals_add(14); /* hello world in string table */
-
- /* now the defs */
- code_defs_add (d1); /* m_init */
- code_defs_add (d2); /* print */
- code_defs_add (d3); /*hello_world*/
- code_functions_add (f1); /* m_init */
- code_functions_add (f2); /* print */
- code_functions_add (f3); /* m_keydown */
- code_functions_add (f4);
- code_functions_add (f5);
- code_functions_add (f6);
- code_statements_add(s1);
- code_statements_add(s2);
- code_statements_add(s3);
+/*
+ * Same principle except this one allocates memory and writes the lno(optional) and the dat file
+ * directly out to allocated memory. Which is actually very useful for the future library support
+ * we're going to add.
+ */
+bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t **lnomem, size_t *sizelno) {
+ prog_header code_header;
+ uint32_t offset = 0;
+
+ if (!datmem)
+ return false;
+
+ code_create_header(code, &code_header);
+
+ #define WRITE_CHUNK(C,X,S) \
+ do { \
+ memcpy((void*)(&(*C)[offset]), (const void*)(X), (S)); \
+ offset += (S); \
+ } while (0)
+
+ /* Calculate size required to store entire file out to memory */
+ if (lnomem) {
+ uint32_t version = 1;
+
+ *sizelno += 4; /* LNOF */
+ *sizelno += sizeof(version);
+ *sizelno += sizeof(code_header.defs.length);
+ *sizelno += sizeof(code_header.globals.length);
+ *sizelno += sizeof(code_header.fields.length);
+ *sizelno += sizeof(code_header.statements.length);
+ *sizelno += sizeof(code->linenums[0]) * vec_size(code->linenums);
+
+ *lnomem = (uint8_t*)mem_a(*sizelno);
+
+ WRITE_CHUNK(lnomem, "LNOF", 4);
+ WRITE_CHUNK(lnomem, &version, sizeof(version));
+ WRITE_CHUNK(lnomem, &code_header.defs.length, sizeof(code_header.defs.length));
+ WRITE_CHUNK(lnomem, &code_header.globals.length, sizeof(code_header.globals.length));
+ WRITE_CHUNK(lnomem, &code_header.fields.length, sizeof(code_header.fields.length));
+ WRITE_CHUNK(lnomem, &code_header.statements.length, sizeof(code_header.statements.length));
+
+ /* something went terribly wrong */
+ if (offset != *sizelno) {
+ mem_d(*lnomem);
+ *sizelno = 0;
+ return false;
+ }
+ offset = 0;
+ }
+
+ /* Write out the dat */
+ *sizedat += sizeof(prog_header);
+ *sizedat += sizeof(prog_section_statement) * vec_size(code->statements);
+ *sizedat += sizeof(prog_section_def) * vec_size(code->defs);
+ *sizedat += sizeof(prog_section_field) * vec_size(code->fields);
+ *sizedat += sizeof(prog_section_function) * vec_size(code->functions);
+ *sizedat += sizeof(int32_t) * vec_size(code->globals);
+ *sizedat += 1 * vec_size(code->chars);
+
+ *datmem = (uint8_t*)mem_a(*sizedat);
+
+ WRITE_CHUNK(datmem, &code_header, sizeof(prog_header));
+ WRITE_CHUNK(datmem, code->statements, sizeof(prog_section_statement) * vec_size(code->statements));
+ WRITE_CHUNK(datmem, code->defs, sizeof(prog_section_def) * vec_size(code->defs));
+ WRITE_CHUNK(datmem, code->fields, sizeof(prog_section_field) * vec_size(code->fields));
+ WRITE_CHUNK(datmem, code->functions, sizeof(prog_section_function) * vec_size(code->functions));
+ WRITE_CHUNK(datmem, code->globals, sizeof(int32_t) * vec_size(code->globals));
+ WRITE_CHUNK(datmem, code->chars, 1 * vec_size(code->chars));
+
+ #undef WRITE_CHUNK
+
+ vec_free(code->statements);
+ vec_free(code->linenums);
+ vec_free(code->defs);
+ vec_free(code->fields);
+ vec_free(code->functions);
+ vec_free(code->globals);
+ vec_free(code->chars);
+
+ util_htdel(code->string_cache);
+ mem_d(code);
+
+ return true;