public void AddValueSourceForVariable(VariableReference variableReference, ValueSource valueSource) { _variableValues[variableReference] = valueSource; }
public bool VisitConstant(Constant constant) { ValueSource outputAllocation = GetTerminalValueSource(constant.OutputTerminal); if (constant.DataType.IsInteger()) { LLVMValueRef constantValueRef; switch (constant.DataType.GetKind()) { case NITypeKind.Int8: constantValueRef = ((sbyte)constant.Value).AsLLVMValue(); break; case NITypeKind.UInt8: constantValueRef = ((byte)constant.Value).AsLLVMValue(); break; case NITypeKind.Int16: constantValueRef = ((short)constant.Value).AsLLVMValue(); break; case NITypeKind.UInt16: constantValueRef = ((ushort)constant.Value).AsLLVMValue(); break; case NITypeKind.Int32: constantValueRef = ((int)constant.Value).AsLLVMValue(); break; case NITypeKind.UInt32: constantValueRef = ((uint)constant.Value).AsLLVMValue(); break; case NITypeKind.Int64: constantValueRef = ((long)constant.Value).AsLLVMValue(); break; case NITypeKind.UInt64: constantValueRef = ((ulong)constant.Value).AsLLVMValue(); break; default: throw new NotSupportedException("Unsupported numeric constant type: " + constant.DataType); } outputAllocation.UpdateValue(_builder, constantValueRef); } else if (constant.Value is bool) { LLVMValueRef constantValueRef = ((bool)constant.Value).AsLLVMValue(); outputAllocation.UpdateValue(_builder, constantValueRef); } else if (constant.Value is string) { VariableReference output = constant.OutputTerminal.GetTrueVariable(); if (output.Type.IsRebarReferenceType() && output.Type.GetReferentType() == DataTypes.StringSliceType) { string stringValue = (string)constant.Value; int length = Encoding.UTF8.GetByteCount(stringValue); LLVMValueRef stringValueConstant = LLVMSharp.LLVM.ConstString(stringValue, (uint)length, true); LLVMValueRef stringConstantPtr = Module.AddGlobal(stringValueConstant.TypeOf(), $"string{constant.UniqueId}"); stringConstantPtr.SetInitializer(stringValueConstant); LLVMValueRef castPointer = _builder.CreateBitCast( stringConstantPtr, LLVMTypeRef.PointerType(LLVMTypeRef.Int8Type(), 0), "ptrCast"); LLVMValueRef[] stringSliceFields = new LLVMValueRef[] { castPointer, length.AsLLVMValue() }; LLVMValueRef stringSliceValue = LLVMValueRef.ConstStruct(stringSliceFields, false); outputAllocation.UpdateValue(_builder, stringSliceValue); } } else { throw new NotImplementedException(); } return(true); }
public static void UpdateDereferencedValue(this ValueSource valueSource, IRBuilder builder, LLVMValueRef value) { builder.CreateStore(value, valueSource.GetValue(builder)); }
private static void CompileOutput(FunctionCompiler compiler, FunctionalNode outputNode) { ValueSource inputValueSource = compiler.GetTerminalValueSource(outputNode.InputTerminals[0]); VariableReference input = outputNode.InputTerminals[0].GetTrueVariable(); NIType referentType = input.Type.GetReferentType(); if (referentType.IsBoolean()) { LLVMValueRef value = inputValueSource.GetDeferencedValue(compiler._builder); compiler._builder.CreateCall(compiler._commonExternalFunctions.OutputBoolFunction, new LLVMValueRef[] { value }, string.Empty); return; } if (referentType.IsInteger()) { LLVMValueRef outputFunction; switch (referentType.GetKind()) { case NITypeKind.Int8: outputFunction = compiler._commonExternalFunctions.OutputInt8Function; break; case NITypeKind.UInt8: outputFunction = compiler._commonExternalFunctions.OutputUInt8Function; break; case NITypeKind.Int16: outputFunction = compiler._commonExternalFunctions.OutputInt16Function; break; case NITypeKind.UInt16: outputFunction = compiler._commonExternalFunctions.OutputUInt16Function; break; case NITypeKind.Int32: outputFunction = compiler._commonExternalFunctions.OutputInt32Function; break; case NITypeKind.UInt32: outputFunction = compiler._commonExternalFunctions.OutputUInt32Function; break; case NITypeKind.Int64: outputFunction = compiler._commonExternalFunctions.OutputInt64Function; break; case NITypeKind.UInt64: outputFunction = compiler._commonExternalFunctions.OutputUInt64Function; break; default: throw new NotImplementedException($"Don't know how to display type {referentType} yet."); } LLVMValueRef value = inputValueSource.GetDeferencedValue(compiler._builder); compiler._builder.CreateCall(outputFunction, new LLVMValueRef[] { value }, string.Empty); return; } if (referentType.IsString()) { // TODO: this should go away once auto-borrowing into string slices works // call output_string with string pointer and size LLVMValueRef stringPtr = inputValueSource.GetValue(compiler._builder), stringSlice = compiler._builder.CreateCall( compiler.GetImportedCommonFunction(CommonModules.StringToSliceRetName), new LLVMValueRef[] { stringPtr }, "stringSlice"); compiler._builder.CreateCall( compiler.GetImportedCommonFunction(CommonModules.OutputStringSliceName), new LLVMValueRef[] { stringSlice }, string.Empty); return; } if (referentType == DataTypes.StringSliceType) { compiler.CreateCallForFunctionalNode(compiler.GetImportedCommonFunction(CommonModules.OutputStringSliceName), outputNode); return; } else { throw new NotImplementedException($"Don't know how to display type {referentType} yet."); } }