fspl/llvm/module.go

71 lines
1.6 KiB
Go

package llvm
import "io"
import "fmt"
type Module struct {
Types []Type
Globals []*Global
Functions []*Function
}
func (this *Module) WriteTo (writer io.Writer) (wrote int64, err error) {
write := func (n int, err error) error {
wrote += int64(n)
return err
}
for _, ty := range this.Types {
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 }
}
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 {
paramTypes[index] = parameter.Type()
}
function := &Function {
FunctionName: name,
Signature: &TypeFunction {
Return: retur,
Parameters: paramTypes,
},
Parameters: parameters,
}
this.Functions = append(this.Functions, function)
return function
}
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
func (align Align) String () string {
return fmt.Sprintf("align %d", align)
}