Hello TinyCC devs,
I have a patch that I think implements __builtin_unreachable, but I am not very
confident in my methodology. In particular, I was getting a stack leak error
that I only fixed by pushing a 0 via vpushi(0). I am not experienced at
developing compilers at all, so I am not sure what exactly this was doing. I
was under the impression that it's just pushing a 0-valued integer to the
stack, which I think is harmless. In either case, it seems to work, according
to my very simple test case included in the patch. I am curious if anybody can
offer a simpler, cleaner way to do this.
Cheers,
- Jonathan M. Wilbur
diff --git a/tests/builtin_unreachable.c b/tests/builtin_unreachable.c
new file mode 100644
index 00000000..9a524863
--- /dev/null
+++ b/tests/builtin_unreachable.c
@@ -0,0 +1,16 @@
+
+__asm__ ("wumbo:\n\thlt\n");
+
+int f (int c, int v) {
+ if (c)
+ return v;
+ else {
+ asm("jmp wumbo");
+ __builtin_unreachable();
+ // return 0;
+ }
+}
+
+int main () {
+ return f(0, 1);
+}
diff --git a/tccgen.c b/tccgen.c
index a1c7db47..e4b71fcc 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -5664,9 +5664,14 @@ ST_FUNC void unary(void)
break;
case TOK_builtin_expect:
- /* __builtin_expect is a no-op for now */
- parse_builtin_params(0, "ee");
- vpop();
+ /* __builtin_expect is a no-op for now */
+ parse_builtin_params(0, "ee");
+ vpop();
+ break;
+ case TOK_builtin_unreachable:
+ /* __builtin_unreachable is a no-op for now */
+ parse_builtin_params(0, "");
+ vpushi(0); // I don't know why this is necessary.
break;
case TOK_builtin_types_compatible_p:
parse_builtin_params(0, "tt");
_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel