digitalmars.D.learn - Would appreciate some help porting Q3VM to D (mainly C macros and type
- Mark Bauermeister (12/12) Oct 15 For the past 2 days I've been trying to port the Q3VM
- barbosso (1/1) Oct 15 Are you heard of https://github.com/dkorpel/ctod ?
- Mark Bauermeister (4/5) Oct 15 I actually used CTOD for part of the translation but it wouldn't
- Lance Bachmeier (7/12) Oct 15 It's not as well known, but ImportC also converts C code to D:
- Mark Bauermeister (6/20) Oct 15 Thanks! That helped me figure out some more macros.
- drug007 (3/22) Oct 15 if you mean line 966 you need to replace `enum` by `const` or even
- Mark Bauermeister (2/15) Oct 16 Ah! Makes sense. I always forget about auto. Thanks!
- Mark Bauermeister (6/22) Oct 16 I think I got every compiler complaint fixed now. However, upon
- Mai Lapyst (8/12) Oct 16 Yes, an exitstatus of -11 is an SIGSEGV, like in any other
- Mark Bauermeister (11/24) Oct 18 Yea. I forgot about the sizeof division being wrong. I replaced
- Jordan Wilson (5/10) Oct 18 Is the `switch` correct? I would expect to see case statements
- matheus (20/22) Oct 18 One thing that I can think looking at your code:
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
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
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: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.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
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: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.On Tuesday, 15 October 2024 at 12:27:42 UTC, barbosso wrote: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.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
On 15.10.2024 20:37, Mark Bauermeister wrote:On Tuesday, 15 October 2024 at 16:05:48 UTC, Lance Bachmeier wrote: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.On Tuesday, 15 October 2024 at 13:30:03 UTC, Mark Bauermeister 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.On Tuesday, 15 October 2024 at 12:27:42 UTC, barbosso wrote: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.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
On Tuesday, 15 October 2024 at 17:50:54 UTC, drug007 wrote:On 15.10.2024 20:37, Mark Bauermeister wrote:Ah! Makes sense. I always forget about auto. Thanks!On Tuesday, 15 October 2024 at 16:05:48 UTC, Lance Bachmeier wrote: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.[...]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 16
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: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/442a571353a48c5377c503e7d02570fbOn 15.10.2024 20:37, Mark Bauermeister wrote:Ah! Makes sense. I always forget about auto. Thanks!On Tuesday, 15 October 2024 at 16:05:48 UTC, Lance Bachmeier wrote: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.[...]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 16
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/442a571353a48c5377c503e7d02570fbYes, 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
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: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.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/442a571353a48c5377c503e7d02570fbYes, 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 18
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:Is the `switch` correct? I would expect to see case statements like `case goto_OP_IGNORE: ` etc. etc. Jordan[...]Yea. I forgot about the sizeof division being wrong. I replaced all calls to ARRAY_LEN with \<array>.length. [...]
Oct 18
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