[Issue 15249] Floating-point division should multiply by inverse if lossless.
https://issues.dlang.org/show_bug.cgi?id=15249 Iain Buclaw changed: What|Removed |Added Priority|P1 |P4 --
Re: How to link a msvcr120.dll in an inverse recursive way after a Windows .exe binary deployment
On Tuesday, 6 September 2022 at 04:59:49 UTC, Mike Parker wrote: On Tuesday, 6 September 2022 at 04:36:55 UTC, ShadoLight wrote: True. In that case just distribute the DLL (taken from the DMD bin folder) alongside the HelloWorld EXE so that both reside in the same folder on the target computer. The proper way to do this is to ship the correct version of the Visual C++ redistributable installer and run it as part of the application install process: https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170 Sure. But the OP seems to indicate that he doesn't have administrative privileges on the machines they wish to distribute and test his HelloWorld example. I don't know if you can run an installer without administrative privileges - at least not if you want to write to Program Files / Program Files (x86), modify the Registry, environment variables, etc. I'm not even sure if you can create a folder on Windows 10 without administrative rights without resorting to some tricks. I've never required that - and would think that the IT department (that handles installs and updates on their organization's computers) should have an admin account on all of them. My take was that he can simply copy the EXE and associated DLLs to an existing folder on the target machine if he just want to test it running on a non-developer machine. Making an installer is another mini-project by itself .. not sure if he wants to be burdened with such a step simply to resolve his DMD issue. But your redistributable installer link is indeed the proper way.
Re: How to link a msvcr120.dll in an inverse recursive way after a Windows .exe binary deployment
On Tuesday, 6 September 2022 at 04:36:55 UTC, ShadoLight wrote: True. In that case just distribute the DLL (taken from the DMD bin folder) alongside the HelloWorld EXE so that both reside in the same folder on the target computer. The proper way to do this is to ship the correct version of the Visual C++ redistributable installer and run it as part of the application install process: https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170
Re: How to link a msvcr120.dll in an inverse recursive way after a Windows .exe binary deployment
On Monday, 5 September 2022 at 07:02:53 UTC, BoQsc wrote: The problem is, D Language Compiler is not included along the Windows Operating System. No compiler is included natively with the Windows OS. Not even Microsoft's. Neither msvcr120.dll is included along the Windows Operating System. You have to download it. No other way. Or... you download the DMD installer which conveniently include it for you. How can you download it, if your .exe binary that has the functionality to download it, cannot even be started due to msvcr120.dll not existing on the operating system. I don't understand this. You need DMD to build your EXE. I suppose you have this since your question is specifically about DMD. If that is the case you have the DLL you need. Copy the DLL to C:\Windows\System32\ It required administrator privilegies and this is only a HelloWorld example of the D language deployed on computers, where the D language is yet to be installed. True. In that case just distribute the DLL (taken from the DMD bin folder) alongside the HelloWorld EXE so that both reside in the same folder on the target computer. If you don't have administrative priveleges you cannot modify the PATH on the target computer either, so this is the only way. That is anyway quite standard under Windows - if you search for msvcr*.dll on any Windows machine you'll find lots of copies co-located with the EXEs that use them (using the version that came with the specific version of Visual Studio they were using to build the EXE - for example msvcr90.dll). These DLLs are simply the Visual Studio C/C++ Runtime distributed with Visual Studio.
Re: How to link a msvcr120.dll in an inverse recursive way after a Windows .exe binary deployment
On Sunday, 4 September 2022 at 22:05:24 UTC, ShadoLight wrote: On Sunday, 4 September 2022 at 15:16:47 UTC, BoQsc wrote: **Folder structure** .\msvcr120.dll .\folder1\HelloWorld.exe .\folder2\HelloWorld.exe You don't need to do this. msvcr120.dll is already shipped with the DMD compiler at [DMD-install-folder]\windows\bin64\msvcr120.dll. (It is also in [DMD-install-folder]\windows\bin). You can access it directly from there. The problem is, D Language Compiler is not included along the Windows Operating System. Neither msvcr120.dll is included along the Windows Operating System. You have to download it. No other way. How can you download it, if your .exe binary that has the functionality to download it, cannot even be started due to msvcr120.dll not existing on the operating system. You can try to use pre-existing Command Line Utilities like bitsadmin (Windows 7), curl (Windows 10), but really how can you interact with them from within D Language Binary if it can't even launch. You can try to write a batch script for all that. I really do not want to write batch scripts for the rest of my life, that's why I'm here in the D Language Forum. Copy the DLL to C:\Windows\System32\ It required administrator privilegies and this is only a HelloWorld example of the D language deployed on computers, where the D language is yet to be installed.
Re: How to link a msvcr120.dll in an inverse recursive way after a Windows .exe binary deployment
On Sunday, 4 September 2022 at 15:16:47 UTC, BoQsc wrote: **Folder structure** .\msvcr120.dll .\folder1\HelloWorld.exe .\folder2\HelloWorld.exe You don't need to do this. msvcr120.dll is already shipped with the DMD compiler at [DMD-install-folder]\windows\bin64\msvcr120.dll. (It is also in [DMD-install-folder]\windows\bin). You can access it directly from there. When you run your EXE... the OS looks for the DLL in the same folder of the EXE- if it cannot find it it looks in the folders specified in your PATH. You can test if this is the case by executing 'where msvcr120.dll' at a DOS console command prompt. If the DLL is reachable in any folder in your PATH environment variable these specific path(s) will be displayed. In this case you don't need to do anything - the EXE should be able to run and load the DLL. The fact that you get this error indicate this is not the case. You can do 1 of the following 2 things: - add your DMD bin64 (or bin) path to the PATH environment variable. --or-- - Copy the DLL to C:\Windows\System32\ - that will for sure already be in your PATH so you don't need to modify your PATH environment variable.
Re: How to link a msvcr120.dll in an inverse recursive way after a Windows .exe binary deployment
I've been reading up fairly recently on RPATH for *nix which does what you want. Unfortunately as far as I've found there is no way to do this on Windows without an extra executable.
How to link a msvcr120.dll in an inverse recursive way after a Windows .exe binary deployment
![HelloWorld](https://i.imgur.com/5BjVIU9.png) **Folder structure** .\msvcr120.dll .\folder1\HelloWorld.exe .\folder2\HelloWorld.exe Basic binaries produced by DMD.exe compiler require Microsoft Compiler Runtime DLL As you might know that a basic D Language example `HelloWorld.exe` requires `msvcr120.dll` to work. Linking inverse recursively? **To not include** `msvcr120.dll` into every `.exe` executable's `.\folder\` **and to not marginally increase the overall size of the project**: I'd like to link every `.\folder\.exe` binary to the `.\msvcr120.dll` dynamic library. **Notice** Launching the `.exe` binary with external script to change the `path` variable is not a solution. I'd like the `.exe` binary to "know" and try to search for `.\msvcr120.dll` by itself after clicking it to launch.
Re: function for inverse relative path?
relativePath works with un-normalized paths, and I'd want the same for inverseRelativePath, eg: should work with: `/a//b/./c/bar.d` and `c//bar.d` => `/a//b` unfortunately buildNormalizedPath(rel) will prepend getcwd to `rel` so it's a tad more complex than just calling buildNormalizedPath on both arguments; which is way would be nice to have in std.path On Wed, Dec 6, 2017 at 8:55 PM, Jonathan M Davis via Digitalmars-d <digitalmars-d@puremagic.com> wrote: > On Wednesday, December 06, 2017 17:36:04 Timothee Cour via Digitalmars-d > wrote: >> what would be a robust way to do this `inverseRelativePath`, and >> should that be in std.path? >> >> ``` >> auto a="/a/b/c.d"; >> auto b="b/c.d"; >> assert(inverseRelativePath(a, b) == "/a"); >> assertThrown(inverseRelativePath(a, "c2.d")); >> ``` > > I've never heard of inverse relative paths, but it looks like all you're > doing is looking for a substring match at the end and returning the parts at > the front that don't match. If you're doing that, you could simply do > something like > > enforce(lhs.length >= rhs.length, "some error message"); > if(lhs[rhs.length .. $] == rhs) > return lhs[0 .. rhs.length]; > throw new Exception("some error message"); > > though if you want /a instead of /a/ in your example, some extra code would > have to be added for properly handling trailing slashes, and depending, you > might want to normalize paths first (though typically, that sort of thing is > left up to the caller). It might also need to be enforced that the left-hand > argument is an absolute path. > > - Jonathan M Davis >
Re: function for inverse relative path?
On Wednesday, December 06, 2017 17:36:04 Timothee Cour via Digitalmars-d wrote: > what would be a robust way to do this `inverseRelativePath`, and > should that be in std.path? > > ``` > auto a="/a/b/c.d"; > auto b="b/c.d"; > assert(inverseRelativePath(a, b) == "/a"); > assertThrown(inverseRelativePath(a, "c2.d")); > ``` I've never heard of inverse relative paths, but it looks like all you're doing is looking for a substring match at the end and returning the parts at the front that don't match. If you're doing that, you could simply do something like enforce(lhs.length >= rhs.length, "some error message"); if(lhs[rhs.length .. $] == rhs) return lhs[0 .. rhs.length]; throw new Exception("some error message"); though if you want /a instead of /a/ in your example, some extra code would have to be added for properly handling trailing slashes, and depending, you might want to normalize paths first (though typically, that sort of thing is left up to the caller). It might also need to be enforced that the left-hand argument is an absolute path. - Jonathan M Davis
Re: function for inverse relative path?
how about: ``` string inverseRelativePath(string root, string rel){ while(true){ if(rel.empty || rel==".") { return root; } auto a1=root.baseName; auto a2=rel.baseName; enforce(a1==a2, text(root, " ", rel)); root=root.dirName; rel=rel.dirName; } } unittest{ import std.exception; auto a="/a/b/c.d"; auto b="b/c.d"; assert(inverseRelativePath(a, b) == "/a"); assertThrown(inverseRelativePath(a, "c2.d")); } ``` On Wed, Dec 6, 2017 at 5:36 PM, Timothee Courwrote: > what would be a robust way to do this `inverseRelativePath`, and > should that be in std.path? > > ``` > auto a="/a/b/c.d"; > auto b="b/c.d"; > assert(inverseRelativePath(a, b) == "/a"); > assertThrown(inverseRelativePath(a, "c2.d")); > ```
function for inverse relative path?
what would be a robust way to do this `inverseRelativePath`, and should that be in std.path? ``` auto a="/a/b/c.d"; auto b="b/c.d"; assert(inverseRelativePath(a, b) == "/a"); assertThrown(inverseRelativePath(a, "c2.d")); ```
[Issue 15249] Floating-point division should multiply by inverse if lossless.
https://issues.dlang.org/show_bug.cgi?id=15249 Vladimir Panteleevchanged: What|Removed |Added Keywords||performance CC||thecybersha...@gmail.com --
[Issue 15249] New: Floating-point division should multiply by inverse if lossless.
https://issues.dlang.org/show_bug.cgi?id=15249 Issue ID: 15249 Summary: Floating-point division should multiply by inverse if lossless. Product: D Version: D2 Hardware: x86_64 OS: All Status: NEW Severity: enhancement Priority: P1 Component: dmd Assignee: nob...@puremagic.com Reporter: marco.le...@gmx.de For ages we have been writing "x * 0.5f" because it would be faster than division by 2. I've just checked back with what GDC and LDC2 and they generate the same code for "* 0.5" and "/ 2", recognizing that the inverse is lossless, while DMD still uses division for the latter. This applies to all divisions by powers of two up to a practical limit like the range limit of a ulong 1/2⁶⁴ or the smallest normalized single-precision value 1/2¹²⁶ in case 128-bit cent/ucent are fully implemented. --
@inverse
Just throwing an idea out there... How about using annotations to teach the compiler which functions are inverses of each-other, in order to facilitate optimizing away certain redundant operations even if they are located inside a library(i.e. no source). A little pseudo-code for illustrational purposes, in case my above text is incomprehensible: void inc() pure nothrow @inverse(dec) void dec() pure nothrow @inverse(inc) void swap(T)(ref T lhs, ref T rhs) pure nothrow @inverse(swap!T)
Re: @inverse
On Wed, Feb 25, 2015 at 09:25:47PM +, Daniel N via Digitalmars-d wrote: Just throwing an idea out there... How about using annotations to teach the compiler which functions are inverses of each-other, in order to facilitate optimizing away certain redundant operations even if they are located inside a library(i.e. no source). A little pseudo-code for illustrational purposes, in case my above text is incomprehensible: void inc() pure nothrow @inverse(dec) void dec() pure nothrow @inverse(inc) void swap(T)(ref T lhs, ref T rhs) pure nothrow @inverse(swap!T) I like this idea. It could help ARC by not requiring specific language support for ARC, at least as far as eliding redundant inc/dec pairs are concerned, but allowing a library refcounting type to hint to the optimizer that if inc/dec of the count occurs in pairs, the compiler can elide them if nobody else looks at the refcount in the interim.. It also helps algebra libraries where repeating a self-inverting operation can be automatically elided, thereby simplifying complex expressions a bit and perhaps allowing further optimizations. For the latter, though, it would be even better if other identities are definable, for example @reflexive for indicating that func(x,x) == true, @symmetric for indicating func(x,y) == func(y,x), and so on. Not sure how likely it is that this will actually make it into the language, though. Recently there seems to be a lot of resistance against adding new features that don't have sufficiently wide applicability. T -- People tell me that I'm paranoid, but they're just out to get me.
Re: @inverse
Short messy recent attempt at implementing some things like this at a library level: https://github.com/evenex/atl/blob/master/source/main.d
Re: @inverse
On Thursday, 26 February 2015 at 01:40:32 UTC, Andrei Alexandrescu wrote: Since you're here, do you plan to fix stable sort as recently discussed? -- Andrei While the fix seems straightforward, I haven't taken the time to study the problem. I plan to do so and if I feel confident that I can fix the issue and provide a test case, then I'll make a pull request. That's the best I can say for now.
Re: @inverse
On Thursday, 26 February 2015 at 02:05:25 UTC, Sativa wrote: On Thursday, 26 February 2015 at 00:56:02 UTC, Xinok wrote: I like the idea but feel that it's application is too narrow. I prefer features which are more general and offer greater flexibility. I believe I've read somewhere that some [functional] languages define common patterns and equivalent substitutions for optimization purposes. inc(dec(x)) - x dec(inc(x)) - x cos(x)^^2 + sin(x)^^2 - 1 Which would exactly be the result of what Daniel is talking about except you are adding invariance which is a harder problem yet can be taken care of by the programmer(you would never intentionally write cos(x)^2 + sin(x)^2 for anything since it is equal to 1 and 1 is more efficient to compute). Not intentionally, but consider that D is a very generative language. Templates and string mixins are used heavily. However, the generated code is usually pretty generic so little things like trig identities may appear in the code which the compiler doesn't know how to optimize away. Some C++ compilers make assumptions about standard library functions so techniques similar to this are not unheard of. The problem is one of composition and it is difficult in real circumstances since compositions may not be simply ordered. e.g., what if you have inc(foo(dec(x)) ? In this case one can't simplify because one doesn't know what foo does. Hence, to do it properly one would have to create a whole compositional system. e.g., @linear, @nonlinear, @additive, @commutative, etc... e.g., if we new foo was linear then we could simplify the above to foo(x). ...and, as you hinted at, most functions are non-linear and therefor will make @inverse nearly useless. That almost seems too complicated, like it would require a complete CAS built into the compiler. Some functions don't have clearly defined inverses as well, such as x^^3. Then x^^2 is only invertible if you limit the domain and range to x = 0. I suppose, though, one might be able to do something like setup @inverse functions for actions. e.g., user clicks on button X. The inverse then is sort of an undo of that. In an undo system one expects every action to be linear(have an inverse)... Hence it might be useful in such circumstances. I wouldn't use the term linear to mean invertible. A linear function is one such that: f(c*a + b) = c*f(a) + f(b)
Re: @inverse
On Thursday, 26 February 2015 at 00:56:02 UTC, Xinok wrote: On Wednesday, 25 February 2015 at 21:25:49 UTC, Daniel N wrote: Just throwing an idea out there... How about using annotations to teach the compiler which functions are inverses of each-other, in order to facilitate optimizing away certain redundant operations even if they are located inside a library(i.e. no source). A little pseudo-code for illustrational purposes, in case my above text is incomprehensible: void inc() pure nothrow @inverse(dec) void dec() pure nothrow @inverse(inc) void swap(T)(ref T lhs, ref T rhs) pure nothrow @inverse(swap!T) I like the idea but feel that it's application is too narrow. I prefer features which are more general and offer greater flexibility. I believe I've read somewhere that some [functional] languages define common patterns and equivalent substitutions for optimization purposes. inc(dec(x)) - x dec(inc(x)) - x cos(x)^^2 + sin(x)^^2 - 1 Which would exactly be the result of what Daniel is talking about except you are adding invariance which is a harder problem yet can be taken care of by the programmer(you would never intentionally write cos(x)^2 + sin(x)^2 for anything since it is equal to 1 and 1 is more efficient to compute). The problem is one of composition and it is difficult in real circumstances since compositions may not be simply ordered. e.g., what if you have inc(foo(dec(x)) ? In this case one can't simplify because one doesn't know what foo does. Hence, to do it properly one would have to create a whole compositional system. e.g., @linear, @nonlinear, @additive, @commutative, etc... e.g., if we new foo was linear then we could simplify the above to foo(x). ...and, as you hinted at, most functions are non-linear and therefor will make @inverse nearly useless. I suppose, though, one might be able to do something like setup @inverse functions for actions. e.g., user clicks on button X. The inverse then is sort of an undo of that. In an undo system one expects every action to be linear(have an inverse)... Hence it might be useful in such circumstances.
Re: @inverse
On 2/25/15 4:55 PM, Xinok wrote: On Wednesday, 25 February 2015 at 21:25:49 UTC, Daniel N wrote: Just throwing an idea out there... How about using annotations to teach the compiler which functions are inverses of each-other, in order to facilitate optimizing away certain redundant operations even if they are located inside a library(i.e. no source). A little pseudo-code for illustrational purposes, in case my above text is incomprehensible: void inc() pure nothrow @inverse(dec) void dec() pure nothrow @inverse(inc) void swap(T)(ref T lhs, ref T rhs) pure nothrow @inverse(swap!T) I like the idea but feel that it's application is too narrow. I prefer features which are more general and offer greater flexibility. I believe I've read somewhere that some [functional] languages define common patterns and equivalent substitutions for optimization purposes. inc(dec(x)) - x dec(inc(x)) - x cos(x)^^2 + sin(x)^^2 - 1 Since you're here, do you plan to fix stable sort as recently discussed? -- Andrei
Re: @inverse
On Wed, 25 Feb 2015 17:40:31 -0800, Andrei Alexandrescu wrote: Since you're here, do you plan to fix stable sort as recently discussed? -- Andrei it's not necessarily so fubared as article says. i ported their java bad case generator and wasn't able to hit the bug with arrays up to 1_807_108_864 elements (which is almost the limit my box can handle). so either phobos sort is immune, or we need REALLY big arrays to $#^@^ it up. signature.asc Description: PGP signature
Re: @inverse
On Wednesday, 25 February 2015 at 21:25:49 UTC, Daniel N wrote: Just throwing an idea out there... How about using annotations to teach the compiler which functions are inverses of each-other, in order to facilitate optimizing away certain redundant operations even if they are located inside a library(i.e. no source). A little pseudo-code for illustrational purposes, in case my above text is incomprehensible: void inc() pure nothrow @inverse(dec) void dec() pure nothrow @inverse(inc) void swap(T)(ref T lhs, ref T rhs) pure nothrow @inverse(swap!T) I like the idea but feel that it's application is too narrow. I prefer features which are more general and offer greater flexibility. I believe I've read somewhere that some [functional] languages define common patterns and equivalent substitutions for optimization purposes. inc(dec(x)) - x dec(inc(x)) - x cos(x)^^2 + sin(x)^^2 - 1
inverse of escapeShellCommand?
is there a function to get the inverse of escapeShellCommand? ie: assert(escapeShellCommandInverse(` foo 'hello world' `)==[`foo`, `hello world`]);
Re: inverse of std.demangle?
On Thursday, 11 July 2013 at 01:32:33 UTC, H. S. Teoh wrote: I think this is a general problem of array and AA literals allocating at runtime (and sometimes *every single time* the literal is used). Aye, it is. Even here it was doing it on every single loop iteration. Disgusting. I was hoping setting it as an enum would ctfe it and just be static data, but nope. Anyway, I updated the file (forgive me if this posts multiple times, I tried last night but my internet died). It is up here now too: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/mangle.d It is allocation-free, except for the keyword list literal. Ugh. (This could be avoided if I sorted it manually but meh i'm lazy). Uses static stack buffers for its scratch space. Now handled more complex function signatures too, compile with -unittest and take a look. I think this is probably all I'll do on this for now, but it should be pretty useful as is.
Re: inverse of std.demangle?
On 2013-07-11 14:39, Adam D. Ruppe wrote: Aye, it is. Even here it was doing it on every single loop iteration. Disgusting. I was hoping setting it as an enum would ctfe it and just be static data, but nope. Have you tried: enum string[23] _primitives = [ ... ]; static immutable primitives = _primitives; -- /Jacob Carlborg
Re: inverse of std.demangle?
On Thursday, 11 July 2013 at 16:38:48 UTC, Jacob Carlborg wrote: enum string[23] _primitives = [ ... ]; static immutable primitives = _primitives; Cool, a variant of that did work. Thanks! Now it is 100% heap allocation free.
Re: inverse of std.demangle?
BTW if you guys are wondering how I found the allocations, it was pretty simple: dmd mangle.d -debug -gc gdb ./mangle break gc_malloc break gc_qalloc r Then when it breaks, do where and see what called the gc alloc. Then hit c to continue and repeat until you've found and fixed them (or ignored if they are in druntime).
Re: inverse of std.demangle?
On Jul 11, 2013, at 9:56 AM, Adam D. Ruppe destructiona...@gmail.com wrote: On Thursday, 11 July 2013 at 16:38:48 UTC, Jacob Carlborg wrote: enum string[23] _primitives = [ ... ]; static immutable primitives = _primitives; Cool, a variant of that did work. Thanks! Now it is 100% heap allocation free. Sweet! And to be fair, I'm fine with heap allocation as a failsafe. What's important is that if the user provides a sufficiently large input buffer, then the routine doesn't allocate. That way, the GC and other sensitive parts of the code can use these functions if needed.
Re: inverse of std.demangle?
ping? On Wed, Jul 3, 2013 at 5:42 PM, Timothee Cour thelastmamm...@gmail.comwrote: I'd like to have a function: string mangle(string mangled_string); unittest{ void foo(int x){} assert(foo.mangleof.demangle.mangle == foo.mangleof); } is there such a functionality, even partially?
Re: inverse of std.demangle?
As far as I know, no such function exists (outside of dmd itself).
Re: inverse of std.demangle?
On Wednesday, 10 July 2013 at 16:30:25 UTC, Timothee Cour wrote: Do you have a pointer for that function in dmd ? The compiler doesn't do it as one function, I mean it can parse as string and mangle it in the process of compiling code. To go from demangled string = mangled string, you'd first parse the demangled string as a declaration and turn it into a symbol, then mangle that symbol. But if you just want to do really simple cases, maybe this isn't too hard. I just quickly slapped this file together: http://arsdnet.net/dcode/mangle.d It is very basic, but might work for a lot of cases - it handled some basic functions I tried. Perhaps not too hard to add support for more from here. As you can see by looking at the code, the way it works is: 1) tokenize the input string (it does this in a stupid way, and doesn't know all of D's symbols and keywords, but it doesn't have to be too smart since demangle produces fairly predictable input, so I think it will be ok). 2) runs a simple, stupid parser to get the components of the function - return type, name, and arguments. To add support for templates, extern(C), and so on, this would need to be expanded. 3) put the pieces back together in a wholly mangled form, borrowing one helper array from core.demangle. There's a main function at the bottom of the file that does one of the few tests I tried with it.
Re: inverse of std.demangle?
Thanks much, that's a good start. Template support would definitely be needed as it's so common. This should go in std.demangle (or maybe a new std.mangle) * One use case is using it in shared libraries: user asks for a symbol via its demangled string representation (which is most natural for user), then the string is mangled, and then calls dlsym to retrieve the actual pointer to symbol in the shared lib. * Another is in REPL where we can optimize certain calls by bypassing the compiler, when the symbol already exists in main executable. * There are other use cases So this is worth doing. On Wed, Jul 10, 2013 at 10:30 AM, Adam D. Ruppe destructiona...@gmail.comwrote: On Wednesday, 10 July 2013 at 16:30:25 UTC, Timothee Cour wrote: Do you have a pointer for that function in dmd ? The compiler doesn't do it as one function, I mean it can parse as string and mangle it in the process of compiling code. To go from demangled string = mangled string, you'd first parse the demangled string as a declaration and turn it into a symbol, then mangle that symbol. But if you just want to do really simple cases, maybe this isn't too hard. I just quickly slapped this file together: http://arsdnet.net/dcode/**mangle.d http://arsdnet.net/dcode/mangle.d It is very basic, but might work for a lot of cases - it handled some basic functions I tried. Perhaps not too hard to add support for more from here. As you can see by looking at the code, the way it works is: 1) tokenize the input string (it does this in a stupid way, and doesn't know all of D's symbols and keywords, but it doesn't have to be too smart since demangle produces fairly predictable input, so I think it will be ok). 2) runs a simple, stupid parser to get the components of the function - return type, name, and arguments. To add support for templates, extern(C), and so on, this would need to be expanded. 3) put the pieces back together in a wholly mangled form, borrowing one helper array from core.demangle. There's a main function at the bottom of the file that does one of the few tests I tried with it.
Re: inverse of std.demangle?
On Wed, Jul 10, 2013 at 10:30 AM, Adam D. Ruppe destructiona...@gmail.comwrote: On Wednesday, 10 July 2013 at 16:30:25 UTC, Timothee Cour wrote: Do you have a pointer for that function in dmd ? The compiler doesn't do it as one function, I mean it can parse as string and mangle it in the process of compiling code. To go from demangled string = mangled string, you'd first parse the demangled string as a declaration and turn it into a symbol, then mangle that symbol. But if you just want to do really simple cases, maybe this isn't too hard. I just quickly slapped this file together: http://arsdnet.net/dcode/**mangle.d http://arsdnet.net/dcode/mangle.d It is very basic, but might work for a lot of cases - it handled some basic functions I tried. Perhaps not too hard to add support for more from here. As you can see by looking at the code, the way it works is: 1) tokenize the input string (it does this in a stupid way, and doesn't know all of D's symbols and keywords, but it doesn't have to be too smart since demangle produces fairly predictable input, so I think it will be ok). 2) runs a simple, stupid parser to get the components of the function - return type, name, and arguments. To add support for templates, extern(C), and so on, this would need to be expanded. 3) put the pieces back together in a wholly mangled form, borrowing one helper array from core.demangle. There's a main function at the bottom of the file that does one of the few tests I tried with it. also, can you push it to your misc-stuff-including-D-programming-language-web-stuff git repo?
Re: inverse of std.demangle?
On Jul 10, 2013, at 10:44 AM, Timothee Cour thelastmamm...@gmail.com wrote: Thanks much, that's a good start. Template support would definitely be needed as it's so common. This should go in std.demangle (or maybe a new std.mangle) core.mangle/demangle. It would have to be done in a way that avoided allocating though (similar to core.demangle), to be in core.
Re: inverse of std.demangle?
On Wednesday, 10 July 2013 at 17:44:51 UTC, Timothee Cour wrote: * One use case is using it in shared libraries: user asks for a symbol via its demangled string representation (which is most natural for user), then the string is mangled, and then calls dlsym to retrieve the actual pointer to symbol in the shared lib. I think in this case, it would be better to use .mangleof anyway because you'll want that type safety. You could still compare a user inputted string to typeof(S).stringof if you want to choose one at runtime. I'll look at the github in a bit, in the middle of a bunch of stuff now.
Re: inverse of std.demangle?
On Wed, Jul 10, 2013 at 11:39 AM, Adam D. Ruppe destructiona...@gmail.comwrote: On Wednesday, 10 July 2013 at 17:44:51 UTC, Timothee Cour wrote: * One use case is using it in shared libraries: user asks for a symbol via its demangled string representation (which is most natural for user), then the string is mangled, and then calls dlsym to retrieve the actual pointer to symbol in the shared lib. I think in this case, it would be better to use .mangleof anyway because you'll want that type safety. You could still compare a user inputted string to typeof(S).stringof if you want to choose one at runtime. How would that work, since this is runtime only? I'm interested in the case where I don't have access to said symbol, all I have is a string representation of it. For example when using dlopen on a library where we don't have source code; so in this case typeof(S) doesn't make sense.
Re: inverse of std.demangle?
On Wednesday, 10 July 2013 at 19:03:22 UTC, Timothee Cour wrote: How would that work, since this is runtime only? Take a pointer with the right type and then attach the rest of the name to it. Given: int foo(int a, string) { return a;} void main() { int function(int, string) fooPtr; pragma(msg, foo.mangleof); pragma(msg, typeof(fooPtr).mangleof); } You get: _D6test303fooFiAyaZi PFiAyaZi The first part of the full name is pretty simple: _Dnamehere, where namehere is length pf the part, then the part. length 6, string test30 length 3, string foo total name: test30.foo Then after that, comes the type: a 'F'unction that returns 'int' with arguments (in reverse order) 'A'rray of immutable ('y') char ('a'), then the 'Z' final argument 'i'nt. The second line shows a 'P'ointer to that same typed symbol. If you want to get the foo function out of a library, you'd probably go: fooptr = cast(typeof(fooptr)) dlsym(handle, _D6test303fooFiAyaZi); Then you can use fooptr. Without knowing the static type of the function though, it isn't safe to use - you could pass it wrong arguments and ruin your day. So having the statically typed function pointer is very useful. Since we have that, we can get a name from the user and put it together: fooptr = cast(typeof(fooptr)) dlsym(handle, _D ~ mangleName(test30.foo) ~ typeof(fooptr).mangleof[1 .. $]); Cut the 'P' off the fooptr mangle and attach that to the name's mangle, and we have the full name. If you want to ask for the type from the user too, what I'd do is compare it against the various options you know how to handle: foreach(type; TypeTuple!(int function(int, string), int function() /* and whatever else you want */) { if(userEnteredString == type.stringof) { type ptr = cast(type) dlsym(handle, _D ~ mangleName(userEnteredName) ~ type.mangleof[1 .. $]); if(ptr is null) throw new Exception(not found in library); } } /* otherwise, throw that the user entered type isn't supported */ So, of course, this limits you to the list of supported function signatures, but I think you realistically are anyway since getting the ABI right at runtime from arbitrary input isn't going to be easy.
Re: inverse of std.demangle?
On Wednesday, 10 July 2013 at 18:10:19 UTC, Sean Kelly wrote: It would have to be done in a way that avoided allocating though (similar to core.demangle), to be in core. This would be really easy to do if enum string[] = [...]; didn't allocate at runtime. Unbelievable.
Re: inverse of std.demangle?
On Thu, Jul 11, 2013 at 03:24:05AM +0200, Adam D. Ruppe wrote: On Wednesday, 10 July 2013 at 18:10:19 UTC, Sean Kelly wrote: It would have to be done in a way that avoided allocating though (similar to core.demangle), to be in core. This would be really easy to do if enum string[] = [...]; didn't allocate at runtime. Unbelievable. I think this is a general problem of array and AA literals allocating at runtime (and sometimes *every single time* the literal is used). We really should have a way of generating array and AA literals in the object file so that no runtime allocation is needed. Runtime-generated literals really should only be used when there are runtime variables involved in their construction, i.e., immutable x = [1,2,y] can't be allocated at compile-time if y is a runtime variable, but immutable x = [1,2,3] can and should be allocated at compile-time and saved in the object file. T -- What's a hot crossed bun? An angry rabbit.
inverse of std.demangle?
I'd like to have a function: string mangle(string mangled_string); unittest{ void foo(int x){} assert(foo.mangleof.demangle.mangle == foo.mangleof); } is there such a functionality, even partially?