www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - WebAssembly _start or crt_constructor not get called.

reply learnfirst1 <learnfirst1 gmail.com> writes:
extern(C) {
	__gshared int counter = 1;
	pragma(crt_constructor) void ginit(){
		counter++;
	}
	
	int getCounter(){
		return counter;
	}

	void _start() {
		counter++;
	}
}
----------------------

build with ldc2 -mtriple=wasm32-unknown-unknown-wasm -betterC 
test.d -link-internally

I expect the getCounter return 3, but it return 0, is this a bug  
?
Aug 10 2018
next sibling parent learnfirst1 <learnfirst1 gmail.com> writes:
On Friday, 10 August 2018 at 08:45:07 UTC, learnfirst1 wrote:
 extern(C) {
 	__gshared int counter = 1;
 	pragma(crt_constructor) void ginit(){
 		counter++;
 	}
 	
 	int getCounter(){
 		return counter;
 	}

 	void _start() {
 		counter++;
 	}
 }
 ----------------------

 build with ldc2 -mtriple=wasm32-unknown-unknown-wasm -betterC 
 test.d -link-internally

 I expect the getCounter return 3, but it return 0, is this a 
 bug  ?
I write wrong, it return 1 not 0 on browser.
Aug 10 2018
prev sibling parent reply kinke <kinke libero.it> writes:
I don't know about _start(), but please don't expect 
`pragma(crt_constructor)` to work. As the name says, these 'C 
runtime constructors' are normally invoked by the C runtime entry 
point, before calling the C main, all of which doesn't apply to 
WebAssembly or any other bare metal target.
Aug 10 2018
parent reply learnfirst1 <learnfirst1 gmail.com> writes:
On Friday, 10 August 2018 at 10:43:09 UTC, kinke wrote:
 I don't know about _start(), but please don't expect 
 `pragma(crt_constructor)` to work. As the name says, these 'C 
 runtime constructors' are normally invoked by the C runtime 
 entry point, before calling the C main, all of which doesn't 
 apply to WebAssembly or any other bare metal target.
please take a look at this: http://llvm.org/doxygen/WebAssemblyLowerGlobalDtors_8cpp_source.html Since LLVM can support WebAssembly GlobalCtors, LDC also append crt_constructor into llvm::GlobalCtors, why not make ldc with WebAssembly work same way.
Aug 10 2018
parent kinke <kinke libero.it> writes:
On Friday, 10 August 2018 at 11:22:39 UTC, learnfirst1 wrote:
 please take a look at this:  
 http://llvm.org/doxygen/WebAssemblyLowerGlobalDtors_8cpp_source.html


 Since LLVM can support WebAssembly GlobalCtors,  LDC also 
 append crt_constructor into llvm::GlobalCtors,  why not make 
 ldc with WebAssembly work same way.
Thx for the link, but I don't see a corresponding file for global *con*structors. LDC emits them into `llvm.global_{ctor,dtor}s` (see https://run.dlang.io/is/q8TIBU and hit the `IR` button), so I guess the lowering already works for `pragma(crt_destructor)`.
Aug 10 2018