From: ken Date: Tue, 17 Jan 2017 00:21:02 +0000 (-0800) Subject: debugging support X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=commitdiff_plain;h=7896cfff4745b3999850dd301a296de71f7356eb debugging support --- diff --git a/Makefile b/Makefile index f86254b..ad90bba 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ # Driver sources DRIVERS ?= apc testapc +# Debug Level +DEBUG ?= 1 + # Yacc YACC := bison YFLAGS ?= -v -d -Wall @@ -74,7 +77,7 @@ cleanCMD = $(if $(wildcard $1),rm $(wildcard $(sort $1))) # Rules .SECONDEXPANSION: $(ldTRG): $$(call ldDEP,$$@) | $(hGEN) ; $(call LDCMD,$^,$@) -%-d.o: CFLAGS+= -Og -ggdb +%-d.o: CFLAGS+= -Og -ggdb -DDEBUG=$(DEBUG) %.o %-d.o: %.c $$(call cGENDEP,$$(dir $$@)%.c) ; $(call CCMD,$<,$@) %.tab.h: %.tab.c ; %.tab.c: %.y $$(call S2S,YCMD,%.y,$$@) ; diff --git a/src/ir.c b/src/ir.c index ff82d01..db97097 100644 --- a/src/ir.c +++ b/src/ir.c @@ -14,16 +14,19 @@ #include //u8_* functions #include //uint8_t as a char #include //u32_cpy +#include //ulc_fprintf /* Local */ +#include "print.h" #include "apc.h" #include "ir.h" +#undef do_error +#define do_error(...) exit(-1) /* Public */ int ir_init(void); void ir_quit(void); int ir_linker(void); int ir_condenser(void); /* Memory allocation structures */ -enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT }; struct pagenode_t; struct pagenode_header_t { struct pagenode_t* next; @@ -37,6 +40,13 @@ struct pagelist_t { struct pagenode_t* root, * head; size_t pagesize; }; +#define DATA_PAGESIZE (sys_pagesize) +#define NAME_PAGESIZE (APC_NAME_MAX * 1024) +#define PL_HEADERSIZE (sizeof(struct pagenode_header_t)) +#define PL_HEADSIZE(_PL) (_PL.head->header.head - _PL.head->root) +#define PL_HEADMEM(_PL) (_PL.pagesize - PL_HEADERSIZE - PL_HEADSIZE(_PL)) +/* Set data mem */ +enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT }; struct ir_namelist_t; struct ir_namelist_t { struct ir_namelist_t* nextsib; @@ -51,7 +61,6 @@ struct ir_setld_t long long ref; struct ir_namelist_t* namelist, * namelist_head; }; -/* Set data mem */ struct ir_setdata_header_t { enum dtype type; uint8_t* src_filename, * data_name; @@ -99,50 +108,22 @@ struct ir_set_t }; /* Functions */ static inline -int init_pagelist(struct pagelist_t*,size_t); -static inline struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,const uint8_t*); +static inline +union ir_setdata_t* ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int); +static inline +int init_pagelist(struct pagelist_t*,size_t); static -void ir_free_pagenodes(struct pagenode_t*); +void ir_free_pagenodes(struct pagenode_t*); static inline -int bytes_identical(const uint8_t*,const uint8_t*); +int bytes_identical(const uint8_t*,const uint8_t*); static -void* stack_alloc(size_t); +void* stack_alloc(size_t); +#define struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T))) static -uint8_t* name_alloc(const uint8_t*); -static inline -union ir_setdata_t* ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int); -/* Function-Like Macros */ -#define do_warn() do { \ - } while (0) -#define wprint(str) do { \ - fprintf(stderr, str); \ - do_warn(); \ - } while (0) -#define wprintf(fmt,...) do { \ - fprintf(stderr, fmt, __VA_ARGS__); \ - do_warn(); \ - } while (0) -#define do_error() do { \ - exit(-1); \ - } while (0) -#define eprint(str) do { \ - fprintf(stderr, str); \ - do_error(); \ - } while (0) -#define eprintf(fmt,...) do { \ - fprintf(stderr, fmt, __VA_ARGS__); \ - do_error(); \ - } while (0) -#define struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T))) -#define DATA_PAGESIZE (sys_pagesize) -#define NAME_PAGESIZE (APC_NAME_MAX * 1024) -#define PL_HEADERSIZE (sizeof(struct pagenode_header_t)) -#define PL_HEADSIZE(_PL) (_PL.head->header.head - _PL.head->root) -#define PL_HEADMEM(_PL) (_PL.pagesize - PL_HEADERSIZE - PL_HEADSIZE(_PL)) -/* Memory */ +uint8_t* name_alloc(const uint8_t*); extern //apc.c -long sys_pagesize; +long sys_pagesize; static struct pagelist_t datapages, namepages; static @@ -152,9 +133,9 @@ struct ir_class_t root_class = { .name = (uint8_t*)"." }; int ir_init ( void ) { if (init_pagelist(&datapages, (size_t)DATA_PAGESIZE)) - eprint("Memory allocation error\n"); + eprintf("Memory allocation error\n"); if (init_pagelist(&namepages, (size_t)NAME_PAGESIZE)) - eprint("Memory allocation error\n"); + eprintf("Memory allocation error\n"); return 0; } @@ -256,7 +237,7 @@ struct ir_set_t* ir_class_addset if (iter->name == NULL) eprintf("Null name pointer in class %p\n", iter); if (name == NULL) - eprintf("Null set added to class %s\n", iter->name); + eprintf("Null set added to class %U\n", iter->name); check: if (bytes_identical(iter->name, name)) return iter; @@ -445,7 +426,7 @@ union ir_setdata_t* ir_framedata ) { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t); if (name == NULL) - eprint("Null name in set allocation\n"); + eprintf("Null name in set allocation\n"); framedata->header.type = type; framedata->header.data_name = name_alloc(name); framedata->frameinfo.facing = d; @@ -458,7 +439,7 @@ union ir_setdata_t* ir_audio ( const uint8_t* name ) { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t); if (name == NULL) - eprint("Null audio\n"); + eprintf("Null audio\n"); audio->header.type = ADAT; audio->header.data_name = name_alloc(name); return (union ir_setdata_t*) audio; @@ -470,7 +451,7 @@ struct ir_classld_t* ir_classld_from_class ( struct ir_class_t* class ) { struct ir_classld_t* classld; if (class == NULL) - eprint("Null class in classld\n"); + eprintf("Null class in classld\n"); classld = struct_alloc(ir_classld_t); classld->root_class = class; return classld; @@ -538,7 +519,7 @@ void* stack_alloc if (PL_HEADMEM(datapages) < bytes) { datapages.head->header.next = (struct pagenode_t*) calloc(datapages.pagesize,1); if (datapages.head->header.next == NULL) - eprint("Memory allocation error \n"); + eprintf("Memory allocation error \n"); datapages.head = datapages.head->header.next; datapages.head->header.head = datapages.head->root; } @@ -560,7 +541,7 @@ uint8_t* name_alloc if (head_mem == 0) //not enough room { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1); if (namepages.head->header.next == NULL) - eprint("Memory allocation error\n"); + eprintf("Memory allocation error\n"); namepages.head = namepages.head->header.next; namepages.head->header.head = namepages.head->root; goto copy; diff --git a/src/lexer.rl b/src/lexer.rl index 7f65276..c7600ae 100644 --- a/src/lexer.rl +++ b/src/lexer.rl @@ -2,10 +2,10 @@ #include #include #include +#include #include "parser.tab.h" #include "apc.h" -#include -#include +#include "print.h" /* Public */ int lexer_init(void); void lexer_quit(void); @@ -22,12 +22,24 @@ static YYSTYPE lval_stack[0xFF + 1]; static uint8_t lval_offs; +#define $($)#$ #define PUSHTOK(T,L) yypush_parse(pstate, T, (L), cstate) -#define LEXTOK(T,Y,L) do { \ - lval_stack[lval_offs].Y = L; \ - PUSHTOK(T,lval_stack + lval_offs); \ - lval_offs++; \ - ntok++; \ +#define LEXTOK(T,Y,L) do { \ + if (DEBUG) { \ + ulc_fprintf(stdout, "["$(T)); \ + switch (T) { \ + case NAME: case PATH: \ + ulc_fprintf(stdout, "->%U", L); break; \ + case REF: \ + ulc_fprintf(stdout, "->%X", L); break; \ + default: break; \ + } \ + ulc_fprintf(stdout, "]"); \ + } \ + lval_stack[lval_offs].Y = L; \ + PUSHTOK(T,lval_stack + lval_offs); \ + lval_offs++; \ + ntok++; \ } while (0) #define PUSHFACE(F) LEXTOK(FACING, face, F) #define PUSHREF(R) LEXTOK(REF, ref, R) @@ -37,12 +49,6 @@ uint8_t lval_offs; #define PUSHOP(O) LEXTOK(O, val, 0) #define PUSHPATH(P) LEXTOK(PATH, str, P) -#define DEBUG 1 -#define dprintf(_B, ...) do { \ - if(_B) printf(__VA_ARGS__); \ - } while (0) - - /* Lexstring is the main lexer for APC and is generated by ragel. It lexes file names of files that have been scanned and pushes their types and values into the tok_stack, which yyparse eventually calls during parsing. */ @@ -54,7 +60,7 @@ uint8_t lval_offs; action push_ref { errno = 0; lval.ref = strtoll((char*)ts,NULL,16); if (errno) - { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str); + { ulc_fprintf(stderr, "Invalid hex number in file %U\n",str); exit(1); } PUSHREF(lval.ref); @@ -63,51 +69,27 @@ uint8_t lval_offs; action push_val { errno = 0; lval.val = strtoll((char*)ts,NULL,10); if (errno) - { fprintf(stderr, "strtoll could not parse %s\n", (char*)str); + { ulc_fprintf(stderr, "strtoll could not parse %U\n",str); exit(1); } PUSHNUM(lval.val); } - action push_name { dprintf(DEBUG, "Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p); - PUSHNAME(ts); - } - action push_map { dprintf(DEBUG, "Lexer_lexstring:: action:push_map: pushing map token\n"); - PUSHOP(MAP); - } - action set_ts { dprintf(DEBUG, "Lexer_lexstring:: action:set_ts. ts = %s\n", p); - ts = p; } - action push_SS { dprintf(DEBUG, "Lexer_lexstring:: action:push_SS. p = %s\n",p); - PUSHOP(SS); - } - action push_S { dprintf(DEBUG, "Lexer_lexstring:: action:push_S. p = %s\n", p); - PUSHFACE(SFACE); - } - action push_SW { dprintf(DEBUG, "Lexer_lexstring:: action:push_SW. p = %s\n", p); - PUSHFACE(SWFACE); - } - action push_W { dprintf(DEBUG, "Lexer_lexstring:: action:push_W. p = %s\n", p); - PUSHFACE(WFACE); - } - action push_NW { dprintf(DEBUG, "Lexer_lexstring:: action:push_NW. p = %s\n", p); - PUSHFACE(NWFACE); - } - action push_N { dprintf(DEBUG, "Lexer_lexstring:: action:push_N. p = %s\n", p); - PUSHFACE(NFACE); - } - action push_NE { dprintf(DEBUG, "Lexer_lexstring:: action:push_NE. p = %s\n", p); - PUSHFACE(NEFACE); - } - action push_E { dprintf(DEBUG, "Lexer_lexstring:: action:push_N. p = %s\n", p); - PUSHFACE(EFACE); - } - action push_SE { dprintf(DEBUG, "Lexer_lexstring:: action:push_N. p = %s\n", p); - PUSHFACE(SEFACE); - } - action ref_error { dprintf(DEBUG, "ref from %s to %s has an inappropriate amount of hex digits, it must have eight.\n", ts, p); + action push_name { PUSHNAME(ts); } + action push_map { PUSHOP(MAP); } + action set_ts { ts = p; } + action push_SS { PUSHOP(SS); } + action push_S { PUSHFACE(SFACE); } + action push_SW { PUSHFACE(SWFACE); } + action push_W { PUSHFACE(WFACE); } + action push_NW { PUSHFACE(NWFACE); } + action push_N { PUSHFACE(NFACE); } + action push_NE { PUSHFACE(NEFACE); } + action push_E { PUSHFACE(EFACE); } + action push_SE { PUSHFACE(SEFACE); } + action ref_error { ulc_fprintf(stderr, "ref from %U to %U has an inappropriate amount of hex digits, it must have eight.\n", ts, p); exit(1); } - action p { dprintf("Lexer_lexstring:: p = %s\n", p); - } + action p { dprintf("Lexer_lexstring:: p = %U\n", p); } N = 'N' %push_N; W = 'W' %push_W; @@ -143,6 +125,7 @@ int lexer_init cstate = yycstate_new(); lval_offs = 0; return !pstate || !cstate; + return en_main == 1; } void lexer_quit @@ -163,12 +146,12 @@ int lexer_lexstring p = ts = str; pe = eof = p + size + 1; - dprintf("|---Begin lexstring on p = %s, pe = %s.\n", (char*)p, pe); + dprintf("\n|---Begin lexstring on p = %U, pe = %p.---|\n",p, pe); %%write init; %%write exec; - dprintf("Ending lexstring of file %s, pushed %d tokens.\n", (char*)str, ntok); + dprintf("\n|---Ending lexstring of file %U, pushed %d tokens.---|\n",str, ntok); return ntok; } @@ -203,17 +186,13 @@ int lexer_lexfile *iter = '_'; PUSHPATH(filename); return ntok + 1; - return en_main == 1; } int lexer_lexdir ( uint8_t* dirname ) -{ uint8_t* de = dirname; - int ntok; +{ int ntok; ntok = 0; - de = dirname; - if (*de) while (*++de); - ntok = lexer_lexstring(dirname, (int)(de - dirname)); + PUSHNAME(dirname); PUSHOP(CLOPEN); return ntok; } diff --git a/src/parser.y b/src/parser.y index 2ff1638..ff2d257 100644 --- a/src/parser.y +++ b/src/parser.y @@ -33,6 +33,7 @@ #define yyclassld(CS) (ir_classld_from_class(yyclass(CS))) } %define parse.error verbose +%define parse.lac full %define lr.type ielr %define api.pure full %define api.push-pull push @@ -137,7 +138,12 @@ void yyerror ( yycstate* cs, char const *s ) -{ fprintf(stderr, "%s\n", s); } +{ ir_class* iter; + for (iter = cs->class_stack; iter < cs->csp; iter++) + fprintf(stderr, "%s/", ir_class_name(*iter)); + fprintf(stderr, "\n\t"); + fprintf(stderr, "%s\n", s); +} yycstate* yycstate_new ( void ) @@ -155,7 +161,9 @@ yycstate* yycstate_new static inline ir_class yyclass_pop ( yycstate* cs ) -{ return *((cs->csp)--); } +{ cs->csp--; + return *(cs->csp + 1); +} static inline ir_class yyclass_push