From f39edcca0c2c7002e9f0a3238226b90f7a9cab74 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 27 Jan 2024 01:47:52 +0000 Subject: [PATCH 1/4] Add function to encode global identifier --- llvm/encode.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/llvm/encode.go b/llvm/encode.go index 861cf5a..c8b373d 100644 --- a/llvm/encode.go +++ b/llvm/encode.go @@ -117,10 +117,14 @@ func EncodeRegisterName (name string) string { return "%" + EscapeIdent(name) } -func EncodeFunctionName (name string) string { +func EncodeGlobalName (name string) string { return "@" + EscapeIdent(name) } +func EncodeFunctionName (name string) string { + return EncodeGlobalName(name) +} + func EncodeLabelName (name string) string { return EscapeIdent(name) + ":" } From 417c7772e4ea91210bf3fa1ee218fe7f4325a21d Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 27 Jan 2024 01:48:29 +0000 Subject: [PATCH 2/4] Move addrspace to attribute file, will put more things in there --- llvm/attribute.go | 9 +++++++++ llvm/type.go | 6 ------ 2 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 llvm/attribute.go diff --git a/llvm/attribute.go b/llvm/attribute.go new file mode 100644 index 0000000..ff1dfa1 --- /dev/null +++ b/llvm/attribute.go @@ -0,0 +1,9 @@ +package llvm + +import "fmt" + +type AddressSpace uint64 + +func (space AddressSpace) String () string { + return fmt.Sprintf("addrspace(%d)", space) +} diff --git a/llvm/type.go b/llvm/type.go index de81b6e..6bba3f3 100644 --- a/llvm/type.go +++ b/llvm/type.go @@ -174,12 +174,6 @@ func (this *TypeMetadata) String () string { return this.LLString() } -type AddressSpace uint64 - -func (space AddressSpace) String () string { - return fmt.Sprintf("addrspace(%d)", space) -} - type TypePointer struct { AbstractType AddressSpace AddressSpace From 14a02f5e22c57c5ac7c2e3fb4274f230e86d95b4 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 27 Jan 2024 01:49:18 +0000 Subject: [PATCH 3/4] Add basic globals implementation --- llvm/global.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ llvm/module.go | 22 ++++++++++++++---- 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 llvm/global.go diff --git a/llvm/global.go b/llvm/global.go new file mode 100644 index 0000000..d932c71 --- /dev/null +++ b/llvm/global.go @@ -0,0 +1,61 @@ +package llvm + +import "fmt" +import "strings" + +type Global struct { + Ty Type + GlobalName string + Constant bool + Initial Const + + AddressSpace AddressSpace + + // TODO complete this +} + +func (this *Global) Type () Type { + return &TypePointer { + AddressSpace: this.AddressSpace, + } +} + +func (this *Global) Named () bool { + return this.GlobalName != "" +} + +func (this *Global) Name () string { + return this.GlobalName +} + +func (this *Global) Identifier () string { + return EncodeGlobalName(this.Name()) +} + +func (this *Global) SetName (name string) { + this.GlobalName = name +} + +func (this *Global) String () string { + return fmt.Sprintf("%v %v", this.Ty, this.Identifier()) +} + +func (this *Global) LLString () string { + buffer := &strings.Builder { } + buffer.WriteString(this.Name()) + buffer.WriteString(" = ") + if this.AddressSpace != 0 { + fmt.Fprintf(buffer, " %v", this.AddressSpace) + } + if this.Constant { + buffer.WriteString(" constant") + } else { + buffer.WriteString(" global") + } + fmt.Fprintf(buffer, " %v", this.Ty) + if this.Initial != nil { + fmt.Fprintf(buffer, " %v", this.Initial) + } + buffer.WriteString("\n") + return buffer.String() +} diff --git a/llvm/module.go b/llvm/module.go index 7bfb614..2574a58 100644 --- a/llvm/module.go +++ b/llvm/module.go @@ -5,6 +5,7 @@ import "fmt" type Module struct { Types []Type + Globals []*Global Functions []*Function } @@ -17,6 +18,10 @@ func (this *Module) WriteTo (writer io.Writer) (wrote int64, err error) { err = write(fmt.Fprintf(writer, "%v = type %v\n", ty, ty.LLString())) if err != nil { return wrote, err } } + for _, global := range this.Globals { + err = write(fmt.Fprint(writer, global.LLString())) + if err != nil { return wrote, err } + } for _, function := range this.Functions { err = write(fmt.Fprint(writer, function.LLString())) if err != nil { return wrote, err } @@ -25,6 +30,12 @@ func (this *Module) WriteTo (writer io.Writer) (wrote int64, err error) { return wrote, err } +func (this *Module) NewType (name string, ty Type) Type { + ty.SetName(name) + this.Types = append(this.Types, ty) + return ty +} + func (this *Module) NewFunction (name string, retur Type, parameters ...*Parameter) *Function { paramTypes := make([]Type, len(parameters)) for index, parameter := range parameters { @@ -43,10 +54,13 @@ func (this *Module) NewFunction (name string, retur Type, parameters ...*Paramet return function } -func (this *Module) NewType (name string, ty Type) Type { - ty.SetName(name) - this.Types = append(this.Types, ty) - return ty +func (this *Module) NewGlobal (name string, ty Type) *Global { + global := &Global { + Ty: ty, + GlobalName: name, + } + this.Globals = append(this.Globals, global) + return global } type Align uint64 From d6a5ded758537782e813338c7d5840e3eb32536a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 27 Jan 2024 04:16:41 +0000 Subject: [PATCH 4/4] Add IsConstant to Global --- llvm/global.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/llvm/global.go b/llvm/global.go index d932c71..f02d8af 100644 --- a/llvm/global.go +++ b/llvm/global.go @@ -14,6 +14,12 @@ type Global struct { // TODO complete this } +func (this *Global) IsConstant () { + // FIXME globals are not always constant... perhaps there is a better + // way of doing this? maybe making a separate type ConstGlobal that + // embeds Global? would have to do away with Constant member. +} + func (this *Global) Type () Type { return &TypePointer { AddressSpace: this.AddressSpace,