diff options
author | Adrian Thurston <thurston@complang.org> | 2014-12-25 11:46:23 -0500 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2014-12-25 11:46:23 -0500 |
commit | 4bd915e48b6b6278ed8c4b7be95781535f2ce10a (patch) | |
tree | c87f8057ba33a56490dcce1f332129acdfd66202 /src | |
parent | 353ddb63f7268d2cc96df328c8562b4f3050d1d6 (diff) | |
download | colm-4bd915e48b6b6278ed8c4b7be95781535f2ce10a.tar.gz |
some renaming and cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/bytecode.c | 512 | ||||
-rw-r--r-- | src/declare.cc | 1 | ||||
-rw-r--r-- | src/program.c | 20 | ||||
-rw-r--r-- | src/program.h | 18 | ||||
-rw-r--r-- | src/struct.c | 24 | ||||
-rw-r--r-- | src/struct.h | 5 | ||||
-rw-r--r-- | src/tree.c | 26 | ||||
-rw-r--r-- | src/tree.h | 1 |
9 files changed, 312 insertions, 297 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 869ddc62..415bd4a2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ noinst_PROGRAMS = bootstrap0 bootstrap1 RUNTIME_SRC = \ map.c pdarun.c list.c input.c debug.c \ codevect.c pool.c string.c tree.c iter.c \ - bytecode.c program.c + bytecode.c program.c struct.c RUNTIME_HDR = \ bytecode.h config.h defs.h debug.h pool.h input.h \ diff --git a/src/bytecode.c b/src/bytecode.c index ef44f5b4..0f2a078d 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -17,6 +17,8 @@ #include <stdlib.h> #include <unistd.h> +typedef struct colm_struct Struct; + #if SIZEOF_LONG != 4 && SIZEOF_LONG != 8 #error "SIZEOF_LONG contained an unexpected value" #endif @@ -470,258 +472,6 @@ void rcodeDownrefAll( Program *prg, Tree **sp, RtCodeVect *rev ) } } -void rcodeDownref( Program *prg, Tree **sp, Code *instr ) -{ -again: - switch ( *instr++ ) { - case IN_PARSE_SAVE_STEPS: { - debug( prg, REALM_BYTECODE, "IN_PARSE_SAVE_STEPS\n" ); - break; - } - case IN_PARSE_INIT_BKT: { - Tree *parser; - Word pcr; - Word steps; - - debug( prg, REALM_BYTECODE, "IN_PARSE_INIT_BKT\n" ); - - read_tree( parser ); - read_word( pcr ); - read_word( steps ); - - treeDownref( prg, sp, (Tree*)parser ); - break; - } - - case IN_LOAD_TREE: { - Word w; - read_word( w ); - debug( prg, REALM_BYTECODE, "IN_LOAD_TREE %p\n", (Tree*)w ); - treeDownref( prg, sp, (Tree*)w ); - break; - } - case IN_LOAD_WORD: { - Word w; - read_word( w ); - debug( prg, REALM_BYTECODE, "IN_LOAD_WORD\n" ); - break; - } - case IN_RESTORE_LHS: { - Tree *restore; - read_tree( restore ); - debug( prg, REALM_BYTECODE, "IN_RESTORE_LHS\n" ); - treeDownref( prg, sp, restore ); - break; - } - - case IN_PARSE_FRAG_BKT: { - Half stopId; - read_half( stopId ); - debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_BKT\n" ); - break; - } - case IN_PARSE_FRAG_EXIT_BKT: { - debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_EXIT_BKT\n" ); - break; - } - case IN_PARSE_FINISH_BKT: { - Half stopId; - read_half( stopId ); - debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_BKT\n" ); - break; - } - case IN_PARSE_FINISH_EXIT_BKT: { - debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_EXIT_BKT\n" ); - break; - } - case IN_PCR_CALL: { - debug( prg, REALM_BYTECODE, "IN_PCR_CALL\n" ); - break; - } - case IN_PCR_RET: { - debug( prg, REALM_BYTECODE, "IN_PCR_RET\n" ); - return; - } - case IN_PCR_END_DECK: { - debug( prg, REALM_BYTECODE, "IN_PCR_END_DECK\n" ); - return; - } - case IN_INPUT_APPEND_BKT: { - Tree *parser; - Tree *input; - Word len; - read_tree( parser ); - read_tree( input ); - read_word( len ); - - debug( prg, REALM_BYTECODE, "IN_INPUT_APPEND_BKT\n" ); - - treeDownref( prg, sp, parser ); - treeDownref( prg, sp, input ); - break; - } - case IN_INPUT_PULL_BKT: { - Tree *string; - read_tree( string ); - - debug( prg, REALM_BYTECODE, "IN_INPUT_PULL_BKT\n" ); - - treeDownref( prg, sp, string ); - break; - } - case IN_INPUT_PUSH_BKT: { - Word len; - read_word( len ); - - debug( prg, REALM_BYTECODE, "IN_INPUT_PUSH_BKT\n" ); - break; - } - case IN_LOAD_GLOBAL_BKT: { - debug( prg, REALM_BYTECODE, "IN_LOAD_GLOBAL_BKT\n" ); - break; - } - case IN_LOAD_CONTEXT_BKT: { - debug( prg, REALM_BYTECODE, "IN_LOAD_CONTEXT_BKT\n" ); - break; - } - case IN_LOAD_PARSER_BKT: { - /* Tree *parser; */ - consume_word(); - debug( prg, REALM_BYTECODE, "IN_LOAD_PARSER_BKT\n" ); - break; - } - case IN_LOAD_INPUT_BKT: { - /* Tree *input; */ - consume_word(); - debug( prg, REALM_BYTECODE, "IN_LOAD_INPUT_BKT\n" ); - break; - } - case IN_GET_FIELD_BKT: { - short field; - read_half( field ); - - debug( prg, REALM_BYTECODE, "IN_GET_FIELD_BKT %hd\n", field ); - break; - } - case IN_SET_FIELD_BKT: { - short field; - Tree *val; - read_half( field ); - read_tree( val ); - - debug( prg, REALM_BYTECODE, "IN_SET_FIELD_BKT %hd\n", field ); - - treeDownref( prg, sp, val ); - break; - } - case IN_PTR_DEREF_BKT: { - Tree *ptr; - read_tree( ptr ); - - debug( prg, REALM_BYTECODE, "IN_PTR_DEREF_BKT\n" ); - - treeDownref( prg, sp, ptr ); - break; - } - case IN_SET_TOKEN_DATA_BKT: { - Word oldval; - read_word( oldval ); - - debug( prg, REALM_BYTECODE, "IN_SET_TOKEN_DATA_BKT\n" ); - - Head *head = (Head*)oldval; - stringFree( prg, head ); - break; - } - case IN_LIST_PUSH_HEAD_BKT: { - debug( prg, REALM_BYTECODE, "IN_LIST_PUSH_HEAD_BKT\n" ); - break; - } - case IN_LIST_POP_HEAD_BKT: { - Tree *val; - read_tree( val ); - - debug( prg, REALM_BYTECODE, "IN_LIST_POP_HEAD_BKT\n" ); - - treeDownref( prg, sp, val ); - break; - } - case IN_LIST_PUSH_TAIL_BKT: { - debug( prg, REALM_BYTECODE, "IN_LIST_PUSH_TAIL_BKT\n" ); - break; - } - case IN_LIST_POP_TAIL_BKT: { - Tree *val; - read_tree( val ); - - debug( prg, REALM_BYTECODE, "IN_LIST_POP_TAIL_BKT\n" ); - - treeDownref( prg, sp, val ); - break; - } - case IN_GET_LIST_MEM_BKT: { - short field; - read_half( field ); - - debug( prg, REALM_BYTECODE, "IN_GET_LIST_MEM_BKT %hd\n", field ); - break; - } - case IN_SET_LIST_MEM_BKT: { - Half field; - Tree *val; - read_half( field ); - read_tree( val ); - - debug( prg, REALM_BYTECODE, "IN_SET_LIST_MEM_BKT %hd\n", field ); - - treeDownref( prg, sp, val ); - break; - } - case IN_MAP_INSERT_BKT: { - /* uchar inserted; */ - Tree *key; - consume_byte(); - read_tree( key ); - - debug( prg, REALM_BYTECODE, "IN_MAP_INSERT_BKT\n" ); - - treeDownref( prg, sp, key ); - break; - } - case IN_MAP_STORE_BKT: { - Tree *key, *val; - read_tree( key ); - read_tree( val ); - - debug( prg, REALM_BYTECODE,"IN_MAP_STORE_BKT\n" ); - - treeDownref( prg, sp, key ); - treeDownref( prg, sp, val ); - break; - } - case IN_MAP_REMOVE_BKT: { - Tree *key, *val; - read_tree( key ); - read_tree( val ); - - debug( prg, REALM_BYTECODE, "IN_MAP_REMOVE_BKT\n" ); - - treeDownref( prg, sp, key ); - treeDownref( prg, sp, val ); - break; - } - case IN_STOP: { - return; - } - default: { - fatal( "UNKNOWN INSTRUCTION 0x%2x: -- reverse code downref\n", *(instr-1)); - assert(false); - break; - } - } - goto again; -} - void mainExecution( Program *prg, Execution *exec, Code *code ) { Tree **sp = prg->stackRoot; @@ -1541,7 +1291,7 @@ again: read_half( size ); debug( prg, REALM_BYTECODE, "IN_NEW_STRUCT %hd\n", size ); - HeapItem *item = newStruct( prg, size ); + Struct *item = colm_new_struct( prg, size ); vm_push( (Tree*)item ); break; } @@ -4142,3 +3892,259 @@ out: return sp; } +/* + * Deleteing rcode required downreffing any trees held by it. + */ +void rcodeDownref( Program *prg, Tree **sp, Code *instr ) +{ +again: + switch ( *instr++ ) { + case IN_PARSE_SAVE_STEPS: { + debug( prg, REALM_BYTECODE, "IN_PARSE_SAVE_STEPS\n" ); + break; + } + case IN_PARSE_INIT_BKT: { + Tree *parser; + Word pcr; + Word steps; + + debug( prg, REALM_BYTECODE, "IN_PARSE_INIT_BKT\n" ); + + read_tree( parser ); + read_word( pcr ); + read_word( steps ); + + treeDownref( prg, sp, (Tree*)parser ); + break; + } + + case IN_LOAD_TREE: { + Word w; + read_word( w ); + debug( prg, REALM_BYTECODE, "IN_LOAD_TREE %p\n", (Tree*)w ); + treeDownref( prg, sp, (Tree*)w ); + break; + } + case IN_LOAD_WORD: { + Word w; + read_word( w ); + debug( prg, REALM_BYTECODE, "IN_LOAD_WORD\n" ); + break; + } + case IN_RESTORE_LHS: { + Tree *restore; + read_tree( restore ); + debug( prg, REALM_BYTECODE, "IN_RESTORE_LHS\n" ); + treeDownref( prg, sp, restore ); + break; + } + + case IN_PARSE_FRAG_BKT: { + Half stopId; + read_half( stopId ); + debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_BKT\n" ); + break; + } + case IN_PARSE_FRAG_EXIT_BKT: { + debug( prg, REALM_BYTECODE, "IN_PARSE_FRAG_EXIT_BKT\n" ); + break; + } + case IN_PARSE_FINISH_BKT: { + Half stopId; + read_half( stopId ); + debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_BKT\n" ); + break; + } + case IN_PARSE_FINISH_EXIT_BKT: { + debug( prg, REALM_BYTECODE, "IN_PARSE_FINISH_EXIT_BKT\n" ); + break; + } + case IN_PCR_CALL: { + debug( prg, REALM_BYTECODE, "IN_PCR_CALL\n" ); + break; + } + case IN_PCR_RET: { + debug( prg, REALM_BYTECODE, "IN_PCR_RET\n" ); + return; + } + case IN_PCR_END_DECK: { + debug( prg, REALM_BYTECODE, "IN_PCR_END_DECK\n" ); + return; + } + case IN_INPUT_APPEND_BKT: { + Tree *parser; + Tree *input; + Word len; + read_tree( parser ); + read_tree( input ); + read_word( len ); + + debug( prg, REALM_BYTECODE, "IN_INPUT_APPEND_BKT\n" ); + + treeDownref( prg, sp, parser ); + treeDownref( prg, sp, input ); + break; + } + case IN_INPUT_PULL_BKT: { + Tree *string; + read_tree( string ); + + debug( prg, REALM_BYTECODE, "IN_INPUT_PULL_BKT\n" ); + + treeDownref( prg, sp, string ); + break; + } + case IN_INPUT_PUSH_BKT: { + Word len; + read_word( len ); + + debug( prg, REALM_BYTECODE, "IN_INPUT_PUSH_BKT\n" ); + break; + } + case IN_LOAD_GLOBAL_BKT: { + debug( prg, REALM_BYTECODE, "IN_LOAD_GLOBAL_BKT\n" ); + break; + } + case IN_LOAD_CONTEXT_BKT: { + debug( prg, REALM_BYTECODE, "IN_LOAD_CONTEXT_BKT\n" ); + break; + } + case IN_LOAD_PARSER_BKT: { + /* Tree *parser; */ + consume_word(); + debug( prg, REALM_BYTECODE, "IN_LOAD_PARSER_BKT\n" ); + break; + } + case IN_LOAD_INPUT_BKT: { + /* Tree *input; */ + consume_word(); + debug( prg, REALM_BYTECODE, "IN_LOAD_INPUT_BKT\n" ); + break; + } + case IN_GET_FIELD_BKT: { + short field; + read_half( field ); + + debug( prg, REALM_BYTECODE, "IN_GET_FIELD_BKT %hd\n", field ); + break; + } + case IN_SET_FIELD_BKT: { + short field; + Tree *val; + read_half( field ); + read_tree( val ); + + debug( prg, REALM_BYTECODE, "IN_SET_FIELD_BKT %hd\n", field ); + + treeDownref( prg, sp, val ); + break; + } + case IN_PTR_DEREF_BKT: { + Tree *ptr; + read_tree( ptr ); + + debug( prg, REALM_BYTECODE, "IN_PTR_DEREF_BKT\n" ); + + treeDownref( prg, sp, ptr ); + break; + } + case IN_SET_TOKEN_DATA_BKT: { + Word oldval; + read_word( oldval ); + + debug( prg, REALM_BYTECODE, "IN_SET_TOKEN_DATA_BKT\n" ); + + Head *head = (Head*)oldval; + stringFree( prg, head ); + break; + } + case IN_LIST_PUSH_HEAD_BKT: { + debug( prg, REALM_BYTECODE, "IN_LIST_PUSH_HEAD_BKT\n" ); + break; + } + case IN_LIST_POP_HEAD_BKT: { + Tree *val; + read_tree( val ); + + debug( prg, REALM_BYTECODE, "IN_LIST_POP_HEAD_BKT\n" ); + + treeDownref( prg, sp, val ); + break; + } + case IN_LIST_PUSH_TAIL_BKT: { + debug( prg, REALM_BYTECODE, "IN_LIST_PUSH_TAIL_BKT\n" ); + break; + } + case IN_LIST_POP_TAIL_BKT: { + Tree *val; + read_tree( val ); + + debug( prg, REALM_BYTECODE, "IN_LIST_POP_TAIL_BKT\n" ); + + treeDownref( prg, sp, val ); + break; + } + case IN_GET_LIST_MEM_BKT: { + short field; + read_half( field ); + + debug( prg, REALM_BYTECODE, "IN_GET_LIST_MEM_BKT %hd\n", field ); + break; + } + case IN_SET_LIST_MEM_BKT: { + Half field; + Tree *val; + read_half( field ); + read_tree( val ); + + debug( prg, REALM_BYTECODE, "IN_SET_LIST_MEM_BKT %hd\n", field ); + + treeDownref( prg, sp, val ); + break; + } + case IN_MAP_INSERT_BKT: { + /* uchar inserted; */ + Tree *key; + consume_byte(); + read_tree( key ); + + debug( prg, REALM_BYTECODE, "IN_MAP_INSERT_BKT\n" ); + + treeDownref( prg, sp, key ); + break; + } + case IN_MAP_STORE_BKT: { + Tree *key, *val; + read_tree( key ); + read_tree( val ); + + debug( prg, REALM_BYTECODE,"IN_MAP_STORE_BKT\n" ); + + treeDownref( prg, sp, key ); + treeDownref( prg, sp, val ); + break; + } + case IN_MAP_REMOVE_BKT: { + Tree *key, *val; + read_tree( key ); + read_tree( val ); + + debug( prg, REALM_BYTECODE, "IN_MAP_REMOVE_BKT\n" ); + + treeDownref( prg, sp, key ); + treeDownref( prg, sp, val ); + break; + } + case IN_STOP: { + return; + } + default: { + fatal( "UNKNOWN INSTRUCTION 0x%2x: -- reverse code downref\n", *(instr-1)); + assert(false); + break; + } + } + goto again; +} + + diff --git a/src/declare.cc b/src/declare.cc index 7066a0da..08f84193 100644 --- a/src/declare.cc +++ b/src/declare.cc @@ -979,7 +979,6 @@ void Compiler::addError() globalObjectDef->rootScope->insertField( el->name, el ); } - void Compiler::initMapFunctions( GenericType *gen ) { addLengthField( gen->objDef, IN_MAP_LENGTH ); diff --git a/src/program.c b/src/program.c index b331c484..3ebf8438 100644 --- a/src/program.c +++ b/src/program.c @@ -17,7 +17,7 @@ #define VM_STACK_SIZE (8192) -void clearGlobal( Program *prg, Tree **sp ) +static void colm_clear_global( Program *prg, Tree **sp ) { /* Downref all the fields in the global object. */ int g; @@ -32,7 +32,7 @@ void clearGlobal( Program *prg, Tree **sp ) treeFree( prg, prg->global ); } -void allocGlobal( Program *prg ) +static void colm_alloc_global( Program *prg ) { /* Alloc the global. */ Tree *tree = treeAllocate( prg ); @@ -206,7 +206,7 @@ Program *colm_new_program( RuntimeData *rtd ) prg->falseVal = (Tree*)falseInt; /* Allocate the global variable. */ - allocGlobal( prg ); + colm_alloc_global( prg ); /* Allocate the VM stack. */ vm_init( prg ); @@ -283,10 +283,10 @@ Tree *colm_run_func( struct colm_program *prg, int frameId, return prg->returnVal; }; -static void colm_clear_heap( Program *prg, Tree **sp ) +static void colm_clear_orig_heap( Program *prg, Tree **sp ) { /* Clear the heap. */ - Kid *a = prg->heap; + Kid *a = prg->origHeap; while ( a != 0 ) { Kid *next = a->next; objectDownref( prg, sp, a->tree ); @@ -295,11 +295,11 @@ static void colm_clear_heap( Program *prg, Tree **sp ) } } -static void colm_clear_heap2( Program *prg, Tree **sp ) +static void colm_clear_heap( Program *prg, Tree **sp ) { - HeapItem *hi = prg->heapHead; + struct colm_struct *hi = prg->heap.head; while ( hi != 0 ) { - HeapItem *next = hi->next; + struct colm_struct *next = hi->next; short *t = prg->rtd->selInfo[hi->id].trees; int i, len = prg->rtd->selInfo[hi->id].treesLen; for ( i = 0; i < len; i++ ) @@ -315,9 +315,9 @@ int colm_delete_program( Program *prg ) int exitStatus = prg->exitStatus; treeDownref( prg, sp, prg->returnVal ); - clearGlobal( prg, sp ); + colm_clear_global( prg, sp ); + colm_clear_orig_heap( prg, sp ); colm_clear_heap( prg, sp ); - colm_clear_heap2( prg, sp ); treeDownref( prg, sp, prg->trueVal ); treeDownref( prg, sp, prg->falseVal ); diff --git a/src/program.h b/src/program.h index 4e119ca4..03fd1d6b 100644 --- a/src/program.h +++ b/src/program.h @@ -83,12 +83,18 @@ typedef struct colm_sections } RuntimeData; -typedef struct colm_heap_item +struct colm_struct { short id; - struct colm_heap_item *prev; - struct colm_heap_item *next; -} HeapItem; + struct colm_struct *prev; + struct colm_struct *next; +}; + +typedef struct colm_heap_list +{ + struct colm_struct *head; + struct colm_struct *tail; +} HeapList; typedef struct colm_program { @@ -114,9 +120,9 @@ typedef struct colm_program Tree *trueVal; Tree *falseVal; - Kid *heap; + Kid *origHeap; - struct colm_heap_item *heapHead, *heapTail; + struct colm_heap_list heap; Stream *stdinVal; Stream *stdoutVal; diff --git a/src/struct.c b/src/struct.c new file mode 100644 index 00000000..8a13e9b5 --- /dev/null +++ b/src/struct.c @@ -0,0 +1,24 @@ +#include <colm/program.h> +#include <colm/struct.h> + +struct colm_struct *colm_new_struct( Program *prg, int id ) +{ + int structSize = prg->rtd->selInfo[id].size; + size_t memsize = sizeof(struct colm_struct) + ( sizeof(Tree*) * structSize ); + struct colm_struct *item = (struct colm_struct*) malloc( memsize ); + memset( item, 0, memsize ); + item->id = id; + + if ( prg->heap.head == 0 ) { + prg->heap.head = prg->heap.tail = item; + item->prev = item->next = 0; + } + else { + item->prev = prg->heap.tail; + item->next = 0; + prg->heap.tail->next = item; + prg->heap.tail = item; + } + + return item; +} diff --git a/src/struct.h b/src/struct.h index ddf2081e..85bbe2d1 100644 --- a/src/struct.h +++ b/src/struct.h @@ -1,5 +1,8 @@ #ifndef _COLM_STRUCT_H -#define colm_struct_val(obj, field) ((Tree**)(((HeapItem*)obj)+1))[field] +#define colm_struct_val(obj, field) \ + ((struct colm_tree**)(((struct colm_struct*)obj)+1))[field] + +struct colm_struct *colm_new_struct( struct colm_program *prg, int id ); #endif @@ -243,8 +243,8 @@ Tree *constructPointer( Program *prg, Tree *tree ) { Kid *kid = kidAllocate( prg ); kid->tree = tree; - kid->next = prg->heap; - prg->heap = kid; + kid->next = prg->origHeap; + prg->origHeap = kid; Pointer *pointer = (Pointer*) treeAllocate( prg ); pointer->id = LEL_ID_PTR; @@ -253,28 +253,6 @@ Tree *constructPointer( Program *prg, Tree *tree ) return (Tree*)pointer; } -HeapItem *newStruct( Program *prg, int id ) -{ - int structSize = prg->rtd->selInfo[id].size; - size_t memsize = sizeof(HeapItem) + ( sizeof(Tree*) * structSize ); - HeapItem *item = (HeapItem*) malloc( memsize ); - memset( item, 0, memsize ); - item->id = id; - - if ( prg->heapHead == 0 ) { - prg->heapHead = prg->heapTail = item; - item->prev = item->next = 0; - } - else { - item->prev = prg->heapTail; - item->next = 0; - prg->heapTail->next = item; - prg->heapTail = item; - } - - return item; -} - Tree *constructTerm( Program *prg, Word id, Head *tokdata ) { LangElInfo *lelInfo = prg->rtd->lelInfo; @@ -360,7 +360,6 @@ void userIterDestroy2( struct colm_program *prg, Tree ***psp, UserIter *uiter ); Tree *castTree( struct colm_program *prg, int langElId, Tree *tree ); StreamImpl *streamToImpl( Stream *ptr ); -struct colm_heap_item *newStruct( struct colm_program *prg, int id ); #if defined(__cplusplus) } |