Verified Commit a912f443 authored by Camil Staps's avatar Camil Staps 🚀

Stripping bytecode (#56) works on GraphTest

parent 23e09611
Pipeline #16661 failed with stages
in 51 seconds
......@@ -25,8 +25,8 @@ import ABC.Interpreter.Util
:: *SerializedGraph =
{ graph :: !*String
, descinfo :: !*{#DescInfo}
, modules :: !*{#String}
, descinfo :: !{#DescInfo}
, modules :: !{#String}
, bytecode :: !String
}
......@@ -56,8 +56,8 @@ serialize_for_interpretation graph bcfile w
# rec =
{ graph = graph
, descinfo = {}//descs // TODO
, modules = {}//mods
, descinfo = descs
, modules = mods
, bytecode = bytecode
}
= (rec, w)
......@@ -67,7 +67,7 @@ where
# prefix_n = di_prefix_arity_and_mod bitand 0xff
# module_n = (di_prefix_arity_and_mod >> 8)-1
# module_name = mod_a.[module_n]
= make_symbol_name module_name di_name (min prefix_n PREFIX_D)
= make_symbol_name module_name di_name (min prefix_n PREFIX_D) +++ "\0"
where
PREFIX_D = 4
......@@ -212,13 +212,11 @@ graphToFile {graph,descinfo,modules,bytecode} f
# f = f <<< graph_size
# f = f <<< {#c \\ c <- graph_cpy}
# (descinfo_cpy,descinfo,descinfo_size) = copy descinfo
# f = f <<< descinfo_size
# f = writeList (\di f -> f <<< di.di_prefix_arity_and_mod <<< size di.di_name <<< di.di_name) descinfo_cpy f
# f = f <<< size descinfo
# f = writeArray (\di f -> f <<< di.di_prefix_arity_and_mod <<< size di.di_name <<< di.di_name) descinfo (size descinfo-1) f
# (modules_cpy,modules,modules_size) = copy modules
# f = f <<< modules_size
# f = writeList (\m f -> f <<< size m <<< m) modules_cpy f
# f = f <<< size modules
# f = writeArray (\m f -> f <<< size m <<< m) modules (size modules-1) f
# f = f <<< size bytecode
# f = f <<< bytecode
......@@ -237,9 +235,9 @@ where
# (x,arr) = arr![i]
= copy (i-1) arr [x:cpy]
writeList :: !(a *File -> *File) ![a] !*File -> *File
writeList write [x:xs] f = writeList write xs (write x f)
writeList _ [] f = f
writeArray :: !(e *File -> *File) !(arr e) !Int !*File -> *File | Array arr e
writeArray write xs -1 f = f
writeArray write xs i f = writeArray write xs (i-1) (write xs.[i] f)
graphFromFile :: !*File -> *(!Either String *SerializedGraph, !*File)
graphFromFile f
......
......@@ -13,6 +13,7 @@ import System._Pointer
import symbols_in_program
import code from "abc_instructions."
import code from "bcgen_instructions."
import code from "bytecode."
import code from "copy_host_to_interpreter."
import code from "copy_interpreter_to_host."
......
......@@ -89,6 +89,7 @@ DEP_INTERPRET:=$(subst .c,.h,$(SRC_INTERPRET))\
settings.h
SRC_INTERPRET_LIB:=$(SRC_INTERPRET)\
bcgen_instructions.c\
copy_host_to_interpreter.c\
copy_interpreter_to_host.c\
finalizers.c\
......
......@@ -49,7 +49,7 @@ void unsupported_instruction_warning(int16_t instruction) {
fprintf(stderr,"Warning: instruction %s is not supported by the interpreter\n",instruction_name(instruction));
}
void initialize_code(void) {
struct program *initialize_code(void) {
int i;
for(i = 0; i < N_ADD_ARG_LABELS; ++i)
Fadd_arg_label_used[i] = 0;
......@@ -68,6 +68,7 @@ void initialize_code(void) {
0,
0,
0,
0,
(struct word*) safe_malloc(512 * sizeof(struct word)),
(uint32_t*) safe_malloc(512 * sizeof(uint32_t)),
(uint64_t*) safe_malloc(512 * sizeof(uint64_t)),
......@@ -80,6 +81,8 @@ void initialize_code(void) {
allocated_strings_size = 512;
allocated_code_relocations_size = 512;
allocated_data_relocations_size = 512;
return &pgrm;
}
void code_next_module(void) {
......@@ -109,6 +112,7 @@ void store_code_elem(uint8_t bytewidth, uint64_t value) {
realloc_code();
pgrm.code[pgrm.code_size++] = (struct word) {bytewidth, value};
pgrm.code_byte_size+=bytewidth;
}
struct label *enter_label(char *label_name) {
......@@ -4346,6 +4350,24 @@ static void print_global_labels(struct label_node *node, FILE *program_file) {
print_global_labels(node->label_node_right, program_file);
}
static char *print_global_labels_to_string(struct label_node *node, char *ptr) {
if (node->label_node_left != NULL)
ptr=print_global_labels_to_string(node->label_node_left, ptr);
struct label *label = node->label_node_label_p;
memcpy(ptr, &label->label_offset, sizeof(label->label_offset));
ptr+=sizeof(label->label_offset);
if (label->label_module_n == - 1 || label->label_offset < 0)
for (char *name_ptr=label->label_name; *name_ptr; name_ptr++)
*ptr++=*name_ptr;
*ptr++='\0';
if (node->label_node_right != NULL)
ptr=print_global_labels_to_string(node->label_node_right, ptr);
return ptr;
}
void write_program(FILE *program_file) {
fwrite(&pgrm.code_size, sizeof(pgrm.code_size), 1, program_file);
fwrite(&pgrm.words_in_strings, sizeof(pgrm.words_in_strings), 1, program_file);
......@@ -4374,6 +4396,67 @@ void write_program(FILE *program_file) {
}
}
char *write_program_to_string(uint32_t *bytes_needed) {
count_and_renumber_labels(labels, 0);
*bytes_needed =
8*sizeof(uint32_t) +
pgrm.code_byte_size +
sizeof(uint32_t)*pgrm.strings_size +
sizeof(uint64_t)*pgrm.data_size +
(sizeof(uint32_t)+1)*label_id +
global_label_string_count +
sizeof(uint32_t)*2*(pgrm.code_reloc_size+pgrm.data_reloc_size);
char *bytecode=safe_malloc(*bytes_needed);
((uint32_t*)bytecode)[0]=pgrm.code_size;
((uint32_t*)bytecode)[1]=pgrm.words_in_strings;
((uint32_t*)bytecode)[2]=pgrm.strings_size;
((uint32_t*)bytecode)[3]=pgrm.data_size;
((uint32_t*)bytecode)[4]=label_id;
((uint32_t*)bytecode)[5]=global_label_string_count;
((uint32_t*)bytecode)[6]=pgrm.code_reloc_size;
((uint32_t*)bytecode)[7]=pgrm.data_reloc_size;
/*EPRINTF("\t%d %d\n\t%d %d\n\t%d %d\n\t%d %d\n",
pgrm.code_size, pgrm.data_size,
pgrm.words_in_strings, pgrm.strings_size,
label_id, global_label_string_count,
pgrm.code_reloc_size, pgrm.data_reloc_size);*/
char *ptr=(char*)&((uint32_t*)bytecode)[8];
for (int i=0; i<pgrm.code_size; i++) {
memcpy(ptr, &pgrm.code[i].value, pgrm.code[i].width);
ptr+=pgrm.code[i].width;
}
for (int i=0; i<pgrm.strings_size; i++) {
memcpy(ptr, &pgrm.strings[i], sizeof(uint32_t));
ptr+=sizeof(uint32_t);
}
for (int i=0; i<pgrm.data_size; i++) {
memcpy(ptr, &pgrm.data[i], sizeof(uint64_t));
ptr+=sizeof(uint64_t);
}
ptr=print_global_labels_to_string(labels, ptr);
for (int i=0; i<pgrm.code_reloc_size; i++) {
memcpy(ptr, &pgrm.code_relocations[i].relocation_offset, sizeof(uint32_t));
memcpy(ptr+4, &pgrm.code_relocations[i].relocation_label->label_id, sizeof(uint32_t));
ptr+=8;
}
for (int i=0; i<pgrm.data_reloc_size; i++) {
memcpy(ptr, &pgrm.data_relocations[i].relocation_offset, sizeof(uint32_t));
memcpy(ptr+4, &pgrm.data_relocations[i].relocation_label->label_id, sizeof(uint32_t));
ptr+=8;
}
return bytecode;
}
void free_label_node(struct label_node *node) {
free(node->label_node_label_p->label_name);
free(node->label_node_label_p);
......
#pragma once
#define BCGEN
#include "bytecode.h"
struct label {
......@@ -16,10 +17,11 @@ typedef struct relocation {
extern uint32_t list_code;
void initialize_code(void);
struct program *initialize_code(void);
void code_next_module(void);
struct word *add_add_arg_labels(void);
void write_program(FILE*);
char *write_program_to_string(uint32_t *bytes_needed);
void free_generated_program(void);
void add_code_and_data_offsets(void);
......@@ -30,6 +32,7 @@ struct relocation *add_code_relocation(struct label *label, uint32_t offset);
struct relocation *add_data_relocation(struct label *label, uint32_t offset);
void add_words_in_strings(uint32_t val);
void add_string_information(uint32_t data_offset);
void store_string(char *string,int string_length,int include_terminator);
void store_code_elem(uint8_t bytewidth, uint64_t value);
void store_data_l(uint64_t v);
......
......@@ -45,10 +45,8 @@ struct host_symbol {
struct program {
uint32_t code_size;
uint32_t data_size;
#ifdef INTERPRETER
BC_WORD *code;
BC_WORD *data;
#elif defined(BCGEN) || defined(LINKER)
#if defined(BCGEN) || defined(LINKER)
uint32_t code_byte_size;
uint32_t strings_size;
uint32_t words_in_strings;
uint32_t code_reloc_size;
......@@ -58,11 +56,14 @@ struct program {
uint64_t *data;
struct relocation *code_relocations;
struct relocation *data_relocations;
#else
BC_WORD *code;
BC_WORD *data;
#endif
uint32_t symbol_table_size;
struct symbol *symbol_table;
char *symbols;
#ifdef LINK_CLEAN_RUNTIME
#if defined(LINK_CLEAN_RUNTIME) && !defined(BCGEN)
char *host_symbols_strings;
int host_symbols_n;
struct host_symbol *host_symbols;
......
@echo off
cl /nologo /c /O2 /GS- ^
/DINTERPRETER /DLINK_CLEAN_RUNTIME /DWINDOWS ^
bcgen_instructions.c ^
bytecode.c ^
copy_host_to_interpreter.c ^
copy_interpreter_to_host.c ^
......@@ -10,6 +11,7 @@ cl /nologo /c /O2 /GS- ^
gc\mark.c ^
interpret.c ^
parse.c ^
strip.c ^
traps.c ^
util.c
ml64 /nologo /c /Fo interface.obj interface.asm
......
......@@ -544,8 +544,6 @@ int parse_program(struct parser *state, struct char_provider *cp) {
return 1;
struct symbol *sym = &state->program->symbol_table[sym_i];
EPRINTF("code reloc %d %d %ld\n",code_i,sym_i,sym->offset);
#ifdef LINKER
struct label *label;
if (sym->name[0] == '\0')
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment