www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Would appreciate some help porting Q3VM to D (mainly C macros and type

reply Mark Bauermeister <bauermeistermarkusdev gmail.com> writes:
For the past 2 days I've been trying to port the Q3VM 
(https://github.com/jnz/q3vm) to D.
While most of the code is converted, it's somewhat riddled with 
cast statements and I'm still having trouble getting it to 
compile without errors.

My conversion can be found here:

https://gist.github.com/markusbkk/442a571353a48c5377c503e7d02570fb

My main problem are the macro functions at line 398 in my code, 
as well as the compiler telling me that "static variable 
`codeImage` cannot be read at compile time".

Would really appreciate some help with this from the D Lords of 
this group.
Oct 15
parent reply barbosso <barb your.io> writes:
Are you heard of https://github.com/dkorpel/ctod ?
Oct 15
parent reply Mark Bauermeister <bauermeistermarkusdev gmail.com> writes:
On Tuesday, 15 October 2024 at 12:27:42 UTC, barbosso wrote:
 Are you heard of https://github.com/dkorpel/ctod ?
I actually used CTOD for part of the translation but it wouldn't translate the macros correctly. It turned them into weird string mixins.
Oct 15
parent reply Lance Bachmeier <no spam.net> writes:
On Tuesday, 15 October 2024 at 13:30:03 UTC, Mark Bauermeister 
wrote:
 On Tuesday, 15 October 2024 at 12:27:42 UTC, barbosso wrote:
 Are you heard of https://github.com/dkorpel/ctod ?
I actually used CTOD for part of the translation but it wouldn't translate the macros correctly. It turned them into weird string mixins.
It's not as well known, but ImportC also converts C code to D: https://dlang.org/spec/importc.html#ctod It needs the -inline switch to output function bodies. The last time I used it, there were problems with generation of duplicate items, but it was able to handle most anything I threw at it. Would be worth a try.
Oct 15
parent reply Mark Bauermeister <bauermeistermarkusdev gmail.com> writes:
On Tuesday, 15 October 2024 at 16:05:48 UTC, Lance Bachmeier 
wrote:
 On Tuesday, 15 October 2024 at 13:30:03 UTC, Mark Bauermeister 
 wrote:
 On Tuesday, 15 October 2024 at 12:27:42 UTC, barbosso wrote:
 Are you heard of https://github.com/dkorpel/ctod ?
I actually used CTOD for part of the translation but it wouldn't translate the macros correctly. It turned them into weird string mixins.
It's not as well known, but ImportC also converts C code to D: https://dlang.org/spec/importc.html#ctod It needs the -inline switch to output function bodies. The last time I used it, there were problems with generation of duplicate items, but it was able to handle most anything I threw at it. Would be worth a try.
Thanks! That helped me figure out some more macros. Now if only I could figure out how to resolve "variable `codeImage` cannot be read at compile time". That's the last thing neither of those automation solutions can help me with.
Oct 15
parent reply drug007 <drug2004 bk.ru> writes:
On 15.10.2024 20:37, Mark Bauermeister wrote:
 On Tuesday, 15 October 2024 at 16:05:48 UTC, Lance Bachmeier wrote:
 On Tuesday, 15 October 2024 at 13:30:03 UTC, Mark Bauermeister wrote:
 On Tuesday, 15 October 2024 at 12:27:42 UTC, barbosso wrote:
 Are you heard of https://github.com/dkorpel/ctod ?
I actually used CTOD for part of the translation but it wouldn't translate the macros correctly. It turned them into weird string mixins.
It's not as well known, but ImportC also converts C code to D: https://dlang.org/spec/importc.html#ctod It needs the -inline switch to output function bodies. The last time I used it, there were problems with generation of duplicate items, but it was able to handle most anything I threw at it. Would be worth a try.
Thanks! That helped me figure out some more macros. Now if only I could figure out how to resolve "variable `codeImage` cannot be read at compile time". That's the last thing neither of those automation solutions can help me with.
if you mean line 966 you need to replace `enum` by `const` or even `auto`. `enum` means the value should be available in compile time.
Oct 15
parent reply Mark Bauermeister <bauermeistermarkusdev gmail.com> writes:
On Tuesday, 15 October 2024 at 17:50:54 UTC, drug007 wrote:
 On 15.10.2024 20:37, Mark Bauermeister wrote:
 On Tuesday, 15 October 2024 at 16:05:48 UTC, Lance Bachmeier 
 wrote:
 [...]
Thanks! That helped me figure out some more macros. Now if only I could figure out how to resolve "variable `codeImage` cannot be read at compile time". That's the last thing neither of those automation solutions can help me with.
if you mean line 966 you need to replace `enum` by `const` or even `auto`. `enum` means the value should be available in compile time.
Ah! Makes sense. I always forget about auto. Thanks!
Oct 16
parent reply Mark Bauermeister <bauermeistermarkusdev gmail.com> writes:
On Wednesday, 16 October 2024 at 12:28:15 UTC, Mark Bauermeister 
wrote:
 On Tuesday, 15 October 2024 at 17:50:54 UTC, drug007 wrote:
 On 15.10.2024 20:37, Mark Bauermeister wrote:
 On Tuesday, 15 October 2024 at 16:05:48 UTC, Lance Bachmeier 
 wrote:
 [...]
Thanks! That helped me figure out some more macros. Now if only I could figure out how to resolve "variable `codeImage` cannot be read at compile time". That's the last thing neither of those automation solutions can help me with.
if you mean line 966 you need to replace `enum` by `const` or even `auto`. `enum` means the value should be available in compile time.
Ah! Makes sense. I always forget about auto. Thanks!
I think I got every compiler complaint fixed now. However, upon launch the program immediately quits with "Error Program exited with code -11". I assume that's a segfault, right? https://gist.github.com/markusbkk/442a571353a48c5377c503e7d02570fb
Oct 16
parent reply Mai Lapyst <mai lapyst.by> writes:
On Wednesday, 16 October 2024 at 13:10:34 UTC, Mark Bauermeister 
wrote:
 I think I got every compiler complaint fixed now. However, upon 
 launch the program immediately quits with "Error Program exited 
 with code -11". I assume that's a segfault, right?

 https://gist.github.com/markusbkk/442a571353a48c5377c503e7d02570fb
Yes, an exitstatus of -11 is an SIGSEGV, like in any other unix/linux program. One thing that you might wanna change is your ARRAY_LEN function: D already supports getting the length of an array with the `.length` property on arrays. Also the `x.sizeof` divided through itself will always only yield `1`.
Oct 16
parent reply Mark Bauermeister <bauermeistermarkusdev gmail.com> writes:
On Thursday, 17 October 2024 at 02:47:03 UTC, Mai Lapyst wrote:
 On Wednesday, 16 October 2024 at 13:10:34 UTC, Mark 
 Bauermeister wrote:
 I think I got every compiler complaint fixed now. However, 
 upon launch the program immediately quits with "Error Program 
 exited with code -11". I assume that's a segfault, right?

 https://gist.github.com/markusbkk/442a571353a48c5377c503e7d02570fb
Yes, an exitstatus of -11 is an SIGSEGV, like in any other unix/linux program. One thing that you might wanna change is your ARRAY_LEN function: D already supports getting the length of an array with the `.length` property on arrays. Also the `x.sizeof` divided through itself will always only yield `1`.
Yea. I forgot about the sizeof division being wrong. I replaced all calls to ARRAY_LEN with \<array>.length. No more segfaults thus far but strangely the proper switch case (https://gist.github.com/markusbkk/442a571353a48c5377c503e7d02570fb file-dq3vm-d-L1200) is never hit. Instead, it goes straight to the default (https://gist.github.com/markusbkk/442a571353a48c5377c503e7d02570fb file-dq3vm-d-L1055) even though there's a matching case. When I `printf("%i", opcode)` inside the default case it correctly prints "3" which is `goto_OP_ENTER`, so I don't quite understand why it won't hit the correct branch. I tried adding the default case to the very bottom of the switch/case chain too, thinking maybe this is somehow sequential. But to no avail.
Oct 18
next sibling parent Jordan Wilson <wilsonjord gmail.com> writes:
On Friday, 18 October 2024 at 14:41:15 UTC, Mark Bauermeister 
wrote:
 On Thursday, 17 October 2024 at 02:47:03 UTC, Mai Lapyst wrote:
 [...]
Yea. I forgot about the sizeof division being wrong. I replaced all calls to ARRAY_LEN with \<array>.length. [...]
Is the `switch` correct? I would expect to see case statements like `case goto_OP_IGNORE: ` etc. etc. Jordan
Oct 18
prev sibling parent matheus <matheus gmail.com> writes:
On Friday, 18 October 2024 at 14:41:15 UTC, Mark Bauermeister 
wrote:
 ...No more segfaults thus far but strangely the proper switch 
 case...
One thing that I can think looking at your code: import std.stdio; void main(){ int op = 1; switch(op){ default: writeln("default"); return; version(XYZ){ case 1: writeln(op); break; } case 2: writeln(op); } } Since XYZ is never set, case 1 will never hit and it will print "default". if you comment or take "version(XYZ)" out there it will work. Code: https://run.dlang.io/?compiler=dmd&args=-unittest&source=void+main%28%29%0A%7B%0A++++import+std.stdio+%3A+writefln%3B%0A++++import+std.algorithm.sorting+%3A+sort%3B%0A++++import+std.range+%3A+chain%3B%0A%0A++++int%5B%5D+arr1+%3D+%5B4%2C+9%2C+7%5D%3B%0A++++int%5B%5D+arr2+%3D+%5B5%2C+2%2C+1%2C+10%5D%3B%0A++++int%5B%5D+arr3+%3D+%5B6%2C+8%2C+3%5D%3B%0A++++%2F%2F+%40nogc+functions+are+guaranteed+by+the+compiler%0A++++%2F%2F+to+be+without+any+GC+allocation%0A++++%28%29+%40nogc+%7B%0A++++++++sort%28chain%28arr1%2C+arr2%2C+arr3%29%29%3B%0A++++%7D%28%29%3B%0A++++writefln%28%22%25s%5Cn%25s%5Cn%25s%5Cn%22%2C+arr1%2C+arr2%2C+arr3%29%3B%0A%7D%0A%0A Matheus.
Oct 18