Issue 24291 - ImportC: support computed goto
Summary: ImportC: support computed goto
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL: https://forum.dlang.org/post/woaixick...
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-21 00:30 UTC by mw
Modified: 2023-12-30 05:29 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description mw 2023-12-21 00:30:23 UTC
https://github.com/janet-lang/janet/releases/download/v1.32.1/janet.c
https://github.com/janet-lang/janet/releases/download/v1.32.1/janet.h
https://github.com/janet-lang/janet/releases/download/v1.32.1/shell.c


$ dmd --version
DMD64 D Compiler v2.106.0-beta.1
Copyright (C) 1999-2023 by The D Language Foundation, All Rights Reserved written by Walter Bright


$ dmd -P-I. -c janet.c 
src/core/vm.c(342): Error: expression expected, not `&&`
src/core/vm.c(342): Error: found `label_JOP_NOOP` when expecting `}`
src/core/vm.c(343): Error: identifier or `(` expected
src/core/vm.c(597): Error: `=`, `;` or `,` expected to end declaration instead of `}`
src/core/vm.c(643): Error: identifier expected following `goto`
src/core/vm.c(643): Error: found `*` when expecting `;` following `goto` statement
src/core/vm.c(650): Error: identifier expected following `goto`
src/core/vm.c(650): Error: found `*` when expecting `;` following `goto` statement
src/core/vm.c(657): Error: identifier expected following `goto`
src/core/vm.c(657): Error: found `*` when expecting `;` following `goto` statement
src/core/vm.c(666): Error: identifier expected following `goto`
src/core/vm.c(666): Error: found `*` when expecting `;` following `goto` statement
src/core/vm.c(676): Error: identifier expected following `goto`
src/core/vm.c(676): Error: found `*` when expecting `;` following `goto` statement
src/core/vm.c(680): Error: identifier expected following `goto`
src/core/vm.c(680): Error: found `*` when expecting `;` following `goto` statement
src/core/vm.c(680): Error: identifier expected following `goto`
src/core/vm.c(680): Error: found `*` when expecting `;` following `goto` statement
src/core/vm.c(683): Error: identifier expected following `goto`
src/core/vm.c(683): Error: found `*` when expecting `;` following `goto` statement


$ dmd -P-I. -c shell.c 
shell.c(657): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(657): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(659): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(681): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(681): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(681): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(681): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(681): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(764): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(778): Error: undefined identifier `isnan`, did you mean function `nan`?
shell.c(780): Error: undefined identifier `isnan`, did you mean function `nan`?
Comment 1 Walter Bright 2023-12-24 06:49:38 UTC
Can you please try reducing this to the offending few lines of code? I don't have src/core/vm.c, and probably not exactly the same .h files you're using, either.
Comment 2 anonymous4 2023-12-25 14:20:56 UTC
From forum discussion:
---
One Problem with Janet is that it uses computed gotos which is a 
gcc extension
The code it complains about looks like:
```C
     /* opcode -> label lookup if using clang/GCC */
#ifdef JANET_USE_COMPUTED_GOTOS
     static void *op_lookup[255] = {
         &&label_JOP_NOOP,
         &&label_JOP_ERROR,
```

As you can see &&label is not valid C11.
Which is what the import C parser complains about.
---

Maybe something can be done there with that JANET_USE_COMPUTED_GOTOS define.
Comment 3 anonymous4 2023-12-25 14:43:38 UTC
clang provides __has_extension function for such checks, but doesn't document computed gotos.
Comment 4 Walter Bright 2023-12-27 20:05:13 UTC
If you can post the definition of JANET_USE_COMPUTED_GOTOS we can see about making it evaluate to false.
Comment 5 Daniel 2023-12-27 21:23:14 UTC
#if defined(__GNUC__) && !defined(__EMSCRIPTEN__)
#define JANET_USE_COMPUTED_GOTOS
#endif
Comment 6 anonymous4 2023-12-29 11:09:23 UTC
Oh, __GNUC__ means gnu C dialect?

--- cjanet.c ---
#undef __GNUC__
#include "janet.c"
---
dmd -P-I. -c cjanet.c

Does this work?
Comment 7 Walter Bright 2023-12-30 04:47:28 UTC
(In reply to Daniel from comment #5)
> #if defined(__GNUC__) && !defined(__EMSCRIPTEN__)
> #define JANET_USE_COMPUTED_GOTOS
> #endif

Unfortunately, if dmd #define'd __EMSCRIPTEN__ it would likely turn on some other nutburger extensions ImportC doesn't support. importc.h cannot #undef JANET_USE_COMPUTED_GOTOS because importc.h is #include'd first.

Turning off __GNUC__ will cause all kinds of other problems.
Comment 8 Walter Bright 2023-12-30 05:20:10 UTC
I'm going to leave this as an enhancement request for computed goto. If there are other ImportC problems with janet, please post a bugzilla issue about it!

https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
Comment 9 Walter Bright 2023-12-30 05:29:35 UTC
At least I can get you a better error message:

https://github.com/dlang/dmd/pull/15967