www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Problem with LLVM-C bindings

reply Daniel <dtoffe gmail.com> writes:
Hi all, I'm having a problem with llvm-c bindings, it is likely a 
trivial thing, but this is my first D project, and I'm not an 
expert in C either.

- symtable.d
struct Symbol {
     string name;
     SymbolKind kind;
     SymbolType type;
     LLVMValueRef valueRef;

     public LLVMValueRef getValueRef() {
         return valueRef;
     }

     public void setValueRef(LLVMValueRef valueRef) {
         this.valueRef = valueRef;
     }
}

- ast.d
class ConstDeclNode : AstNode {
     string constName;
     int constValue;
     Symbol constSymbol;

     Symbol getConstSymbol() {
         return constSymbol;
     }

     void setConstSymbol(Symbol constSymbol) {
         this.constSymbol = constSymbol;
     }

- codegen.d
             valRef = LLVMBuildAlloca(llvmBuilder, int32Type, 
symbolName.toStringz());
             vars[symbolName] = valRef;
             node.getVarSymbol().setValueRef(valRef);
---snip---
             variableRef = vars[symbolName];                    => 
WORKS
             variableRef = node.getVarSymbol().getValueRef();   => 
DOESN'T WORK

Here, vars[] is declared in codegen.d, I set and get 
LLVMValueRefs and everything goes well, but I tried to store the 
LLVMValueRefs in the Symbol structure which is in another D 
module and cannot get it to work, the program stops there when 
trying to access those values.

Any hint about what to try will be greatly appreciated.

Daniel
Oct 25 2023
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 26 October 2023 at 01:28:42 UTC, Daniel wrote:
 Hi all, I'm having a problem with llvm-c bindings, it is likely 
 a trivial thing, but this is my first D project, and I'm not an 
 expert in C either.

 [...]
Just a tip, you can format your code in here. If you're using the web there's a checkbox, "Enable Markdown", that you can check. Then paste your entire file content as ```d // code ``` I will post this with the Markdown disabled so that you see how to write it literally.
Oct 26 2023
prev sibling parent Daniel <dtoffe gmail.com> writes:
On Thursday, 26 October 2023 at 01:28:42 UTC, Daniel wrote:
 Hi all, I'm having a problem with llvm-c bindings, it is likely 
 a trivial thing, but this is my first D project, and I'm not an 
 expert in C either.
 
 ---snipped
 
 Any hint about what to try will be greatly appreciated.

 Daniel
EDIT: My bad, that is what happens when you do things in a hurry, sorry... Hi all, I'm having a problem with llvm-c bindings, it is likely a trivial thing, but this is my first D project, and I'm not an expert in C either. - symtable.d ```d struct Symbol { string name; SymbolKind kind; SymbolType type; LLVMValueRef valueRef; public LLVMValueRef getValueRef() { return valueRef; } public void setValueRef(LLVMValueRef valueRef) { this.valueRef = valueRef; } } ``` - ast.d ```d class ConstDeclNode : AstNode { string constName; int constValue; Symbol constSymbol; Symbol getConstSymbol() { return constSymbol; } void setConstSymbol(Symbol constSymbol) { this.constSymbol = constSymbol; } } ``` - codegen.d ```d valRef = LLVMBuildAlloca(llvmBuilder, int32Type, symbolName.toStringz()); vars[symbolName] = valRef; // A node.getVarSymbol().setValueRef(valRef); // B ---snip--- variableRef = vars[symbolName]; // A => THIS WORKS variableRef = node.getVarSymbol().getValueRef(); // B => THIS DOES NOT WORK ``` Here, vars[] is declared in codegen.d, I set and get LLVMValueRefs using the lines commented with "A" and everything goes well, but I tried to store the LLVMValueRefs in the Symbol structure which is in another D module, in the lines commented with "B", and cannot get it to work, the program stops there when trying to access those values. Any hint about what to try will be greatly appreciated. Daniel
Oct 26 2023