Skip to content

Commit 1ea5177

Browse files
committed
compiler: Support TCC
This requires making some constants actually constant (per the C standard), not just the subtle variants allowed by Clang and GCC.
1 parent 07a0318 commit 1ea5177

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ jobs:
4242
run: sudo apt update
4343
- name: Install Python
4444
run: sudo apt install --yes ${{matrix.PYTHON}}
45+
- name: Install TCC
46+
run: sudo apt install --yes tcc
4547
- name: Run interpreter tests
4648
run: ${{matrix.PYTHON}} scrapscript.py test
4749
- name: Run compiler tests
4850
run: ${{matrix.PYTHON}} compiler_tests.py
51+
- name: Run compiler tests with TCC
52+
run: CC=tcc ${{matrix.PYTHON}} compiler_tests.py
4953
build_docker_image:
5054
runs-on: ubuntu-latest
5155
permissions:

compiler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,10 @@ def compile_match_function(self, env: Env, exp: MatchFunction, name: Optional[st
219219
case_result = self.compile({**funcenv, **env_updates}, case.body)
220220
self._emit(f"return {case_result};")
221221
self._emit(f"{fallthrough}:;")
222-
# TODO(max): (non-fatal?) exceptions
223222
self._emit(r'fprintf(stderr, "no matching cases\n");')
224223
self._emit("abort();")
224+
# Pacify the C compiler
225+
self._emit("return NULL;")
225226
self.function = cur
226227
return self.make_closure(env, fn)
227228

@@ -387,6 +388,9 @@ def compile_to_string(source: str, memory: int, debug: bool) -> str:
387388
for key, idx in compiler.variant_tags.items():
388389
print(f"Tag_{key} = {idx},", file=f)
389390
print("};", file=f)
391+
else:
392+
# Pacify the C compiler
393+
print("const char* variant_names[] = { NULL };", file=f)
390394
for function in compiler.functions:
391395
print(function.decl() + ";", file=f)
392396
for builtin in builtins:

runtime.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ struct object {};
3030
// The three low bits make up a primary tag, used to differentiate gc_obj
3131
// from immediate objects. All even tags map to SmallInt, which is
3232
// optimized by checking only the lowest bit for parity.
33-
static const uword kSmallIntTagBits = 1;
34-
static const uword kPrimaryTagBits = 3;
35-
static const uword kImmediateTagBits = 5;
33+
enum {
34+
kSmallIntTagBits = 1,
35+
kPrimaryTagBits = 3,
36+
kImmediateTagBits = 5,
37+
kWordSize = sizeof(word),
38+
kBitsPerByte = 8,
39+
kBitsPerPointer = kBitsPerByte * kWordSize,
40+
};
3641
static const uword kSmallIntTagMask = (1 << kSmallIntTagBits) - 1;
3742
static const uword kPrimaryTagMask = (1 << kPrimaryTagBits) - 1;
3843
static const uword kImmediateTagMask = (1 << kImmediateTagBits) - 1;
3944

40-
const int kWordSize = sizeof(word);
4145
static const word kMaxSmallStringLength = kWordSize - 1;
42-
const int kBitsPerByte = 8;
4346

4447
static const uword kSmallIntTag = 0; // 0b****0
4548
static const uword kHeapObjectTag = 1; // 0b**001
@@ -309,6 +312,8 @@ size_t heap_object_size(struct gc_obj* obj) {
309312
default:
310313
fprintf(stderr, "unknown tag: %lu\n", obj->tag);
311314
abort();
315+
// Pacify the C compiler
316+
return (size_t)-1;
312317
}
313318
}
314319

@@ -341,8 +346,9 @@ size_t trace_heap_object(struct gc_obj* obj, struct gc_heap* heap,
341346
return heap_object_size(obj);
342347
}
343348

344-
const int kBitsPerPointer = kBitsPerByte * kWordSize;
345-
static const word kSmallIntBits = kBitsPerPointer - kSmallIntTagBits;
349+
enum {
350+
kSmallIntBits = kBitsPerPointer - kSmallIntTagBits,
351+
};
346352
static const word kSmallIntMinValue = -(((word)1) << (kSmallIntBits - 1));
347353
static const word kSmallIntMaxValue = (((word)1) << (kSmallIntBits - 1)) - 1;
348354

0 commit comments

Comments
 (0)