Skip to content

Commit

Permalink
Change the way xxd generated files are used, so that we do not have t…
Browse files Browse the repository at this point in the history
…o append a 0 to them (and thus can use xxd itself to generate the file rather than piping through sed). The advantage is that if xxd is missing the build fails cleanly with no 0 length files left behind
  • Loading branch information
totalspectrum committed Nov 7, 2023
1 parent 7bece4c commit de05b12
Show file tree
Hide file tree
Showing 29 changed files with 75 additions and 38 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Version 6.6.2
- Added warning (-Warray-index) for constant array indices out of bounds
- Allow warnings and optimizations to be turned off with a `no-` prefix (so `warn(no-init-vars)` is like `warn(!init-vars)`).
- Added some peephole optimizations to remove unnecessary sign extension
- Some internal changes to make recovering from missing xxd easier

Version 6.6.1
- Changed type of `dat` array in C++ output to `unsigned char`
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ $(BUILD)/%.o: %.c
# which is what the sed script will do
#
sys/%.spin.h: sys/%.spin
xxd -i $< | sed 's/\([0-9a-f]\)$$/\0, 0x00/' > $@
xxd -i $< $@
sys/%.bas.h: sys/%.bas
xxd -i $< | sed 's/\([0-9a-f]\)$$/\0, 0x00/' > $@
xxd -i $< $@

