Skip to content

Commit de05b12

Browse files
committed
Change the way xxd generated files are used, so that we do not have to 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
1 parent 7bece4c commit de05b12

29 files changed

+75
-38
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Version 6.6.2
22
- Added warning (-Warray-index) for constant array indices out of bounds
33
- Allow warnings and optimizations to be turned off with a `no-` prefix (so `warn(no-init-vars)` is like `warn(!init-vars)`).
44
- Added some peephole optimizations to remove unnecessary sign extension
5+
- Some internal changes to make recovering from missing xxd easier
56

67
Version 6.6.1
78
- Changed type of `dat` array in C++ output to `unsigned char`

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ $(BUILD)/%.o: %.c
255255
# which is what the sed script will do
256256
#
257257
sys/%.spin.h: sys/%.spin
258-
xxd -i $< | sed 's/\([0-9a-f]\)$$/\0, 0x00/' > $@
258+
xxd -i $< $@
259259
sys/%.bas.h: sys/%.bas
260-
xxd -i $< | sed 's/\([0-9a-f]\)$$/\0, 0x00/' > $@
260+
xxd -i $< $@
261261

262262
#
263263
# automatic dependencies

backends/asm/outasm.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6332,32 +6332,42 @@ EmitBuiltins(IRList *irl)
63326332
{
63336333
if (HUB_CODE) {
63346334
const char *builtin_lmm = NULL;
6335+
size_t builtin_lmm_len = 0;
6336+
63356337
if (gl_p2) {
63366338
if (gl_fcache_size > 0) {
63376339
builtin_lmm = builtin_fcache_p2;
6340+
builtin_lmm_len = strlen(builtin_fcache_p2);
63386341
}
63396342
} else {
63406343
switch(gl_lmm_kind) {
63416344
case LMM_KIND_SLOW:
63426345
builtin_lmm = (const char *)sys_lmm_slow_spin;
6346+
builtin_lmm_len = sys_lmm_slow_spin_len;
63436347
break;
63446348
case LMM_KIND_TRACE:
63456349
builtin_lmm = (const char *)sys_lmm_trace_spin;
6350+
builtin_lmm_len = sys_lmm_trace_spin_len;
63466351
break;
63476352
case LMM_KIND_CACHE:
63486353
builtin_lmm = (const char *)sys_lmm_cache_spin;
6354+
builtin_lmm_len = sys_lmm_cache_spin_len;
63496355
break;
63506356
case LMM_KIND_COMPRESS:
63516357
builtin_lmm = (const char *)sys_lmm_compress_spin;
6358+
builtin_lmm_len = sys_lmm_compress_spin_len;
63526359
break;
63536360
default:
63546361
builtin_lmm = (const char *)sys_lmm_orig_spin;
6362+
builtin_lmm_len = sys_lmm_orig_spin_len;
63556363
break;
63566364
}
63576365
}
63586366
if (builtin_lmm) {
6367+
/* the header files are not zero terminated, so do that here */
63596368
Operand *loop;
6360-
loop = NewOperand(IMM_STRING, builtin_lmm, 0);
6369+
char *lmm_zero_str = strndup(builtin_lmm, builtin_lmm_len);
6370+
loop = NewOperand(IMM_STRING, lmm_zero_str, 0);
63616371
EmitOp1(irl, OPC_LITERAL, loop);
63626372
}
63636373
}

backends/brkdebug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ Flexbuf CompileBrkDebugger(size_t appsize) {
371371
current = D;
372372
D->Lptr = (LexStream *)calloc(sizeof(*systemModule->Lptr), 1);
373373
D->Lptr->flags |= LEXSTREAM_FLAG_NOSRC;
374-
strToLex(D->Lptr, (const char *)sys_p2_brkdebug_spin, "__brkdebug__", LANG_SPIN_SPIN2);
374+
strToLex(D->Lptr, (const char *)sys_p2_brkdebug_spin, sys_p2_brkdebug_spin_len, "__brkdebug__", LANG_SPIN_SPIN2);
375375
spinyyparse();
376376
ProcessModule(D);
377377
// We good now?

backends/nucode/nuir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void CreateBuiltinOp(NuIrOpcode op, const char *impl) {
9898
}
9999

100100
void NuIrInit(NuContext *ctxt) {
101-
const char *ptr = (char *)sys_nuinterp_spin;
101+
const char *ptr = strndup((char *)sys_nuinterp_spin, sys_nuinterp_spin_len);
102102
const char *linestart;
103103
int c;
104104
int i;

frontends/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef enum SpinExprState {
3737
struct lexstream {
3838
void *ptr; /* current pointer */
3939
void *arg; /* original value of pointer */
40+
size_t maxbytes;
4041
int (*getcf)(LexStream *);
4142
#define UNGET_MAX 16 /* we can ungetc this many times */
4243
int ungot[UNGET_MAX];

frontends/lexer.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,12 @@ strgetc(LexStream *L)
125125
{
126126
char *s;
127127
int c;
128-
128+
size_t delta;
129129
s = (char *)L->ptr;
130+
delta = s - (char *)L->arg;
131+
if (delta >= L->maxbytes) {
132+
return EOF;
133+
}
130134
c = (*s++) & 0x00ff;
131135
if (c == '\r') {
132136
c = (*s++) & 0x00ff;
@@ -141,13 +145,14 @@ strgetc(LexStream *L)
141145
}
142146

143147
/* open a stream from a string s */
144-
void strToLex(LexStream *L, const char *s, const char *name, int language)
148+
void strToLex(LexStream *L, const char *s, size_t maxBytes, const char *name, int language)
145149
{
146150
if (!L) {
147151
current->Lptr = L = (LexStream *)malloc(sizeof(*L));
148152
}
149153
memset(L, 0, sizeof(*L));
150154
L->arg = L->ptr = (void *)s;
155+
L->maxbytes = maxBytes;
151156
L->getcf = strgetc;
152157
L->pendingLine = 1;
153158
L->fileName = name ? name : "<string>";

frontends/lexer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern void resetLineState(LexStream *L);
2222
/*
2323
* function to open a lexer stream from a string
2424
*/
25-
void strToLex(LexStream *lex, const char *s, const char *name, int language);
25+
void strToLex(LexStream *lex, const char *s, size_t maxBytes, const char *name, int language);
2626

2727
/*
2828
* function to open a lexer stream from a FILE

spinc.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ InitGlobalModule(void)
107107
int oldtmpnum;
108108
int saveyydebug;
109109
const char *syscode = "";
110+
size_t syscode_len = 0;
110111
int debugVal = gl_debug;
111112
int bcVal = (gl_output == OUTPUT_BYTECODE);
112113

@@ -194,9 +195,11 @@ InitGlobalModule(void)
194195
switch (gl_interp_kind) {
195196
case INTERP_KIND_P1ROM:
196197
syscode = (const char *)sys_bytecode_rom_spin;
198+
syscode_len = sys_bytecode_rom_spin_len;
197199
break;
198200
case INTERP_KIND_NUCODE:
199201
syscode = (const char *)sys_nucode_util_spin;
202+
syscode_len = sys_nucode_util_spin_len;
200203
break;
201204
default:
202205
ERROR(NULL, "No internal code for bytecode type\n");
@@ -205,32 +208,34 @@ InitGlobalModule(void)
205208
} else {
206209
if (gl_p2) {
207210
syscode = (const char *)sys_p2_code_spin;
211+
syscode_len = sys_p2_code_spin_len;
208212
} else {
209213
syscode = (const char *)sys_p1_code_spin;
214+
syscode_len = sys_p1_code_spin_len;
210215
}
211216
}
212217
gl_normalizeIdents = 0;
213218
systemModule->Lptr = (LexStream *)calloc(sizeof(*systemModule->Lptr), 1);
214219
systemModule->Lptr->flags |= LEXSTREAM_FLAG_NOSRC;
215-
strToLex(systemModule->Lptr, syscode, "_system_", LANG_SPIN_SPIN1);
220+
strToLex(systemModule->Lptr, syscode, syscode_len, "_system_", LANG_SPIN_SPIN1);
216221
spinyyparse();
217222

218223
// add common PASM code
219224
if (gl_output != OUTPUT_BYTECODE) {
220-
strToLex(systemModule->Lptr, (const char *)sys_common_pasm_spin, "_common_pasm_", LANG_SPIN_SPIN1);
225+
strToLex(systemModule->Lptr, (const char *)sys_common_pasm_spin, sys_common_pasm_spin_len, "_common_pasm_", LANG_SPIN_SPIN1);
221226
spinyyparse();
222227
}
223-
strToLex(systemModule->Lptr, (const char *)sys_common_spin, "_common_", LANG_SPIN_SPIN1);
228+
strToLex(systemModule->Lptr, (const char *)sys_common_spin, sys_common_spin_len, "_common_", LANG_SPIN_SPIN1);
224229
spinyyparse();
225-
strToLex(systemModule->Lptr, (const char *)sys_float_spin, "_float_", LANG_SPIN_SPIN1);
230+
strToLex(systemModule->Lptr, (const char *)sys_float_spin, sys_float_spin_len, "_float_", LANG_SPIN_SPIN1);
226231
spinyyparse();
227-
strToLex(systemModule->Lptr, (const char *)sys_gcalloc_spin, "_gc_", LANG_SPIN_SPIN1);
232+
strToLex(systemModule->Lptr, (const char *)sys_gcalloc_spin, sys_gcalloc_spin_len, "_gc_", LANG_SPIN_SPIN1);
228233
spinyyparse();
229234
if (gl_output == OUTPUT_BYTECODE) {
230-
strToLex(systemModule->Lptr, (const char *)sys_gc_bytecode_spin, "_platform_", LANG_SPIN_SPIN1);
235+
strToLex(systemModule->Lptr, (const char *)sys_gc_bytecode_spin, sys_gc_bytecode_spin_len, "_platform_", LANG_SPIN_SPIN1);
231236
spinyyparse();
232237
} else {
233-
strToLex(systemModule->Lptr, (const char *)sys_gc_pasm_spin, "_platform_", LANG_SPIN_SPIN1);
238+
strToLex(systemModule->Lptr, (const char *)sys_gc_pasm_spin, sys_gc_pasm_spin_len, "_platform_", LANG_SPIN_SPIN1);
234239
spinyyparse();
235240
}
236241
ProcessModule(systemModule);
@@ -761,7 +766,7 @@ doParseFile(const char *name, Module *P, int *is_dup, AST *paramlist)
761766
parseString = pp_finish(&gl_pp);
762767
pp_restore_define_state(&gl_pp, defineState);
763768
}
764-
strToLex(NULL, parseString, fname, language);
769+
strToLex(NULL, parseString, strlen(parseString), fname, language);
765770
doparse(language);
766771
free(parseString);
767772
} else {

sys/bytecode_rom.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/common.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/common_pasm.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/float.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/gc_bytecode.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/gc_pasm.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/gcalloc.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/lmm_cache.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/lmm_compress.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/lmm_orig.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/lmm_slow.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/lmm_trace.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/nucode_util.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/nuinterp.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/p1_code.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/p2_brkdebug.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/p2_code.spin.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testlex.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ testNumber(const char *str, uint32_t val)
8888
Token t;
8989
int c;
9090
printf("testing number[%s]...", str);
91-
strToLex(&L, str, NULL, LANG_DEFAULT);
91+
strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT);
9292
t = getSpinToken(&L, &ast);
9393
EXPECTEQ(t, SP_NUM);
9494
c = lexgetc(&L);
@@ -115,7 +115,7 @@ testFloat(const char *str, float fval)
115115
val = v.i;
116116

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

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

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

170170
printf("testing tokens [%s]...", str); fflush(stdout);
171-
strToLex(&L, str, NULL, LANG_SPIN_SPIN2);
171+
strToLex(&L, str, strlen(str), NULL, LANG_SPIN_SPIN2);
172172
for (i = 0; i < numtokens; i++) {
173173
t = getSpinToken(&L, &ast);
174174
EXPECTEQ(t, tokens[i]);

util/strdupcat.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ char *strdupcat(const char *a, const char *b)
1010
strcat(c, b);
1111
return c;
1212
}
13+
14+
char *strndup(const char *src, size_t n)
15+
{
16+
char *r = (char *)calloc(1, n+1);
17+
strncpy(r, src, n);
18+
return r;
19+
}

0 commit comments

Comments
 (0)