52
52
#include < unistd.h>
53
53
#endif
54
54
55
- // Does this need to be done, or can it be made to resolve from the main program?
56
- extern " C" void __morestack (void *args, void *fn_ptr, uintptr_t stack_ptr);
57
-
58
55
using namespace llvm ;
59
56
60
57
static const char *LLVMRustError;
@@ -95,11 +92,13 @@ void LLVMInitializeX86AsmParser();
95
92
// that rustllvm doesn't actually link to and it's pointless to put target info
96
93
// into the registry that Rust can not generate machine code for.
97
94
98
- #define INITIALIZE_TARGETS () LLVMInitializeX86TargetInfo(); \
99
- LLVMInitializeX86Target (); \
100
- LLVMInitializeX86TargetMC (); \
101
- LLVMInitializeX86AsmPrinter (); \
102
- LLVMInitializeX86AsmParser ();
95
+ void LLVMRustInitializeTargets () {
96
+ LLVMInitializeX86TargetInfo ();
97
+ LLVMInitializeX86Target ();
98
+ LLVMInitializeX86TargetMC ();
99
+ LLVMInitializeX86AsmPrinter ();
100
+ LLVMInitializeX86AsmParser ();
101
+ }
103
102
104
103
extern " C" bool
105
104
LLVMRustLoadLibrary (const char * file) {
@@ -113,8 +112,6 @@ LLVMRustLoadLibrary(const char* file) {
113
112
return true ;
114
113
}
115
114
116
- ExecutionEngine* EE;
117
-
118
115
// Custom memory manager for MCJITting. It needs special features
119
116
// that the generic JIT memory manager doesn't entail. Based on
120
117
// code from LLI, change where needed for Rust.
@@ -123,8 +120,9 @@ class RustMCJITMemoryManager : public JITMemoryManager {
123
120
SmallVector<sys::MemoryBlock, 16 > AllocatedDataMem;
124
121
SmallVector<sys::MemoryBlock, 16 > AllocatedCodeMem;
125
122
SmallVector<sys::MemoryBlock, 16 > FreeCodeMem;
123
+ void * __morestack;
126
124
127
- RustMCJITMemoryManager () { }
125
+ RustMCJITMemoryManager (void * sym) : __morestack(sym ) { }
128
126
~RustMCJITMemoryManager ();
129
127
130
128
virtual uint8_t *allocateCodeSection (uintptr_t Size, unsigned Alignment,
@@ -275,7 +273,7 @@ void *RustMCJITMemoryManager::getPointerToNamedFunction(const std::string &Name,
275
273
if (Name == " mknod" ) return (void *)(intptr_t )&mknod;
276
274
#endif
277
275
278
- if (Name == " __morestack" ) return ( void *)( intptr_t ) &__morestack;
276
+ if (Name == " __morestack" ) return &__morestack;
279
277
280
278
const char *NameStr = Name.c_str ();
281
279
void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol (NameStr);
@@ -294,13 +292,13 @@ RustMCJITMemoryManager::~RustMCJITMemoryManager() {
294
292
free (AllocatedDataMem[i].base ());
295
293
}
296
294
297
- extern " C" bool
298
- LLVMRustJIT (LLVMPassManagerRef PMR,
295
+ extern " C" void *
296
+ LLVMRustJIT (void * __morestack,
297
+ LLVMPassManagerRef PMR,
299
298
LLVMModuleRef M,
300
299
CodeGenOpt::Level OptLevel,
301
300
bool EnableSegmentedStacks) {
302
301
303
- INITIALIZE_TARGETS ();
304
302
InitializeNativeTarget ();
305
303
InitializeNativeTargetAsmPrinter ();
306
304
@@ -315,39 +313,37 @@ LLVMRustJIT(LLVMPassManagerRef PMR,
315
313
PM->add (createInstructionCombiningPass ());
316
314
PM->add (createReassociatePass ());
317
315
PM->add (createGVNPass ());
318
- PM->add (createPromoteMemoryToRegisterPass ());
319
316
PM->add (createCFGSimplificationPass ());
320
317
PM->add (createFunctionInliningPass ());
318
+ PM->add (createPromoteMemoryToRegisterPass ());
321
319
PM->run (*unwrap (M));
322
320
323
- RustMCJITMemoryManager* MM = new RustMCJITMemoryManager ();
324
- EE = EngineBuilder (unwrap (M))
321
+ RustMCJITMemoryManager* MM = new RustMCJITMemoryManager (__morestack );
322
+ ExecutionEngine* EE = EngineBuilder (unwrap (M))
325
323
.setTargetOptions (Options)
326
324
.setJITMemoryManager (MM)
327
325
.setOptLevel (OptLevel)
328
326
.setUseMCJIT (true )
327
+ .setAllocateGVsWithCode (false )
329
328
.create ();
330
329
331
330
if (!EE || Err != " " ) {
332
331
LLVMRustError = Err.c_str ();
333
- return false ;
332
+ return 0 ;
334
333
}
335
334
336
335
MM->invalidateInstructionCache ();
337
- Function* func = EE->FindFunctionNamed (" main " );
336
+ Function* func = EE->FindFunctionNamed (" _rust_main " );
338
337
339
338
if (!func || Err != " " ) {
340
339
LLVMRustError = Err.c_str ();
341
- return false ;
340
+ return 0 ;
342
341
}
343
342
344
- typedef int (*Entry)(int , int );
345
- Entry entry = (Entry) EE->getPointerToFunction (func);
346
-
343
+ void * entry = EE->getPointerToFunction (func);
347
344
assert (entry);
348
- entry (0 , 0 );
349
345
350
- return true ;
346
+ return entry ;
351
347
}
352
348
353
349
extern " C" bool
@@ -359,7 +355,7 @@ LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
359
355
CodeGenOpt::Level OptLevel,
360
356
bool EnableSegmentedStacks) {
361
357
362
- INITIALIZE_TARGETS ();
358
+ LLVMRustInitializeTargets ();
363
359
364
360
TargetOptions Options;
365
361
Options.NoFramePointerElim = true ;
0 commit comments