// BuildPkg builds a package and returns the built lib func BuildPkg(p *Pkg) (*link8.Pkg, []*lex8.Error) { p.strPool.declare(p.lib) p.datPool.declare(p.lib) for _, v := range p.vars { var align uint32 = regSize if v.size <= 1 { align = 1 } obj := link8.NewVar(align) obj.Zeros(uint32(v.size)) p.lib.DefineVar(v.name, obj) } for _, f := range p.funcs { genFunc(p.g, f) } if errs := p.g.Errs(); errs != nil { return nil, errs } for _, f := range p.funcs { writeFunc(p, f) } if p.tests != nil { v := link8.NewVar(regSize) for _, f := range p.tests.funcs { if err := v.WriteLink(p.path, f.name); err != nil { panic(err) } } p.lib.DefineVar(p.tests.name, v) } return p.lib, nil }
func (p *strPool) declare(lib *link8.Pkg) { if lib.Path() != p.pkg { panic("package name mismatch") } ndigit := countDigit(len(p.strs)) nfmt := fmt.Sprintf(":str_%%0%dd", ndigit) for i, s := range p.strs { s.name = fmt.Sprintf(nfmt, i) s.pkg = p.pkg v := link8.NewVar(0) v.Write([]byte(s.str)) lib.DeclareVar(s.name) lib.DefineVar(s.name, v) } }
func buildVar(b *builder, v *varDecl) *link8.Var { if varSize(v) == 0 { b.Errorf(v.Name.Pos, "var %q has no data", v.Name.Lit) return nil } ret := link8.NewVar(varAlign(v)) for _, stmt := range v.stmts { n := ret.Size() if stmt.align == 4 && n%4 != 0 { ret.Pad(4 - n%4) } ret.Write(stmt.data) } if ret.TooLarge() { b.Errorf(v.Name.Pos, "var %q too large", v.Name.Lit) return nil } return ret }
func (p *datPool) declare(lib *link8.Pkg) { if lib.Path() != p.pkg { panic("package name mismatch") } if len(p.dat) == 0 { return } ndigit := countDigit(len(p.dat)) nfmt := fmt.Sprintf(":dat_%%0%dd", ndigit) for i, d := range p.dat { d.name = fmt.Sprintf(nfmt, i) align := uint32(0) if d.regSizeAlign { align = regSize } v := link8.NewVar(align) v.Write(d.bs) lib.DeclareVar(d.name) lib.DefineVar(d.name, v) } }