public static LLVMTypeRef AsLLVMType(this NIType niType) { if (niType.IsInteger()) { switch (niType.GetKind()) { case NITypeKind.UInt8: case NITypeKind.Int8: return(LLVMTypeRef.Int8Type()); case NITypeKind.UInt16: case NITypeKind.Int16: return(LLVMTypeRef.Int16Type()); case NITypeKind.UInt32: case NITypeKind.Int32: return(LLVMTypeRef.Int32Type()); case NITypeKind.UInt64: case NITypeKind.Int64: return(LLVMTypeRef.Int64Type()); } } if (niType.IsBoolean()) { return(LLVMTypeRef.Int1Type()); } if (niType.IsString()) { return(StringType); } if (niType.IsRebarReferenceType()) { NIType referentType = niType.GetReferentType(); if (referentType == DataTypes.StringSliceType) { return(StringSliceReferenceType); } return(LLVMTypeRef.PointerType(referentType.AsLLVMType(), 0u)); } if (niType == DataTypes.FileHandleType) { return(FileHandleType); } NIType innerType; if (niType.TryDestructureOptionType(out innerType)) { return(CreateLLVMOptionType(innerType.AsLLVMType())); } if (niType == DataTypes.RangeIteratorType) { return(RangeIteratorType); } throw new NotSupportedException("Unsupported type: " + niType); }
public override LLVMValueRef Emit(EmittingContext pContext) { LLVMValueRef _main = Module.Emit(pContext); //Emit our function that the runtime will call. //This will just call the method marked with "@run" //The reason we do this is so we have a static method name we can call var main = pContext.EmitMethodHeader("_main", LLVMTypeRef.Int32Type(), new LLVMTypeRef[] { }); var mainB = main.AppendBasicBlock(""); LLVM.PositionBuilderAtEnd(pContext.Builder, mainB); LLVM.BuildCall(pContext.Builder, _main, new LLVMValueRef[] { }, ""); LLVM.BuildRet(pContext.Builder, pContext.GetInt(0)); pContext.ValidateMethod(main); return(default);
public static LLVMTypeRef ToLLVMType(this Type type) { if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Vector128 <>)) { var et = type.GetGenericArguments()[0]; return(LLVMTypeRef.VectorType(et.ToLLVMType(), 16U / (uint)Marshal.SizeOf(et))); } if (typeof(MulticastDelegate).IsAssignableFrom(type)) { var mi = type.GetMethod("Invoke"); return(LLVMTypeRef.FunctionType(mi.ReturnType.ToLLVMType(), mi.GetParameters().Select(x => x.ParameterType.ToLLVMType()).ToArray(), false)); } if (type == typeof(void)) { return(LLVMTypeRef.VoidType()); } if (type.IsPointer) { return(LLVMTypeRef.Int64Type()); } switch (Activator.CreateInstance(type)) { case sbyte _: case byte _: return(LLVMTypeRef.Int8Type()); case short _: case ushort _: return(LLVMTypeRef.Int16Type()); case int _: case uint _: return(LLVMTypeRef.Int32Type()); case long _: case ulong _: return(LLVMTypeRef.Int64Type()); case Int128 _: case UInt128 _: return(LLVMTypeRef.IntType(128)); case float _: return(LLVMTypeRef.FloatType()); case double _: return(LLVMTypeRef.DoubleType()); case bool _: return(LLVMTypeRef.IntType(1)); default: throw new NotSupportedException(type.Name); } }
private static LLVMTypeRef GetLlvmType([NotNull] Type lambdaTypeParam) { switch (lambdaTypeParam) { case PrimaryType primaryType: if (string.Equals(primaryType.Name, "f64", Ordinal)) { return(LLVMSharp.LLVM.DoubleType()); } if (string.Equals(primaryType.Name, "f32", Ordinal)) { return(LLVMTypeRef.FloatType()); } if (string.Equals(primaryType.Name, "i8", Ordinal)) { return(LLVMTypeRef.Int8Type()); } if (string.Equals(primaryType.Name, "i16", Ordinal)) { return(LLVMTypeRef.Int16Type()); } if (string.Equals(primaryType.Name, "i32", Ordinal)) { return(LLVMTypeRef.Int32Type()); } if (string.Equals(primaryType.Name, "i64", Ordinal)) { return(LLVMTypeRef.Int64Type()); } break; case LambdaType lambdaType: return(LLVMTypeRef.FunctionType(GetLlvmType(lambdaType.RetType), (from type in lambdaType.ParamsList select GetLlvmType(type)).ToArray(), false)); case SecondaryType secondaryType: break; } throw new NotImplementedException(); }
private static void CreateRangeModule(Module rangeModule) { CommonModuleSignatures[RangeIteratorNextName] = LLVMSharp.LLVM.FunctionType( LLVMTypeRef.Int32Type().CreateLLVMOptionType(), new LLVMTypeRef[] { LLVMTypeRef.PointerType(LLVMExtensions.RangeIteratorType, 0) }, false); BuildRangeIteratorNextFunction(rangeModule); CommonModuleSignatures[CreateRangeIteratorName] = LLVMSharp.LLVM.FunctionType( LLVMTypeRef.Int32Type().CreateLLVMOptionType(), new LLVMTypeRef[] { LLVMTypeRef.Int32Type(), LLVMTypeRef.Int32Type(), LLVMTypeRef.PointerType(LLVMExtensions.RangeIteratorType, 0) }, false); BuildCreateRangeIteratorFunction(rangeModule); }
public void Emit(Emitting.EmittingContext pContext) { EmitReferencedNodes(pContext); pContext.Cache = Cache; pContext.Locals.AddScope(); //Emit our function that the runtime will call. //This will just call the method marked with "@run" //The reason we do this is so we have a static method name we can call var main = pContext.EmitMethodHeader("_main", LLVMTypeRef.Int32Type(), new LLVMTypeRef[] { }); var mainB = main.AppendBasicBlock(""); LLVMValueRef _main = Module.Emit(pContext); LLVM.PositionBuilderAtEnd(pContext.Builder, mainB); LLVM.BuildCall(pContext.Builder, _main, new LLVMValueRef[] { }, ""); LLVM.BuildRet(pContext.Builder, pContext.GetInt(0)); pContext.ValidateMethod(main); pContext.Locals.RemoveScope(); }
private static void BuildWriteStringToFileHandleFunction(Module fileModule, CommonExternalFunctions externalFunctions) { LLVMValueRef writeStringToFileHandleFunction = fileModule.AddFunction(WriteStringToFileHandleName, CommonModuleSignatures[WriteStringToFileHandleName]); LLVMBasicBlockRef entryBlock = writeStringToFileHandleFunction.AppendBasicBlock("entry"); var builder = new IRBuilder(); builder.PositionBuilderAtEnd(entryBlock); LLVMValueRef fileHandleStructPtr = writeStringToFileHandleFunction.GetParam(0u), fileHandlePtr = builder.CreateStructGEP(fileHandleStructPtr, 0u, "fileHandlePtr"), fileHandle = builder.CreateLoad(fileHandlePtr, "fileHandle"), stringSlice = writeStringToFileHandleFunction.GetParam(1u), stringSliceAllocationPtr = builder.CreateExtractValue(stringSlice, 0u, "stringSliceAllocationPtr"), stringSliceLength = builder.CreateExtractValue(stringSlice, 1u, "stringSliceLength"), bytesWrittenPtr = builder.CreateAlloca(LLVMTypeRef.Int32Type(), "bytesWrittenPtr"); builder.CreateCall( externalFunctions.WriteFileFunction, new LLVMValueRef[] { fileHandle, stringSliceAllocationPtr, stringSliceLength, bytesWrittenPtr, LLVMExtensions.NullVoidPointer }, "writeFileResult"); builder.CreateRetVoid(); }
private static void BuildRangeIteratorNextFunction(Module rangeModule) { LLVMValueRef rangeIteratorNextFunction = rangeModule.AddFunction(RangeIteratorNextName, CommonModuleSignatures[RangeIteratorNextName]); LLVMBasicBlockRef entryBlock = rangeIteratorNextFunction.AppendBasicBlock("entry"); var builder = new IRBuilder(); builder.PositionBuilderAtEnd(entryBlock); LLVMValueRef rangeIteratorPtr = rangeIteratorNextFunction.GetParam(0u), rangeCurrentPtr = builder.CreateStructGEP(rangeIteratorPtr, 0u, "rangeCurrentPtr"), rangeHighPtr = builder.CreateStructGEP(rangeIteratorPtr, 1u, "rangeHighPtr"), rangeCurrent = builder.CreateLoad(rangeCurrentPtr, "rangeCurrent"), rangeHigh = builder.CreateLoad(rangeHighPtr, "rangeHigh"), rangeCurrentInc = builder.CreateAdd(rangeCurrent, 1.AsLLVMValue(), "rangeCurrentInc"); builder.CreateStore(rangeCurrentInc, rangeCurrentPtr); LLVMValueRef inRange = builder.CreateICmp(LLVMIntPredicate.LLVMIntSLT, rangeCurrentInc, rangeHigh, "inRange"); LLVMTypeRef optionType = LLVMTypeRef.Int32Type().CreateLLVMOptionType(); LLVMValueRef option0 = builder.CreateInsertValue(LLVMSharp.LLVM.GetUndef(optionType), inRange, 0u, "option0"), option1 = builder.CreateInsertValue(option0, rangeCurrentInc, 1u, "option1"); builder.CreateRet(option1); }
internal static LLVMValueRef MakeFatPointer(LLVMBuilderRef builder, LLVMValueRef targetLlvmFunction, WebAssemblyCodegenCompilation compilation) { var asInt = LLVM.BuildPtrToInt(builder, targetLlvmFunction, LLVMTypeRef.Int32Type(), "toInt"); return(LLVM.BuildBinOp(builder, LLVMOpcode.LLVMOr, asInt, LLVM.ConstInt(LLVM.Int32Type(), (ulong)compilation.TypeSystemContext.Target.FatFunctionPointerOffset, LLVMMisc.False), "makeFat")); }
public LLVMModuleRef GenerateLLVM() { LLVMTypeRef stringType = LLVMTypeRef.PointerType(LLVMTypeRef.Int8Type(), 0); var printfArguments = new LLVMTypeRef[] { stringType }; var printf = LLVM.AddFunction(_module, "printf", LLVM.FunctionType(LLVMTypeRef.Int32Type(), printfArguments, LLVMBoolTrue)); LLVM.SetLinkage(printf, LLVMLinkage.LLVMExternalLinkage); var scanfArguments = new LLVMTypeRef[] { stringType }; var scanf = LLVM.AddFunction(_module, "scanf", LLVM.FunctionType(LLVMTypeRef.Int32Type(), scanfArguments, LLVMBoolTrue)); LLVM.SetLinkage(scanf, LLVMLinkage.LLVMExternalLinkage); // Generate functions, globals, etc. for (int i = 0; i < _statements.Count; i++) { _statements[i].Accept(this); } // Generate everything inside the functions while (_valueStack.Count > 0) { _valueStack.Peek().Item2.Accept(this); } return(_module); }
public CommonExternalFunctions(Module addTo) { LLVMTypeRef bytePointerType = LLVMTypeRef.PointerType(LLVMTypeRef.Int8Type(), 0); // NB: this will get resolved to the Win32 RtlCopyMemory function. LLVMTypeRef copyMemoryFunctionType = LLVMSharp.LLVM.FunctionType( LLVMSharp.LLVM.VoidType(), new LLVMTypeRef[] { bytePointerType, bytePointerType, LLVMTypeRef.Int64Type() }, false); CopyMemoryFunction = addTo.AddFunction("CopyMemory", copyMemoryFunctionType); CopyMemoryFunction.SetLinkage(LLVMLinkage.LLVMExternalLinkage); OutputBoolFunction = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int1Type(), "output_bool"); OutputInt8Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int8Type(), "output_int8"); OutputUInt8Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int8Type(), "output_uint8"); OutputInt16Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int16Type(), "output_int16"); OutputUInt16Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int16Type(), "output_uint16"); OutputInt32Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int32Type(), "output_int32"); OutputUInt32Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int32Type(), "output_uint32"); OutputInt64Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int64Type(), "output_int64"); OutputUInt64Function = CreateSingleParameterVoidFunction(addTo, LLVMTypeRef.Int64Type(), "output_uint64"); LLVMTypeRef outputStringFunctionType = LLVMSharp.LLVM.FunctionType( LLVMSharp.LLVM.VoidType(), new LLVMTypeRef[] { bytePointerType, LLVMTypeRef.Int32Type() }, false); OutputStringFunction = addTo.AddFunction("output_string", outputStringFunctionType); OutputStringFunction.SetLinkage(LLVMLinkage.LLVMExternalLinkage); LLVMTypeRef closeHandleFunctionType = LLVMSharp.LLVM.FunctionType( LLVMTypeRef.Int32Type(), // bool new LLVMTypeRef[] { LLVMExtensions.VoidPointerType }, false); CloseHandleFunction = addTo.AddFunction("CloseHandle", closeHandleFunctionType); LLVMTypeRef createFileAFunctionType = LLVMSharp.LLVM.FunctionType( LLVMExtensions.VoidPointerType, new LLVMTypeRef[] { bytePointerType, // filename LLVMTypeRef.Int32Type(), // access LLVMTypeRef.Int32Type(), // share LLVMExtensions.VoidPointerType, // securityAttributes LLVMTypeRef.Int32Type(), // creationDisposition LLVMTypeRef.Int32Type(), // flagsAndAttributes LLVMExtensions.VoidPointerType, // templateFile }, false); CreateFileAFunction = addTo.AddFunction("CreateFileA", createFileAFunctionType); LLVMTypeRef readFileFunctionType = LLVMSharp.LLVM.FunctionType( LLVMTypeRef.Int32Type(), // bool new LLVMTypeRef[] { LLVMExtensions.VoidPointerType, // hFile LLVMExtensions.VoidPointerType, // lpBuffer LLVMTypeRef.Int32Type(), // nNumberOfBytesToRead LLVMTypeRef.PointerType(LLVMTypeRef.Int32Type(), 0), // lpNumberOfBytesRead LLVMExtensions.VoidPointerType, // lpOverlapped }, false); ReadFileFunction = addTo.AddFunction("ReadFile", readFileFunctionType); LLVMTypeRef writeFileFunctionType = LLVMSharp.LLVM.FunctionType( LLVMTypeRef.Int32Type(), // bool new LLVMTypeRef[] { LLVMExtensions.VoidPointerType, // hFile LLVMExtensions.VoidPointerType, // lpBuffer LLVMTypeRef.Int32Type(), // nNumberOfBytesToWrite, LLVMTypeRef.PointerType(LLVMTypeRef.Int32Type(), 0), // lpNumberOfBytesWritten LLVMExtensions.VoidPointerType, // lpOverlapped }, false); WriteFileFunction = addTo.AddFunction("WriteFile", writeFileFunctionType); }
private static void BuildReadLineFromFileHandleFunction(Module fileModule, CommonExternalFunctions externalFunctions) { LLVMValueRef readLineFromFileHandleFunction = fileModule.AddFunction(ReadLineFromFileHandleName, CommonModuleSignatures[ReadLineFromFileHandleName]); LLVMBasicBlockRef entryBlock = readLineFromFileHandleFunction.AppendBasicBlock("entry"); var builder = new IRBuilder(); builder.PositionBuilderAtEnd(entryBlock); LLVMBasicBlockRef loopStartBlock = readLineFromFileHandleFunction.AppendBasicBlock("loopStart"), handleByteBlock = readLineFromFileHandleFunction.AppendBasicBlock("handleByte"), byteIsCRBlock = readLineFromFileHandleFunction.AppendBasicBlock("byteIsCR"), byteIsNotCRBlock = readLineFromFileHandleFunction.AppendBasicBlock("byteIsNotCR"), notNewLineBlock = readLineFromFileHandleFunction.AppendBasicBlock("notNewLine"), appendCRBlock = readLineFromFileHandleFunction.AppendBasicBlock("appendCR"), appendByteBlock = readLineFromFileHandleFunction.AppendBasicBlock("appendByte"), loopEndBlock = readLineFromFileHandleFunction.AppendBasicBlock("loopEnd"), nonEmptyStringBlock = readLineFromFileHandleFunction.AppendBasicBlock("nonEmptyString"), emptyStringBlock = readLineFromFileHandleFunction.AppendBasicBlock("emptyString"); LLVMValueRef fileHandlePtr = readLineFromFileHandleFunction.GetParam(0u), stringPtr = builder.CreateAlloca(LLVMExtensions.StringType, "stringPtr"), carriageReturnPtr = builder.CreateAlloca(LLVMTypeRef.Int8Type(), "carriageReturnPtr"), carriageReturn = ((byte)0xD).AsLLVMValue(), byteReadPtr = builder.CreateAlloca(LLVMTypeRef.Int8Type(), "byteReadPtr"), bytesReadPtr = builder.CreateAlloca(LLVMTypeRef.Int32Type(), "bytesReadPtr"), nonEmptyStringPtr = builder.CreateAlloca(LLVMTypeRef.Int1Type(), "nonEmptyStringPtr"), seenCRPtr = builder.CreateAlloca(LLVMTypeRef.Int1Type(), "seenCRPtr"); builder.CreateStore(carriageReturn, carriageReturnPtr); builder.CreateStore(false.AsLLVMValue(), seenCRPtr); builder.CreateStore(false.AsLLVMValue(), nonEmptyStringPtr); builder.CreateCall(_createEmptyStringFunction, new LLVMValueRef[] { stringPtr }, string.Empty); builder.CreateBr(loopStartBlock); builder.PositionBuilderAtEnd(loopStartBlock); LLVMValueRef hFilePtr = builder.CreateStructGEP(fileHandlePtr, 0u, "hFilePtr"), hFile = builder.CreateLoad(hFilePtr, "hFile"); LLVMValueRef readFileResult = builder.CreateCall( externalFunctions.ReadFileFunction, new LLVMValueRef[] { hFile, byteReadPtr, 1.AsLLVMValue(), bytesReadPtr, LLVMExtensions.NullVoidPointer }, "readFileResult"), readFileResultBool = builder.CreateICmp(LLVMIntPredicate.LLVMIntNE, readFileResult, 0.AsLLVMValue(), "readFileResultBool"), bytesRead = builder.CreateLoad(bytesReadPtr, "bytesRead"), zeroBytesRead = builder.CreateICmp(LLVMIntPredicate.LLVMIntEQ, bytesRead, 0.AsLLVMValue(), "zeroBytesRead"), eof = builder.CreateAnd(readFileResultBool, zeroBytesRead, "eof"); builder.CreateCondBr(eof, loopEndBlock, handleByteBlock); builder.PositionBuilderAtEnd(handleByteBlock); LLVMValueRef byteRead = builder.CreateLoad(byteReadPtr, "byteRead"), byteReadIsCR = builder.CreateICmp(LLVMIntPredicate.LLVMIntEQ, byteRead, carriageReturn, "byteReadIsCR"); builder.CreateCondBr(byteReadIsCR, byteIsCRBlock, byteIsNotCRBlock); builder.PositionBuilderAtEnd(byteIsCRBlock); builder.CreateStore(true.AsLLVMValue(), seenCRPtr); builder.CreateBr(loopStartBlock); builder.PositionBuilderAtEnd(byteIsNotCRBlock); LLVMValueRef byteIsLF = builder.CreateICmp(LLVMIntPredicate.LLVMIntEQ, byteRead, ((byte)0xA).AsLLVMValue(), "byteIsLF"), seenCR = builder.CreateLoad(seenCRPtr, "seenCR"), newLine = builder.CreateAnd(byteIsLF, seenCR, "newLine"); builder.CreateCondBr(newLine, loopEndBlock, notNewLineBlock); builder.PositionBuilderAtEnd(notNewLineBlock); builder.CreateCondBr(seenCR, appendCRBlock, appendByteBlock); builder.PositionBuilderAtEnd(appendCRBlock); LLVMValueRef crSlice = builder.BuildStringSliceReferenceValue(carriageReturnPtr, 1.AsLLVMValue()); builder.CreateCall(_stringAppendFunction, new LLVMValueRef[] { stringPtr, crSlice }, string.Empty); builder.CreateBr(appendByteBlock); builder.PositionBuilderAtEnd(appendByteBlock); LLVMValueRef byteSlice = builder.BuildStringSliceReferenceValue(byteReadPtr, 1.AsLLVMValue()); builder.CreateCall(_stringAppendFunction, new LLVMValueRef[] { stringPtr, byteSlice }, string.Empty); builder.CreateStore(true.AsLLVMValue(), nonEmptyStringPtr); builder.CreateStore(false.AsLLVMValue(), seenCRPtr); builder.CreateBr(loopStartBlock); builder.PositionBuilderAtEnd(loopEndBlock); LLVMValueRef optionStringPtr = readLineFromFileHandleFunction.GetParam(1u), optionStringIsSomePtr = builder.CreateStructGEP(optionStringPtr, 0u, "optionStringIsSomePtr"), nonEmptyString = builder.CreateLoad(nonEmptyStringPtr, "nonEmptyString"); builder.CreateCondBr(nonEmptyString, nonEmptyStringBlock, emptyStringBlock); builder.PositionBuilderAtEnd(nonEmptyStringBlock); builder.CreateStore(true.AsLLVMValue(), optionStringIsSomePtr); LLVMValueRef optionStringInnerValuePtr = builder.CreateStructGEP(optionStringPtr, 1u, "optionStringInnerValuePtr"), stringValue = builder.CreateLoad(stringPtr, "string"); builder.CreateStore(stringValue, optionStringInnerValuePtr); builder.CreateRetVoid(); builder.PositionBuilderAtEnd(emptyStringBlock); builder.CreateStore(false.AsLLVMValue(), optionStringIsSomePtr); builder.CreateCall(_dropStringFunction, new LLVMValueRef[] { stringPtr }, string.Empty); builder.CreateRetVoid(); }
public static LLVMValueRef AsLLVMValue(this uint intValue) { return(LLVMSharp.LLVM.ConstInt(LLVMTypeRef.Int32Type(), intValue, false)); }
public static LLVMValueRef AsLLVMValue(this int intValue) { return(LLVMSharp.LLVM.ConstInt(LLVMTypeRef.Int32Type(), (ulong)intValue, true)); }