Skip to content

Commit

Permalink
CORE: Improve name mangling based on the namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Jan 9, 2024
1 parent 24973bd commit ece003d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
57 changes: 46 additions & 11 deletions core/ast/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ package ast

import (
"fireball/core/scanner"
"fmt"
"strings"
)

// NamespaceName

func (n *NamespaceName) WriteTo(sb *strings.Builder) {
for i, part := range n.Parts {
if i > 0 {
sb.WriteRune('.')
}

sb.WriteString(part.String())
}
}

// Struct

func (s *Struct) GetStaticField(name string) (int, *Field) {
Expand Down Expand Up @@ -93,14 +104,27 @@ func (f *Field) Index() int {
panic("ast.Field.Index() - Field not found")
}

func (f *Field) GetMangledName() string {
parent := ""
func (f *Field) MangledName() string {
sb := strings.Builder{}
sb.WriteString("fb$")

file := GetParent[*File](f)
file.Namespace.Name.WriteTo(&sb)

if f.IsStatic() {
sb.WriteString(":s:")
} else {
sb.WriteString(":i:")
}

sb.WriteString(f.Parent().(*Struct).Name.String())
sb.WriteRune('.')

if s, ok := f.Parent().(*Struct); ok && s.Name != nil {
parent = s.Name.String()
if f.Name != nil {
sb.WriteString(f.Name.String())
}

return fmt.Sprintf("fb$%s::%s", parent, f.Name)
return sb.String()
}

// Func
Expand Down Expand Up @@ -203,15 +227,26 @@ func (f *Func) MangledName() string {
}

// Normal
name := ""
sb := strings.Builder{}
sb.WriteString("fb$")

if f.Name != nil {
name = f.Name.String()
file := GetParent[*File](f)
file.Namespace.Name.WriteTo(&sb)

if _, ok := f.Parent().(*Impl); ok && !f.IsStatic() {
sb.WriteString(":m:")
} else {
sb.WriteString(":f:")
}

if struct_, ok := f.Parent().(*Impl); ok {
name = fmt.Sprintf("%s.%s", struct_.Struct, name)
sb.WriteString(struct_.Struct.String())
sb.WriteRune('.')
}

if f.Name != nil {
sb.WriteString(f.Name.String())
}

return "fb$" + name
return sb.String()
}
2 changes: 1 addition & 1 deletion core/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (c *codegen) createStaticVariable(field *ast.Field, external bool) exprValu
ptr := ast.Pointer{Pointee: field.Type}

llvmValue := c.module.Variable(external, c.getType(field.Type), c.getType(&ptr))
llvmValue.SetName(field.GetMangledName())
llvmValue.SetName(field.MangledName())

value := exprValue{
v: llvmValue,
Expand Down
4 changes: 2 additions & 2 deletions core/llvm/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func WriteText(module *Module, writer io.Writer) {
// Defines
for _, define := range module.defines {
w.beginFunction()
w.fmt("define %s @%s(", w.type_(define.type_.returns), define.type_.name)
w.fmt("define %s @%s(", w.type_(define.type_.returns), surroundName(define.type_.name))

for i, parameter := range define.parameters {
if i > 0 {
Expand Down Expand Up @@ -127,7 +127,7 @@ func WriteText(module *Module, writer io.Writer) {
// Declares
for _, declare := range module.declares {
w.beginFunction()
w.fmt("declare %s @%s(", w.type_(declare.returns), declare.name)
w.fmt("declare %s @%s(", w.type_(declare.returns), surroundName(declare.name))

for i, parameter := range declare.parameters {
if i > 0 {
Expand Down

0 comments on commit ece003d

Please sign in to comment.