use of com.oracle.truffle.llvm.runtime.datalayout.DataLayout in project graal by oracle.
the class InitializeGlobalNode method createGlobalVariableInitializer.
private static StaticInitsNode createGlobalVariableInitializer(LLVMParserResult parserResult, Object moduleName) {
LLVMParserRuntime runtime = parserResult.getRuntime();
GetStackSpaceFactory stackFactory = GetStackSpaceFactory.createAllocaFactory();
List<GlobalVariable> globals = parserResult.getDefinedGlobals();
DataLayout dataLayout = parserResult.getDataLayout();
LLVMStatementNode initNode;
int totalSize = 0;
try {
int[] sizes = new int[globals.size()];
int nonEmptyGlobals = 0;
for (int i = 0; i < sizes.length; i++) {
GlobalVariable global = globals.get(i);
if (global == null || global.getValue() == null) {
continue;
}
long size = globals.get(i).getType().getPointeeType().getSize(dataLayout);
if (size > Integer.MAX_VALUE || (totalSize + size) > Integer.MAX_VALUE) {
throw new TypeOverflowException("globals section > 2GB is not supported");
}
if (size == 0) {
continue;
}
sizes[i] = (int) size;
totalSize += (int) size;
nonEmptyGlobals++;
}
int[] bufferOffsets = new int[nonEmptyGlobals];
LLVMGlobal[] descriptors = new LLVMGlobal[nonEmptyGlobals];
Buffer buffer = new Buffer(totalSize, runtime, dataLayout);
int globalIndex = 0;
totalSize = 0;
for (int i = 0; i < sizes.length; i++) {
if (sizes[i] == 0) {
continue;
}
GlobalVariable global = globals.get(i);
/*
* For fetching the address of the global that we want to initialize, we must use
* the file scope because we are initializing the globals of the current file.
*/
descriptors[globalIndex] = runtime.getFileScope().getGlobalVariable(global.getName());
assert descriptors[globalIndex] != null;
bufferOffsets[globalIndex] = totalSize;
global.getValue().addToBuffer(buffer, runtime, dataLayout, stackFactory);
totalSize += sizes[i];
globalIndex++;
}
initNode = buffer.createNode(bufferOffsets, descriptors);
} catch (TypeOverflowException e) {
initNode = Type.handleOverflowStatement(e);
}
return StaticInitsNodeGen.create(new LLVMStatementNode[] { initNode }, "global variable initializers", moduleName);
}
Aggregations