@@ -20,201 +20,62 @@ procedure RunTests();
2020implementation
2121
2222uses
23- WinApi.Windows,
2423 System.SysUtils,
25- System.AnsiStrings,
26- System.IOUtils,
27- libLLVM;
24+ libLLVM,
25+ libLLVM.Utils,
26+ libLLVM.Test.CodeGen,
27+ libLLVM.Test.Variable,
28+ libLLVM.Test.Values,
29+ libLLVM.Test.Types,
30+ libLLVM.Test.TypeConversion,
31+ libLLVM.Test.Module ,
32+ libLLVM.Test.Memory,
33+ libLLVM.Test.JIT,
34+ libLLVM.Test.Functions,
35+ libLLVM.Test.FunctionCall,
36+ libLLVM.Test.ControlFlow,
37+ libLLVM.Test.Comparison,
38+ libLLVM.Test.Bitwise,
39+ libLLVM.Test.BasicBlock,
40+ libLLVM.Test.Arithmetic;
2841
29- procedure BuildAndLink ();
42+ procedure RunTests ();
3043var
31- // Version info
32- LMajor: Cardinal;
33- LMinor: Cardinal;
34- LPatch: Cardinal;
35-
36- // File paths
37- LLLFile: string;
38- LObjFile: string;
39- LExeFile: string;
40-
41- // LLVM objects
42- LCtx: LLVMContextRef;
43- LBuf: LLVMMemoryBufferRef;
44- LMod: LLVMModuleRef;
45- LTarget: LLVMTargetRef;
46- LTM: LLVMTargetMachineRef;
47- LTD: LLVMTargetDataRef;
48-
49- // Strings and pointers
50- LIR: string;
51- LTripleStr: string;
52- LCPU: string;
53- LFeatures: string;
54-
55- // C pointers for LLVM (need disposal)
56- LMsg: PAnsiChar;
57- LErr: PAnsiChar;
58- LEmitErr: PAnsiChar;
59- LTripleHeap: PAnsiChar;
60- LDLStrHeap: PAnsiChar;
61-
62- // Linking
63- LArgs: TArray<string>;
64- LRC: Integer;
65- LCan: Boolean;
66-
67- // String conversion helpers
68- LStrLen: Cardinal;
69- LStr: Pointer;
70- FMarshaller: TMarshaller;
71-
72- function AsUTF8 (const AValue: string; ALength: PCardinal=nil ): Pointer;
73- begin
74- Result := FMarshaller.AsUtf8(AValue).ToPointer;
75- if Assigned(ALength) then
76- ALength^ := System.AnsiStrings.StrLen(PAnsiChar(Result));
77- end ;
78-
79- procedure FailIf (const Cond: Boolean; const Msg: string; const AArgs: array of const );
80- begin
81- if Cond then
82- raise Exception.CreateFmt(Msg, AArgs);
83- end ;
84-
44+ LNum: UInt32;
8545begin
86- // === LLVM Version ===
87- LMajor := 0 ;
88- LMinor := 0 ;
89- LPatch := 0 ;
90- LLVMGetVersion(@LMajor, @LMinor, @LPatch);
91-
92- WriteLn(Format(' === libLLVM v%s ===' , [libLLVM_VERSION]));
93- WriteLn(Format(' Running LLVM v%d.%d.%d' , [LMajor, LMinor, LPatch]));
94-
95- // === File Setup ===
96- LLLFile := ' .\output\HelloWorld.ll' ;
97- LObjFile := ' .\output\HelloWorld.obj' ;
98- LExeFile := ' .\output\HelloWorld.exe' ;
99-
100- // === IR Code ===
101- LIR :=
102- ' ; ModuleID = "hello"' #10 +
103- ' declare i32 @printf(ptr, ...)' #10 +
104- ' @.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"' #10 +
105- ' define i32 @main() {' #10 +
106- ' %call = call i32 (ptr, ...) @printf(ptr @.str)' #10 +
107- ' ret i32 0' #10 +
108- ' }' #10 ;
109-
110- TDirectory.CreateDirectory(TPath.GetDirectoryName(LLLFile));
111- TFile.WriteAllText(LLLFile, LIR, TEncoding.UTF8);
112-
113- // === LLVM Initialization ===
114- LLVMInitializeX86TargetInfo();
115- LLVMInitializeX86Target();
116- LLVMInitializeX86TargetMC();
117- LLVMInitializeX86AsmPrinter();
118-
119- // === Context and Module Creation ===
120- LCtx := LLVMContextCreate();
121- LMod := nil ;
122- LTM := nil ;
123-
12446 try
125- // Parse IR from memory buffer
126- LStr := AsUTF8(LIR, @LStrLen);
127- LBuf := LLVMCreateMemoryBufferWithMemoryRangeCopy(LStr, LStrLen, AsUTF8(' hello.ll' ));
128-
129- LMsg := nil ;
130- FailIf(LLVMParseIRInContext(LCtx, LBuf, @LMod, @LMsg) <> 0 ,
131- ' Parse IR failed: %s' , [string(LMsg)]);
132-
133- // === Target Setup ===
134- LTripleHeap := LLVMGetDefaultTargetTriple();
135- try
136- // Force Windows MSVC target for COFF compatibility
137- LTripleStr := ' x86_64-pc-windows-msvc' ;
138- LCPU := ' x86-64' ;
139- LFeatures := ' ' ;
140-
141- LErr := nil ;
142- FailIf(LLVMGetTargetFromTriple(AsUTF8(LTripleStr), @LTarget, @LErr) <> 0 ,
143- ' GetTarget failed: %s' , [string(LErr)]);
144-
145- LTM := LLVMCreateTargetMachine(LTarget, AsUTF8(LTripleStr), AsUTF8(LCPU),
146- AsUTF8(LFeatures), LLVMCodeGenLevelDefault,
147- LLVMRelocDefault, LLVMCodeModelDefault);
148- finally
149- if LTripleHeap <> nil then
150- LLVMDisposeMessage(LTripleHeap);
47+ TLLUtils.PrintLn(' === libLLVM v%s ===' , [TLLVM.GetVersionStr()]);
48+ TLLUtils.PrintLn(' Running LLVM v%s' , [TLLVM.GetLLVMVersionStr()]);
49+ TLLUtils.PrintLn();
50+
51+ LNum := 15 ;
52+
53+ case LNum of
54+ 01 : TTestArithmetic.RunAllTests();
55+ 02 : TTestBasicBlock.RunAllTests();
56+ 03 : TTestBitwise.RunAllTests();
57+ 04 : TTestComparison.RunAllTests();
58+ 05 : TTestControlFlow.RunAllTests();
59+ // 06: TTestFunctionCall.RunAllTests(); // TODO: fix range check error
60+ 07 : TTestFunction.RunAllTests();
61+ 08 : TTestJIT.RunAllTests();
62+ 09 : TTestMemory.RunAllTests();
63+ 10 : TTestModule.RunAllTests();
64+ 11 : TTestTypeConversion.RunAllTests();
65+ 12 : TTestTypes.RunAllTests();
66+ 13 : TTestValues.RunAllTests();
67+ 14 : TTestVariable.RunAllTests();
68+ 15 : TTestCodeGen.RunAllTests();
69+ else
70+ TLLUtils.Print(' Invalid test number.' );
15171 end ;
15272
153- // === Module Configuration ===
154- LLVMSetTarget(LMod, AsUTF8(LTripleStr));
155- LTD := LLVMCreateTargetDataLayout(LTM);
156- LDLStrHeap := LLVMCopyStringRepOfTargetData(LTD);
157- try
158- LLVMSetDataLayout(LMod, LDLStrHeap);
159- finally
160- if LDLStrHeap <> nil then
161- LLVMDisposeMessage(LDLStrHeap);
162- LLVMDisposeTargetData(LTD);
163- end ;
164-
165- // === Object File Generation ===
166- LEmitErr := nil ;
167- FailIf(LLVMTargetMachineEmitToFile(LTM, LMod, AsUTF8(LObjFile), LLVMObjectFile, @LEmitErr) <> 0 ,
168- ' EmitToFile failed: %s' , [string(LEmitErr)]);
169-
170- finally
171- // Clean up in reverse order of creation
172- if LTM <> nil then
173- LLVMDisposeTargetMachine(LTM);
174- if LMod <> nil then
175- LLVMDisposeModule(LMod);
176- if LCtx <> nil then
177- LLVMContextDispose(LCtx);
178- end ;
179-
180- // === Linking Phase ===
181- LArgs := [
182- ' lld-link' ,
183- ' /verbose' ,
184- ' /nologo' ,
185- ' /subsystem:console' ,
186- ' /entry:main' ,
187- ' /out:' + LExeFile,
188- LObjFile,
189- ' /libpath:.\libs' ,
190- ' kernel32.lib' ,
191- ' msvcrt.lib' ,
192- ' legacy_stdio_definitions.lib'
193- ];
194-
195- LRC := LLDLink(LArgs, ' coff' , LCan);
196-
197- // === Results ===
198- Writeln(Format(' LLD rc=%d canRunAgain=%s' , [LRC, BoolToStr(LCan, True)]));
199- Writeln(' LL file: ' + LLLFile);
200- Writeln(' OBJ file: ' + LObjFile);
201- Writeln(' EXE file: ' + LExeFile);
202- end ;
203-
204-
205- procedure RunTests ();
206- begin
207- try
208- BuildAndLink();
20973 except
21074 on E: Exception do
21175 Writeln(E.ClassName, ' : ' , E.Message);
21276 end ;
21377
214- WriteLn;
215- Write(' Press ENTER to continue...' );
216- ReadLn;
217- WriteLn;
78+ TLLUtils.Pause();
21879end ;
21980
22081end .
0 commit comments