private int GetTypeSize(LatteParser.TypeContext type) { return(type switch { LatteParser.TBoolContext boolContext => 1, LatteParser.TIntContext intContext => 4, LatteParser.TStringContext stringContext => 8, LatteParser.TTypeNameContext typeNameContext => 8, LatteParser.TVoidContext voidContext => throw new NotSupportedException(), _ => throw new ArgumentOutOfRangeException(nameof(type)) });
public static string Type(LatteParser.TypeContext typeContext) { return(typeContext switch { LatteParser.TBoolContext boolContext => "i1", LatteParser.TIntContext intContext => "i32", LatteParser.TStringContext stringContext => "i8*", LatteParser.TTypeNameContext typeNameContext => $"%{typeNameContext.GetText()}*", LatteParser.TVoidContext voidContext => "void", _ => throw new ArgumentOutOfRangeException(nameof(typeContext)) });
public void GenerateConstructor(ClassDef @classDef) { _llvmGenerator.Emit($"define void @g_{@classDef.Id}_construct(%{@classDef.Id}* %this) " + "{"); if (!_globalState.LiteralToStringConstId.ContainsKey("")) { _globalState.LiteralToStringConstId[""] = _globalState.NewString; } var emptyStr = _globalState.LiteralToStringConstId[""]; int register = 1, counter = @classDef.OwnFieldsStartIndex; if (counter > 0) { _llvmGenerator.Emit($"%r{register} = bitcast %{@classDef.Id}* %this to %{@classDef.ParentId}*"); _llvmGenerator.Emit($"call void @g_{@classDef.ParentId}_construct(%{@classDef.ParentId}* %r{register})"); register++; } _llvmGenerator.Emit($"%r0 = getelementptr %{@classDef.Id}, %{@classDef.Id}* %this, i32 0, i32 0"); _llvmGenerator.Emit($"store %{@classDef.Id}_vtable* @{@classDef.Id}_vtable_ptrs, %{@classDef.Id}_vtable** %r0"); @classDef.Fields.Skip(counter).ToList().ForEach(field => { _llvmGenerator.Emit($"%r{register} = getelementptr %{@classDef.Id}, %{@classDef.Id}* %this, i32 0, i32 {counter + 1}"); _llvmGenerator.Emit( $"store {AstToLlvmString.Type(field.Value.Type)} " + $@"{(field.Value.Type switch { LatteParser.TStringContext _ => emptyStr, LatteParser.TTypeNameContext _ => "null", _ => "0" })}, " + $"{AstToLlvmString.Type(field.Value.Type)}* %r{register}"); register++; counter++; });