#
# automatic dependencies
Expand Down
12 changes: 11 additions & 1 deletion backends/asm/outasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6332,32 +6332,42 @@ EmitBuiltins(IRList *irl)
{
if (HUB_CODE) {
const char *builtin_lmm = NULL;
size_t builtin_lmm_len = 0;

if (gl_p2) {
if (gl_fcache_size > 0) {
builtin_lmm = builtin_fcache_p2;
builtin_lmm_len = strlen(builtin_fcache_p2);
}
} else {
switch(gl_lmm_kind) {
case LMM_KIND_SLOW:
builtin_lmm = (const char *)sys_lmm_slow_spin;
builtin_lmm_len = sys_lmm_slow_spin_len;
break;
case LMM_KIND_TRACE:
builtin_lmm = (const char *)sys_lmm_trace_spin;
builtin_lmm_len = sys_lmm_trace_spin_len;
break;
case LMM_KIND_CACHE:
builtin_lmm = (const char *)sys_lmm_cache_spin;
builtin_lmm_len = sys_lmm_cache_spin_len;
break;
case LMM_KIND_COMPRESS:
builtin_lmm = (const char *)sys_lmm_compress_spin;
builtin_lmm_len = sys_lmm_compress_spin_len;
break;
default:
builtin_lmm = (const char *)sys_lmm_orig_spin;
builtin_lmm_len = sys_lmm_orig_spin_len;
break;
}
}
if (builtin_lmm) {
/* the header files are not zero terminated, so do that here */
Operand *loop;
loop = NewOperand(IMM_STRING, builtin_lmm, 0);
char *lmm_zero_str = strndup(builtin_lmm, builtin_lmm_len);
loop = NewOperand(IMM_STRING, lmm_zero_str, 0);
EmitOp1(irl, OPC_LITERAL, loop);
}
}
Expand Down
2 changes: 1 addition & 1 deletion backends/brkdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ Flexbuf CompileBrkDebugger(size_t appsize) {
current = D;
D->Lptr = (LexStream *)calloc(sizeof(*systemModule->Lptr), 1);
D->Lptr->flags |= LEXSTREAM_FLAG_NOSRC;
strToLex(D->Lptr, (const char *)sys_p2_brkdebug_spin, "__brkdebug__", LANG_SPIN_SPIN2);
strToLex(D->Lptr, (const char *)sys_p2_brkdebug_spin, sys_p2_brkdebug_spin_len, "__brkdebug__", LANG_SPIN_SPIN2);
spinyyparse();
ProcessModule(D);
// We good now?
Expand Down
2 changes: 1 addition & 1 deletion backends/nucode/nuir.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static void CreateBuiltinOp(NuIrOpcode op, const char *impl) {
}

void NuIrInit(NuContext *ctxt) {
const char *ptr = (char *)sys_nuinterp_spin;
const char *ptr = strndup((char *)sys_nuinterp_spin, sys_nuinterp_spin_len);
const char *linestart;
int c;
int i;
Expand Down
1 change: 1 addition & 0 deletions frontends/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef enum SpinExprState {
struct lexstream {
void *ptr; /* current pointer */
void *arg; /* original value of pointer */
size_t maxbytes;
int (*getcf)(LexStream *);
#define UNGET_MAX 16 /* we can ungetc this many times */
int ungot[UNGET_MAX];
Expand Down
9 changes: 7 additions & 2 deletions frontends/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ strgetc(LexStream *L)
{
char *s;
int c;

size_t delta;
s = (char *)L->ptr;
delta = s - (char *)L->arg;
if (delta >= L->maxbytes) {
return EOF;
}
c = (*s++) & 0x00ff;
if (c == '\r') {
c = (*s++) & 0x00ff;
Expand All @@ -141,13 +145,14 @@ strgetc(LexStream *L)
}

/* open a stream from a string s */
void strToLex(LexStream *L, const char *s, const char *name, int language)
void strToLex(LexStream *L, const char *s, size_t maxBytes, const char *name, int language)
{
if (!L) {
current->Lptr = L = (LexStream *)malloc(sizeof(*L));
}
memset(L, 0, sizeof(*L));
L->arg = L->ptr = (void *)s;
L->maxbytes = maxBytes;
L->getcf = strgetc;
L->pendingLine = 1;
L->fileName = name ? name : "<string>";
Expand Down
2 changes: 1 addition & 1 deletion frontends/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern void resetLineState(LexStream *L);
/*
* function to open a lexer stream from a string
*/
void strToLex(LexStream *lex, const char *s, const char *name, int language);
void strToLex(LexStream *lex, const char *s, size_t maxBytes, const char *name, int language);

/*
* function to open a lexer stream from a FILE
Expand Down
21 changes: 13 additions & 8 deletions spinc.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ InitGlobalModule(void)
int oldtmpnum;
int saveyydebug;
const char *syscode = "";
size_t syscode_len = 0;
int debugVal = gl_debug;
int bcVal = (gl_output == OUTPUT_BYTECODE);

Expand Down Expand Up @@ -194,9 +195,11 @@ InitGlobalModule(void)
switch (gl_interp_kind) {
case INTERP_KIND_P1ROM:
syscode = (const char *)sys_bytecode_rom_spin;
syscode_len = sys_bytecode_rom_spin_len;
break;
case INTERP_KIND_NUCODE:
syscode = (const char *)sys_nucode_util_spin;
syscode_len = sys_nucode_util_spin_len;
break;
default:
ERROR(NULL, "No internal code for bytecode type\n");
Expand All @@ -205,32 +208,34 @@ InitGlobalModule(void)
} else {
if (gl_p2) {
syscode = (const char *)sys_p2_code_spin;
syscode_len = sys_p2_code_spin_len;
} else {
syscode = (const char *)sys_p1_code_spin;
syscode_len = sys_p1_code_spin_len;
}
}
gl_normalizeIdents = 0;
systemModule->Lptr = (LexStream *)calloc(sizeof(*systemModule->Lptr), 1);
systemModule->Lptr->flags |= LEXSTREAM_FLAG_NOSRC;
strToLex(systemModule->Lptr, syscode, "_system_", LANG_SPIN_SPIN1);
strToLex(systemModule->Lptr, syscode, syscode_len, "_system_", LANG_SPIN_SPIN1);
spinyyparse();

// add common PASM code
if (gl_output != OUTPUT_BYTECODE) {
strToLex(systemModule->Lptr, (const char *)sys_common_pasm_spin, "_common_pasm_", LANG_SPIN_SPIN1);
strToLex(systemModule->Lptr, (const char *)sys_common_pasm_spin, sys_common_pasm_spin_len, "_common_pasm_", LANG_SPIN_SPIN1);
spinyyparse();
}
strToLex(systemModule->Lptr, (const char *)sys_common_spin, "_common_", LANG_SPIN_SPIN1);
strToLex(systemModule->Lptr, (const char *)sys_common_spin, sys_common_spin_len, "_common_", LANG_SPIN_SPIN1);
spinyyparse();
strToLex(systemModule->Lptr, (const char *)sys_float_spin, "_float_", LANG_SPIN_SPIN1);
strToLex(systemModule->Lptr, (const char *)sys_float_spin, sys_float_spin_len, "_float_", LANG_SPIN_SPIN1);
spinyyparse();
strToLex(systemModule->Lptr, (const char *)sys_gcalloc_spin, "_gc_", LANG_SPIN_SPIN1);
strToLex(systemModule->Lptr, (const char *)sys_gcalloc_spin, sys_gcalloc_spin_len, "_gc_", LANG_SPIN_SPIN1);
spinyyparse();
if (gl_output == OUTPUT_BYTECODE) {
strToLex(systemModule->Lptr, (const char *)sys_gc_bytecode_spin, "_platform_", LANG_SPIN_SPIN1);
strToLex(systemModule->Lptr, (const char *)sys_gc_bytecode_spin, sys_gc_bytecode_spin_len, "_platform_", LANG_SPIN_SPIN1);
spinyyparse();
} else {
strToLex(systemModule->Lptr, (const char *)sys_gc_pasm_spin, "_platform_", LANG_SPIN_SPIN1);
strToLex(systemModule->Lptr, (const char *)sys_gc_pasm_spin, sys_gc_pasm_spin_len, "_platform_", LANG_SPIN_SPIN1);
spinyyparse();
}
ProcessModule(systemModule);
Expand Down Expand Up @@ -761,7 +766,7 @@ doParseFile(const char *name, Module *P, int *is_dup, AST *paramlist)
parseString = pp_finish(&gl_pp);
pp_restore_define_state(&gl_pp, defineState);
}
strToLex(NULL, parseString, fname, language);
strToLex(NULL, parseString, strlen(parseString), fname, language);
doparse(language);
free(parseString);
} else {
Expand Down
2 changes: 1 addition & 1 deletion sys/bytecode_rom.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/common.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/common_pasm.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/float.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/gc_bytecode.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/gc_pasm.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/gcalloc.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/lmm_cache.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/lmm_compress.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/lmm_orig.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/lmm_slow.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/lmm_trace.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/nucode_util.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/nuinterp.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/p1_code.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/p2_brkdebug.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/p2_code.spin.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions testlex.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ testNumber(const char *str, uint32_t val)
Token t;
int c;
printf("testing number[%s]...", str);
strToLex(&L, str, NULL, LANG_DEFAULT);
strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT);
t = getSpinToken(&L, &ast);
EXPECTEQ(t, SP_NUM);
c = lexgetc(&L);
Expand All @@ -115,7 +115,7 @@ testFloat(const char *str, float fval)
val = v.i;

printf("testing number[%s]...", str);
strToLex(&L, str, NULL, LANG_DEFAULT);
strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT);
t = getSpinToken(&L, &ast);
EXPECTEQ(t, SP_FLOATNUM);
c = lexgetc(&L);
Expand All @@ -133,7 +133,7 @@ testIdentifier(const char *str, const char *expect)
AST *ast;
Token t;

strToLex(&L, str, NULL, LANG_DEFAULT);
strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT);
t = getSpinToken(&L, &ast);
EXPECTEQ(t, SP_IDENTIFIER);
assert(ast != NULL);
Expand All @@ -151,7 +151,7 @@ testTokenStream(const char *str, int *tokens, int numtokens)
Token t;

printf("testing tokens [%s]...", str); fflush(stdout);
strToLex(&L, str, NULL, LANG_DEFAULT);
strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT);
for (i = 0; i < numtokens; i++) {
t = getSpinToken(&L, &ast);
EXPECTEQ(t, tokens[i]);
Expand All @@ -168,7 +168,7 @@ testTokenStream2(const char *str, int *tokens, int numtokens)
Token t;

printf("testing tokens [%s]...", str); fflush(stdout);
strToLex(&L, str, NULL, LANG_SPIN_SPIN2);
strToLex(&L, str, strlen(str), NULL, LANG_SPIN_SPIN2);
for (i = 0; i < numtokens; i++) {
t = getSpinToken(&L, &ast);
EXPECTEQ(t, tokens[i]);
Expand Down
7 changes: 7 additions & 0 deletions util/strdupcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ char *strdupcat(const char *a, const char *b)
strcat(c, b);
return c;
}

char *strndup(const char *src, size_t n)
{
char *r = (char *)calloc(1, n+1);
strncpy(r, src, n);
return r;
}
Loading

0 comments on commit de05b12

Please sign in to comment.