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

WIP on the stripper for #56

parent acfb8045
......@@ -50,6 +50,10 @@ serialize_for_interpretation graph bcfile w
| isError bytecode = abort "Failed to read the bytecode file\n"
# bytecode = fromOk bytecode
#! bytecodep = strip_bytecode bytecode {#symbol_name di mods \\ di <-: descs}
#! bytecode = derefString bytecodep
| free_to_false bytecodep = abort "cannot happen\n"
# rec =
{ graph = graph
, descinfo = descs
......@@ -57,6 +61,20 @@ serialize_for_interpretation graph bcfile w
, bytecode = bytecode
}
= (rec, w)
where
symbol_name :: !DescInfo !{#String} -> String
symbol_name {di_prefix_arity_and_mod,di_name} mod_a
# 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)
where
PREFIX_D = 4
strip_bytecode :: !String !{#String} -> Pointer
strip_bytecode bytecode descriptors = code {
ccall strip_bytecode "sA:VIp"
}
deserialize :: !SerializedGraph !FilePath !*World -> *(a, !*World)
deserialize {graph,descinfo,modules,bytecode} thisexe w
......
......@@ -23,6 +23,7 @@ import code from "interface."
import code from "interpret."
import code from "mark."
import code from "parse."
import code from "strip."
import code from "traps."
import code from "util."
import code from library "msvcrt_library"
......
......@@ -91,7 +91,8 @@ DEP_INTERPRET:=$(subst .c,.h,$(SRC_INTERPRET))\
SRC_INTERPRET_LIB:=$(SRC_INTERPRET)\
copy_host_to_interpreter.c\
copy_interpreter_to_host.c\
finalizers.c
finalizers.c\
strip.c
ASM_INTERPRET_LIB:=$(subst .c,.s,$(SRC_INTERPRET_LIB))
OBJ_INTERPRET_LIB:=$(subst .c,.o,$(SRC_INTERPRET_LIB)) interface.o
DEP_INTERPRET_LIB:=$(subst .c,.h,$(SRC_INTERPRET_LIB))\
......
#include "bytecode.h"
#include "strip.h"
#include "util.h"
struct label {
int32_t offset;
char *name;
};
static uint8_t *code;
static int32_t *code_indices;
static uint64_t *data;
static struct label *inactive_labels;
static void activate_label() {
}
void strip_bytecode(uint32_t *bytecode, struct clean_string **descriptors,
uint32_t *result_size, uint32_t **result) {
uint32_t code_size=bytecode[0];
uint32_t words_in_strings=bytecode[1];
uint32_t strings_size=bytecode[2];
uint32_t data_size=bytecode[3];
uint32_t n_labels=bytecode[4];
uint32_t global_label_string_count=bytecode[5];
uint32_t code_reloc_size=bytecode[6];
uint32_t data_reloc_size=bytecode[7];
code_indices=safe_malloc(sizeof(uint32_t)*code_size);
code=(uint8_t*)&bytecode[8];
int ci=0;
int i=0;
while (ci<code_size) {
uint16_t instr=*(uint16_t*)&code[i];
code_indices[ci++]=i;
i+=2;
char *type=instruction_type(instr);
for (; *type; type++) {
code_indices[ci++]=-1;
switch (*type) {
case 'c': /* Char */
i+=1;
break;
case 'I': /* Instruction */
case 'n': /* Stack index */
case 'N': /* Stack index, optimised to byte width */
case 'a': /* Arity */
i+=2;
break;
case 'd': /* Descriptor */
case 'l': /* Label */
case 'S': /* String label */
case 's': /* String label */
i+=4;
break;
case 'r': /* Real */
case 'i': /* Int */
i+=8;
break;
default:
EPRINTF("error in strip_bytecode\n");
exit(-1);
}
}
}
bytecode=(uint32_t*)&code[i];
bytecode+=strings_size;
data=(uint64_t*)bytecode;
bytecode=(uint32_t*)&data[data_size];
char *labels_in_bytecode=(char*)bytecode;
inactive_labels=safe_malloc(sizeof(struct label)*n_labels);
for (int i=0; i<n_labels; i++) {
inactive_labels[i].offset=*(int32_t*)labels_in_bytecode;
inactive_labels[i].name=&labels_in_bytecode[4];
labels_in_bytecode+=4;
while (*labels_in_bytecode++);
}
fprintf(stderr,"%ld descriptors\n",((BC_WORD*)descriptors)[-2]);
for (int i=0; i<((BC_WORD*)descriptors)[-2]; i++) {
char desc[256];
strncpy(desc, &descriptors[i]->cs_characters, descriptors[i]->cs_size);
desc[descriptors[i]->cs_size]='\0';
fprintf(stderr,"desc: %3d %s\n",i,desc);
}
result=&bytecode;
*result_size=0;
}
#pragma once
#include "bytecode.h"
struct clean_string {
void *cs_desc;
BC_WORD cs_size;
char cs_characters[0];
};
void strip_bytecode(uint32_t *bytecode, struct clean_string **descriptors,
uint32_t *result_size, uint32_t **result);
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