Re: betterC question
On Thursday, 19 November 2020 at 14:34:38 UTC, Adam D. Ruppe wrote: On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu Majumdar wrote: Okay thanks. Bad idea IMO. That's kinda how I see C taking the address of various things implicitly. To be honest it seems irrelevant what C does.
Re: betterC question
On Thursday, 19 November 2020 at 09:23:25 UTC, Jacob Carlborg wrote: Yes, calling `writeln` like that is a bad idea. That was a bad example. But the actual reason is, this is how D implements properties [1]. Any function that doesn't take an argument can be called without parentheses. Any function which takes a single argument can be called like setting a field. I think that properties on an object are a special case - but treating an random function identifier as callable is still bad.
Re: betterC question
On Thursday, 19 November 2020 at 01:42:16 UTC, Mike Parker wrote: On Thursday, 19 November 2020 at 00:20:50 UTC, Dibyendu Majumdar wrote: On Thursday, 19 November 2020 at 00:18:54 UTC, rikki cattermole wrote: You don't need the brackets to call a function (and with a little help from UFCS): void main() { import std.stdio; "Hello!".writeln; writeln; } Okay thanks. Bad idea IMO. Imagine what range pipelines would look like without it. This is one of my favorite D features. Well Java and C# have streams and it looks perfectly fine without this kind of syntax.
Re: betterC question
On Thursday, 19 November 2020 at 00:18:54 UTC, rikki cattermole wrote: You don't need the brackets to call a function (and with a little help from UFCS): void main() { import std.stdio; "Hello!".writeln; writeln; } Okay thanks. Bad idea IMO.
Re: betterC question
On Thursday, 19 November 2020 at 00:08:59 UTC, Adam D. Ruppe wrote: On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu Majumdar wrote: int function() fp = test; You want to get the address. Okay that works. Thanks
Re: betterC question
On Thursday, 19 November 2020 at 00:08:59 UTC, Adam D. Ruppe wrote: On Thursday, 19 November 2020 at 00:07:12 UTC, Dibyendu Majumdar wrote: int function() fp = test; This tries to *call* the function test and assign its return value to fp. Really? why does it do that? You want to get the address.
betterC question
I have simple test program: import core.stdc.stdio : printf; void test() { int* a; printf("a == null %d\n", a == null); } int function() fp = test; extern (C) void main() { fp(); } Why do I get: \d\dmd-2.092.1\windows\bin64\dmd.exe -betterC tests.d tests.d(5): Error: printf cannot be interpreted at compile time, because it has no available source code This is on Windows
Re: Trying to understand a simple piece of code: dmd barray
On Saturday, 7 March 2020 at 14:33:29 UTC, Steven Schveighoffer wrote: It's D's version of implicit conversion. You can make the alias this a no-arg function and it will try calling that function. Okay thank you.
Re: Trying to understand a simple piece of code: dmd barray
On Saturday, 7 March 2020 at 12:26:32 UTC, drug wrote: I am trying to understand https://github.com/dlang/dmd/blob/master/src/dmd/backend/barray.d. Two questions: 1. What does this mean and why is it needed? line 95: alias array this; This means that `array` can be used instead of `this` Hmm should not that change the meaning of this throughout the struct? is this good practice? Thank you
Trying to understand a simple piece of code: dmd barray
Hi, I am trying to understand https://github.com/dlang/dmd/blob/master/src/dmd/backend/barray.d. Two questions: 1. What does this mean and why is it needed? line 95: alias array this; 2. The struct has no property called length - but this is referenced. Where does this come from? Thank you Regards
Re: Inline assembly question
On Monday, 13 November 2017 at 18:40:42 UTC, Basile B. wrote: TBH I wonder if this is not worth a enhancement (or even a DIP) to have in asm blocks a special alias syntax... { asm { version(...) { alias First = RDI; alias Second = RSI; // ... } else { alias First = RCX; alias Second = RDX; } mov First, Second; call aFunctionWithOneParam; // called with 2nd parent param as 1st param } } since the whole mixin solution make the custom asm unreadable just because of this problem. Hi, that would be nice but I won't be holding my breath for such a feature to appear. I have a simple solution - I will just run it past a C pre-processor or maybe a custom one. Regards Dibyendu
Re: Inline assembly question
On Sunday, 12 November 2017 at 22:40:06 UTC, Dibyendu Majumdar wrote: On Sunday, 12 November 2017 at 22:00:58 UTC, Basile B. wrote: no in naked mode you have to save and restore by hand. So how does one manually generate the .pdata and .xdata sections? Are you saying that this is what I would need to do? Another question - how can I tell DMD to no generate the frame pointer? Hi, any further info on this? I am not talking here of the assembly push/pop instructions, rather the .pdata and .xdata sections needed on Win64. Thanks and Regards Dibyendu
Re: Inline assembly question
On Sunday, 12 November 2017 at 22:24:08 UTC, Basile B. wrote: On Sunday, 12 November 2017 at 22:20:46 UTC, Dibyendu Majumdar wrote: On Sunday, 12 November 2017 at 22:00:58 UTC, Basile B. wrote: On Sunday, 12 November 2017 at 21:27:28 UTC, Dibyendu Majumdar I am not sure I have understood above; will DMD generate the right Win64 unwind info for this contrived example: no in naked mode you have to save and restore by hand. So how does one manually generate the .pdata and .xdata sections? Are you saying that this is what I would need to do? Another question - how can I tell DMD to no generate the frame pointer? Thanks for answering my questions. Regards Dibyendu
Re: Inline assembly question
On Sunday, 12 November 2017 at 22:00:58 UTC, Basile B. wrote: On Sunday, 12 November 2017 at 21:27:28 UTC, Dibyendu Majumdar Does the compiler generate appropriate unwind information on Win64? Prsumably if a function is marked 'naked' then it doesn't? yeah about stack frame..., also don't forget to mark the asm block "pure nothrow" if possible... It's not documented but the syntax is like that: ``` void foo() { asm pure nothrow { naked; ret; } } ``` I am not sure I have understood above; will DMD generate the right Win64 unwind info for this contrived example: int luaV_interp(lua_State *L) { asm pure nothrow { naked; push RDI; push RSI; push RBX; push R12; push R13; push R14; push R15; sub RSP, 5*8; mov RAX, 0; add RSP, 5*8; pop R15; pop R14; pop R13; pop R12; pop RBX; pop RSI; pop RDI; pop RBP; ret; } }
Re: Inline assembly question
On Sunday, 12 November 2017 at 18:48:02 UTC, Eugene Wissner wrote: https://dlang.org/spec/iasm.html#agregate_member_offsets aggregate.member.offsetof[someregister] Sorry I didn't phrase my question accurately. Presumably to use above with the mnemonics I would need additional mixin templates where the aggregate type and member etc would need to be parameters? You can use just string parameters instead of enums, then you can pass arbitrary arguments to the instructions. The compiler will tell you if something is wrong with the syntax of the generated assembly. Okay thank you. Sigh. It would be so much simpler to be able to just define mnemonics for registers. Anyway, another question: Does the compiler generate appropriate unwind information on Win64? Prsumably if a function is marked 'naked' then it doesn't? Thanks and Regards Dibyendu
Re: Inline assembly question
On Sunday, 12 November 2017 at 12:32:09 UTC, Basile B. wrote: On Sunday, 12 November 2017 at 12:17:51 UTC, Dibyendu Majumdar wrote: On Sunday, 12 November 2017 at 11:55:23 UTC, Eugene Wissner wrote: [...] Thank you - I probably could use something like this. It is uglier than the simpler approach in dynasm of course. How about when I need to combine this with some struct/union access? In dynasm I can write: | mov BASE, CI->u.l.base // BASE = ci->u.l.base (volatile) | mov PC, CI->u.l.savedpc// PC = CI->u.l.savedpc How can I mix the mixin above and combine with struct offsets? https://dlang.org/spec/iasm.html#agregate_member_offsets aggregate.member.offsetof[someregister] Sorry I didn't phrase my question accurately. Presumably to use above with the mnemonics I would need additional mixin templates where the aggregate type and member etc would need to be parameters?
Re: Inline assembly question
On Sunday, 12 November 2017 at 11:55:23 UTC, Eugene Wissner wrote: On Sunday, 12 November 2017 at 11:01:39 UTC, Dibyendu Majumdar wrote: I have recently started work on building a VM for Lua (actually a derivative of Lua) in X86-64 assembly. I am using the dynasm tool that is part of LuaJIT. I was wondering whether I could also write this in D's inline assembly perhaps, but there is one aspect that I am not sure how to do. The assembly code uses static allocation of registers, but because of the differences in how registers are used in Win64 versus Unix X64 - different registers are assigned depending on the architecture. dynasm makes this easy to do using macros; e.g. below. |.if X64WIN |.define CARG1, rcx // x64/WIN64 C call arguments. |.define CARG2, rdx |.define CARG3, r8 |.define CARG4, r9 |.else |.define CARG1, rdi // x64/POSIX C call arguments. |.define CARG2, rsi |.define CARG3, rdx |.define CARG4, rcx |.endif With above in place, the code can use the mnemonics to refer to the registers rather than the registers themselves. This allows the assembly code to be coded once for both architectures. How would one do this in D inline assembly? Thanks and Regards Dibyendu Here is an example with mixins: version (Windows) { enum Reg : string { CARG1 = "RCX", CARG2 = "RDX", } } else { enum Reg : string { CARG1 = "RDI", CARG2 = "RSI", } } template Instruction(string I, Reg target, Reg source) { enum string Instruction = "asm { mov " ~ target ~ ", " ~ source ~ "; }"; } void func() { mixin(Instruction!("mov", Reg.CARG1, Reg.CARG2)); } Thank you - I probably could use something like this. It is uglier than the simpler approach in dynasm of course. How about when I need to combine this with some struct/union access? In dynasm I can write: | mov BASE, CI->u.l.base // BASE = ci->u.l.base (volatile) | mov PC, CI->u.l.savedpc// PC = CI->u.l.savedpc How can I mix the mixin above and combine with struct offsets? Thanks and Regards Dibyendu
Re: Inline assembly question
On Sunday, 12 November 2017 at 12:00:00 UTC, Basile B. wrote: On Sunday, 12 November 2017 at 11:01:39 UTC, Dibyendu Majumdar wrote: [...] The assembly code uses static allocation of registers, but because of the differences in how registers are used in Win64 versus Unix X64 - different registers are assigned depending on the architecture. dynasm makes this easy to do using macros; e.g. below. [...] With above in place, the code can use the mnemonics to refer to the registers rather than the registers themselves. This allows the assembly code to be coded once for both architectures. I see...the problem is not the input parameters but functions calls **inside** iasm, right ? Not sure I understand the question. Once the defines are there I can write following: | // Call luaF_close | mov CARG1, L // arg1 = L | mov CARG2, BASE// arg2 = base | call extern luaF_close // call luaF_close As you can see above, CARG1, L, CARG2, BASE are all mnemonics that map to registers. However this is only defined in one place. Regards Dibyendu
Inline assembly question
Hi, I have recently started work on building a VM for Lua (actually a derivative of Lua) in X86-64 assembly. I am using the dynasm tool that is part of LuaJIT. I was wondering whether I could also write this in D's inline assembly perhaps, but there is one aspect that I am not sure how to do. The assembly code uses static allocation of registers, but because of the differences in how registers are used in Win64 versus Unix X64 - different registers are assigned depending on the architecture. dynasm makes this easy to do using macros; e.g. below. |.if X64WIN |.define CARG1, rcx // x64/WIN64 C call arguments. |.define CARG2, rdx |.define CARG3, r8 |.define CARG4, r9 |.else |.define CARG1, rdi // x64/POSIX C call arguments. |.define CARG2, rsi |.define CARG3, rdx |.define CARG4, rcx |.endif With above in place, the code can use the mnemonics to refer to the registers rather than the registers themselves. This allows the assembly code to be coded once for both architectures. How would one do this in D inline assembly? Thanks and Regards Dibyendu
Re: Using double value in string template mixin
On Friday, 26 February 2016 at 11:37:32 UTC, BBasile wrote: Erratum! Actually you can, example: import std.stdio; string foo(double a)() { return "auto value = " ~ a.stringof ~ ";"; } void main(string[] args) { mixin(foo!0.1); writeln(value); // 0.1 writeln(typeof(value).stringof); // double } So you have to use .stringof on the template argument. Sorry for the previous answer. Thank you!
Re: Using double value in string template mixin
On Friday, 26 February 2016 at 11:07:28 UTC, BBasile wrote: On Friday, 26 February 2016 at 11:03:43 UTC, Dibyendu Majumdar wrote: How do I use a double value in a mixin template that is generating string? Have you an example of what's failing right now to show ? I am trying something like this: template MyTAlloc(int n_vars, double v) { const char[] MyT = "MyT_init(cast(MyT *) alloca(alloc_size(" ~ n_vars ~ ")), " ~ n_vars ~ ", " ~ v ~ ")"; } MyT *t = mixin(MyTAlloc!(2,5.0)); Error: incompatible types for (("MyT_init(cast(MyT *) alloca(alloc_size(" ~ cast(immutable(char))2 ~ ")), " ~ cast(immutable(char))2 ~ ", ") ~ (5.0)): 'string' and 'double'
Using double value in string template mixin
Hi, How do I use a double value in a mixin template that is generating string? Thanks and Regards Dibyendu
Example of code with manual memory management
Hi, I am looking for example of types where memory management is manual, and the type supports operator overloading, etc. Grateful if someone could point me to sample example code. Thanks and Regards Dibyendu
Re: Disabling GC in D
On Friday, 22 January 2016 at 05:15:13 UTC, Mike Parker wrote: On Thursday, 21 January 2016 at 23:06:55 UTC, Dibyendu Majumdar wrote: On Thursday, 21 January 2016 at 22:44:14 UTC, H. S. Teoh wrote: Hi - I want to be sure that my code is not allocating memory via the GC allocator; but when shipping I don't need to disable GC - it is mostly a development check. I want to manage all memory allocation manually via malloc/free. You can also compile with -vgc and it will tell you where gc allocations are taking place. Thanks!
Shared library question
Hi I am trying to create a simple shared library that exports a D function, but when I try to link to it I get errors such as: error LNK2001: unresolved external symbol _D7xxx12__ModuleInfoZ Here xxx is the module inside the shared library. I am using DMD and MS LINKER (Windows 64-bit) to create the DLL / LIB files. Then I am using DMD to build the executable. In the test program I do following: module app; import xxx; void main() { testing(); /* exported from xxx */ } I cannot figure out what I am doing wrong - any help appreciated. Regards
Re: How to represent struct with trailing array member
On Friday, 22 January 2016 at 01:53:53 UTC, Chris Wright wrote: On Thu, 21 Jan 2016 21:52:06 +, Dibyendu Majumdar wrote: Hi I have C code where the struct has a trailing array member: struct matrix { int rows; int cols; double data[1]; }; D has bounds checking, which makes this awkward. You would be able to access the data using: struct matrix { int rows, cols; double[] data() { void* p = p += this.sizeof; return (cast(double*)p)[0 .. rows * cols]; } } Right - I should use slices in other words. Thanks
Re: Shared library question
On Friday, 22 January 2016 at 22:06:35 UTC, Dibyendu Majumdar wrote: Hi I am trying to create a simple shared library that exports a D function, but when I try to link to it I get errors such as: error LNK2001: unresolved external symbol _D7xxx12__ModuleInfoZ I have uploaded my small test here so if anyone can tell me what I am doing wrong it will hugely appreciated. Thanks! https://github.com/dibyendumajumdar/d-lab/tree/master/sharedlib
Re: Linking C libraries with DMD
On Thursday, 21 January 2016 at 16:14:40 UTC, jmh530 wrote: I'm trying to understand calling C libraries from D on Windows with DMD. I made a simple example and compiled it with a static library fine (so I've converted the .h file correctly). Then, I compiled with gcc to a shared library (because I cannot for the life of me figure out how to do this with DMC). I then used implib to generate a .lib file (the fact that this is necessary is not described nearly well enough in the documentation). Hi I am also new to D and trying to do similar things - i.e. call a shared library written in C from D, but also create a shared library in D. For the latter - on Windows 10 b64-bit - I am using following options for example: -shared -L/LIBPATH:c:\\lib -L//IMPLIB:mylib.lib In my case I would like stuff from my D code to be exported. I found that I need to do following if I want to export a C API. extern (C) export void myfunc(); I did not find examples of how to export D classes / functions - and right now I am getting link errors when trying to export D code. Regards Dibyendu
Re: Linking C libraries with DMD
On Thursday, 21 January 2016 at 22:09:47 UTC, jmh530 wrote: The -L/LIBPATH:c:\lib gives me an error that OPTLINK : Warning 9: Unknown Option : LIBPATH and then gives the path I put is not found. At least when it's outputting the text, it's combining :C:\lib\yourlib.lib so it seemingly is finding it. OPTLINK is for 32-bit code - the options I showed are for 64-bit, which uses MS LINK. You get 64-bit code by adding -m64. Regards
Re: Disabling GC in D
On Thursday, 21 January 2016 at 22:44:14 UTC, H. S. Teoh wrote: Hi - I want to be sure that my code is not allocating memory via the GC allocator; but when shipping I don't need to disable GC - it is mostly a development check. I want to manage all memory allocation manually via malloc/free. Just write "@nogc:" at the top of every module and the compiler will tell you if there's a GC allocation anywhere. Thanks!
How to represent struct with trailing array member
Hi I have C code where the struct has a trailing array member: struct matrix { int rows; int cols; double data[1]; }; In C code this is allocated dynamically to be variable size. The array is used just as normal. How should this be translated to D? Will D's array access allow data elements to be accessed beyond the size declared? Thanks and Regards Dibyendu
Disabling GC in D
Is there a way to disable GC in D? I am aware of the @nogc qualifier but I would like to completely disable GC for the whole app/library. Regards Dibyendu
Re: Disabling GC in D
On Thursday, 21 January 2016 at 22:34:43 UTC, cym13 wrote: Out of curiosity, why would you force not being able to allocate memory? Hi - I want to be sure that my code is not allocating memory via the GC allocator; but when shipping I don't need to disable GC - it is mostly a development check. I want to manage all memory allocation manually via malloc/free. Regards
Re: Linking C libraries with DMD
On Thursday, 21 January 2016 at 22:23:36 UTC, jmh530 wrote: Thanks. I had been trying to get 32bit code to work. I don't think I did anything special with gcc to compile the dll as 64bit. Anyway, this is what I get when I try it again (stuff in brackets I replaced). C:>dmd -m64 .d -L/LIBPATH:C: -L//IMPLIB:.lib LINK : warning LNK4044: unrecognized option '//IMPLIB:.lib'; ignored callC0.obj : error LNK2019: unresolved external symbol referenced in f unction _Dmain callC0.exe : fatal error LNK1120: 1 unresolved externals --- errorlevel 1120 Sorry the option should be -L/IMPLIB:.. - with single slash but you only need this if you are trying to create a shared library which presumably you are not? I believe to create a static library you need to use -lib, else it is an app so you need to supply a main function. Regards
Re: Linking C libraries with DMD
On Thursday, 21 January 2016 at 22:49:06 UTC, jmh530 wrote: I'm not trying to created a shared library in D. My goal is to use a shared library from C in D. Right now, I'm working with a simple test case to make sure I could understand it before working with the actual shared library I want to use. I recall some discussion in LearningD (don't have it in front of me now) that different types of shared libraries are needed on 32bit vs. 64bit because there is a different linker. This is what I did to created the shared library: gcc -Wall -fPIC -c .c -I. gcc -shared -o .dll .o -I. implib .lib .dll Okay then you don't need the /IMPLIB option. But you do need to specify the library via -L as I mentioned before. i.e. use: dmd -m64 -L/LIBPATH: -L prog.d Where is yourlib.lib and this is present along with the DLL in the path you gave. Plus your prog.d needs to have appropriate code. Example: module app; extern (C) void testing(); void main() { testing(); } Here testing() is provided in the DLL.
Re: Linking C libraries with DMD
On Thursday, 21 January 2016 at 21:55:10 UTC, jmh530 wrote: For the latter - on Windows 10 b64-bit - I am using following options for example: -shared -L/LIBPATH:c:\\lib -L//IMPLIB:mylib.lib I'm not having any luck using your options with dmd either (excluding -shared because I don't need to create a shared D library). Sorry forgot to mention that I also include the library I am linking to. Example: dmd -m64 prog.d -L/LIBPATH:c:\lib -Lyourlib.lib Where yourlib.lib and yourlib.dll are in c:\lib folder.
Re: Disabling GC in D
On Thursday, 21 January 2016 at 22:15:13 UTC, Chris Wright wrote: Finally, you can use gc_setProxy() with a a GC proxy you create. Have it throw an exception instead of allocating. That means you will get crashes instead of memory leaks if something uses the GC when it shouldn't. gc_setProxy() and struct Proxy seem not to be part of the public runtime. You can copy the definitions into your own project -- they're listed as extern(C) to make that easier. This may tie you to specific DMD revisions in the case that the GC interface changes. Thanks - I am looking for an option where no GC memory allocation is possible so above looks like the solution. Regards
Re: htod question
On Friday, 22 January 2016 at 00:52:59 UTC, W.J. wrote: Counter question: What's so bad about the D std library ? I am trying to create bindings for existing C library so I was trying to use htod for that.
htod question
I tried using htod but got errors as it could not handle the std C header files (Visual C++). How do people work around this? Thanks and Regards Dibyendu
Re: htod question
On Friday, 22 January 2016 at 01:03:09 UTC, Dibyendu Majumdar wrote: On Friday, 22 January 2016 at 00:52:59 UTC, W.J. wrote: Counter question: What's so bad about the D std library ? I am trying to create bindings for existing C library so I was trying to use htod for that. The library includes various C header files ... causing htod to fail
Distribution of D apps
Hi, I am trying to understand the options for distributing a D app to users. My assumption is that only the shared libraries and binaries need to be distributed, and I need to include the D libraries. Is this correct? Thanks and Regards Dibyendu
Re: DUB - link error on Windows 10 64-bit
On Sunday, 17 January 2016 at 03:07:39 UTC, Mike Parker wrote: Have you verified that this is the only DMD installation on your path? Looks like the problem is not in dub - but the fact that a shared library in D requires a DllMain - as described in: http://forum.dlang.org/post/eokrmosskwelrcyfk...@forum.dlang.org So the DMD compiler is basically failing as I do not have a DllMain. Regards
DUB - link error on Windows 10 64-bit
Hi I am using DUB on Windows 10 64-bit with DMD. I have simple project with following configuration: { "name": "testing", "description": "A minimal D application.", "copyright": "Copyright © 2016, dibyendu", "authors": ["dibyendu"], "targetType": "dynamicLibrary", "libs": ["somelib"], "lflags-windows-x86_64": ["/LIBPATH:c:\\lib\\"], } I get link errors such as: error LNK2019: unresolved external symbol _d_assert error LNK2019: unresolved external symbol _d_unittest error LNK2001: unresolved external symbol _DllMainCRTStartup I have installed DMD by unzipping the DMD archive (The installer does not work correctly on Windows 10). DUB installed as normal. Would appreciate any tips on what I am doing wrong. Thanks and Regards Dibyendu
Re: DUB - link error on Windows 10 64-bit
On Saturday, 16 January 2016 at 20:50:51 UTC, Robert M. Münch wrote: Check your paths in sc.ini Looks like the D link libraries are not found. Well as far as I can tell they are correct (unchanged from whatever the installer set them to): ; environment for both 32/64 bit [Environment] DFLAGS="-I%@P%\..\..\src\phobos" "-I%@P%\..\..\src\druntime\import" ; optlink only reads from the Environment section so we need this redundancy ; from the Environment32 section (bugzilla 11302) LIB="%@P%\..\lib" [Environment32] LIB="%@P%\..\lib" LINKCMD=%@P%\link.exe [Environment64] LIB="%@P%\..\lib64"
Re: CMake support for D
On Monday, 4 January 2016 at 08:28:03 UTC, Luis wrote: I suggest use dub instead of cmake. I did a try to use cmake some time ago (a few years ago, before dub), and was a nightmare to get ir working on GNU/Linux and Windows. With dub , simply works fine with a simple json file. CMake has worked well for me for C/C++ projects, on Windows, Linux and OSX. Pity no official support for D. I need support for apps that have a mixed code base not just D. Thanks for suggesting dub, will check it out. Also premake seems to support D so that is another option. Regards
CMake support for D
Does CMake recognise D in the enable_language command? If not is there a workaround? Thanks and Regards Dibyendu