Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Sep 27, 2022
1 parent c6687b3 commit 1af6dc7
Show file tree
Hide file tree
Showing 25 changed files with 292 additions and 160 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
go-lang-lk
go-lang-lk
*/*.lkc
41 changes: 17 additions & 24 deletions binchunk/binary_chunk.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package binchunk

import (
"bytes"
"math"
"strconv"

"git.lolli.tech/lollipopkit/go-lang-lk/consts"
jsoniter "github.com/json-iterator/go"
)

var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
VERSION, _ = strconv.ParseFloat(string(consts.VERSION), 64)
)

const (
Expand All @@ -23,6 +18,13 @@ const (
TAG_LONG_STR = 0x14
)

type binaryChunk struct {
Version string `json:"v"`
Sign string `json:"si"`
Hash string `json:"h"`
Proto *Prototype `json:"p"`
}

// function prototype
type Prototype struct {
Source string `json:"s"` // debug
Expand Down Expand Up @@ -52,31 +54,22 @@ type LocVar struct {
}

func IsJsonChunk(data []byte) (bool, *Prototype) {
if len(data) < 9 {
var bin binaryChunk
err := json.Unmarshal(data, &bin)
if err != nil {
return false, nil
}
if !bytes.HasPrefix(data, []byte{'\x1b'}) {
if bin.Sign != consts.SIGNATURE || bin.Version != consts.VERSION {
return false, nil
}
if data[1] != byte(math.Float64bits(VERSION)) {
panic("version not match!")
}
data = data[9:]
var proto Prototype
err := json.Unmarshal(data, &proto)
return err == nil, &proto
return err == nil, bin.Proto
}

func (proto *Prototype) Dump() ([]byte, error) {
data, err := json.Marshal(proto)
if err != nil {
return nil, err
bin := &binaryChunk{
Version: consts.VERSION,
Sign: consts.SIGNATURE,
Proto: proto,
}

v := math.Float64bits(VERSION)
by := []byte{'\x1b'}
by = append(by, byte(v))
by = append(by, consts.SIGNATURE...)
data = append(by, data...)
return data, err
return json.Marshal(bin)
}
10 changes: 5 additions & 5 deletions compiler/codegen/cg_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package codegen
import . "git.lolli.tech/lollipopkit/go-lang-lk/compiler/ast"

func cgBlock(fi *funcInfo, node *Block) {
for _, stat := range node.Stats {
cgStat(fi, stat)
for k := range node.Stats {
cgStat(fi, node.Stats[k])
}

if node.RetExps != nil {
Expand Down Expand Up @@ -36,12 +36,12 @@ func cgRetStat(fi *funcInfo, exps []Exp, lastLine int) {
}

multRet := isVarargOrFuncCall(exps[nExps-1])
for i, exp := range exps {
for i := range exps {
r := fi.allocReg()
if i == nExps-1 && multRet {
cgExp(fi, exp, r, -1)
cgExp(fi, exps[i], r, -1)
} else {
cgExp(fi, exp, r, 1)
cgExp(fi, exps[i], r, 1)
}
}
fi.freeRegs(nExps)
Expand Down
26 changes: 13 additions & 13 deletions compiler/codegen/cg_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func cgFuncDefExp(fi *funcInfo, node *FuncDefExp, a int) {
subFI := newFuncInfo(fi, node)
fi.subFuncs = append(fi.subFuncs, subFI)

for _, param := range node.ParList {
subFI.addLocVar(param, 0)
for i := range node.ParList {
subFI.addLocVar(node.ParList[i], 0)
}

cgBlock(subFI, node.Block)
Expand All @@ -81,8 +81,8 @@ func cgFuncDefExp(fi *funcInfo, node *FuncDefExp, a int) {

func cgTableConstructorExp(fi *funcInfo, node *TableConstructorExp, a int) {
nArr := 0
for _, keyExp := range node.KeyExps {
if keyExp == nil {
for i := range node.KeyExps {
if node.KeyExps[i] == nil {
nArr++
}
}
Expand All @@ -93,10 +93,10 @@ func cgTableConstructorExp(fi *funcInfo, node *TableConstructorExp, a int) {
fi.emitNewTable(node.Line, a, nArr, nExps-nArr)

arrIdx := 0
for i, keyExp := range node.KeyExps {
for i := range node.KeyExps {
valExp := node.ValExps[i]

if keyExp == nil {
if node.KeyExps[i] == nil {
arrIdx++
tmp := fi.allocReg()
if i == nExps-1 && multRet {
Expand Down Expand Up @@ -124,7 +124,7 @@ func cgTableConstructorExp(fi *funcInfo, node *TableConstructorExp, a int) {
}

b := fi.allocReg()
cgExp(fi, keyExp, b, 1)
cgExp(fi, node.KeyExps[i], b, 1)
c := fi.allocReg()
cgExp(fi, valExp, c, 1)
fi.freeRegs(2)
Expand Down Expand Up @@ -172,9 +172,9 @@ func cgBinopExp(fi *funcInfo, node *BinopExp, a int) {

// r[a] := exp1 .. exp2
func cgConcatExp(fi *funcInfo, node *ConcatExp, a int) {
for _, subExp := range node.Exps {
for i := range node.Exps {
a := fi.allocReg()
cgExp(fi, subExp, a, 1)
cgExp(fi, node.Exps[i], a, 1)
}

c := fi.usedRegs - 1
Expand Down Expand Up @@ -238,13 +238,13 @@ func prepFuncCall(fi *funcInfo, node *FuncCallExp, a int) int {
fi.freeRegs(1)
}
}
for i, arg := range node.Args {
for i := range node.Args {
tmp := fi.allocReg()
if i == nArgs-1 && isVarargOrFuncCall(arg) {
if i == nArgs-1 && isVarargOrFuncCall(node.Args[i]) {
lastArgIsVarargOrFuncCall = true
cgExp(fi, arg, tmp, -1)
cgExp(fi, node.Args[i], tmp, -1)
} else {
cgExp(fi, arg, tmp, 1)
cgExp(fi, node.Args[i], tmp, 1)
}
}
fi.freeRegs(nArgs)
Expand Down
64 changes: 32 additions & 32 deletions compiler/codegen/cg_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ func cgIfStat(fi *funcInfo, node *IfStat) {
pcJmpToEnds := make([]int, len(node.Exps))
pcJmpToNextExp := -1

for i, exp := range node.Exps {
for i := range node.Exps {
if pcJmpToNextExp >= 0 {
fi.fixSbx(pcJmpToNextExp, fi.pc()-pcJmpToNextExp)
}

oldRegs := fi.usedRegs
a, _ := expToOpArg(fi, exp, ARG_REG)
a, _ := expToOpArg(fi, node.Exps[i], ARG_REG)
fi.usedRegs = oldRegs

line := lastLineOf(exp)
line := lastLineOf(node.Exps[i])
fi.emitTest(line, a, 0)
pcJmpToNextExp = fi.emitJmp(line, 0, 0)

Expand All @@ -110,8 +110,8 @@ func cgIfStat(fi *funcInfo, node *IfStat) {
}
}

for _, pc := range pcJmpToEnds {
fi.fixSbx(pc, fi.pc()-pc)
for i := range pcJmpToEnds {
fi.fixSbx(pcJmpToEnds[i], fi.pc()-pcJmpToEnds[i])
}
}

Expand Down Expand Up @@ -155,8 +155,8 @@ func cgForInStat(fi *funcInfo, node *ForInStat) {
NameList: []string{forGeneratorVar, forStateVar, forControlVar},
ExpList: node.ExpList,
})
for _, name := range node.NameList {
fi.addLocVar(name, fi.pc()+2)
for i := range node.NameList {
fi.addLocVar(node.NameList[i], fi.pc()+2)
}

pcJmpToTFC := fi.emitJmp(node.LineOfDo, 0, 0)
Expand All @@ -182,30 +182,30 @@ func cgLocalVarDeclStat(fi *funcInfo, node *LocalVarDeclStat) {

oldRegs := fi.usedRegs
if nExps == nNames {
for _, exp := range exps {
for i := range exps {
a := fi.allocReg()
cgExp(fi, exp, a, 1)
cgExp(fi, exps[i], a, 1)
}
} else if nExps > nNames {
for i, exp := range exps {
for i := range exps {
a := fi.allocReg()
if i == nExps-1 && isVarargOrFuncCall(exp) {
cgExp(fi, exp, a, 0)
if i == nExps-1 && isVarargOrFuncCall(exps[i]) {
cgExp(fi, exps[i], a, 0)
} else {
cgExp(fi, exp, a, 1)
cgExp(fi, exps[i], a, 1)
}
}
} else { // nNames > nExps
multRet := false
for i, exp := range exps {
for i := range exps {
a := fi.allocReg()
if i == nExps-1 && isVarargOrFuncCall(exp) {
if i == nExps-1 && isVarargOrFuncCall(exps[i]) {
multRet = true
n := nNames - nExps + 1
cgExp(fi, exp, a, n)
cgExp(fi, exps[i], a, n)
fi.allocRegs(n - 1)
} else {
cgExp(fi, exp, a, 1)
cgExp(fi, exps[i], a, 1)
}
}
if !multRet {
Expand All @@ -217,8 +217,8 @@ func cgLocalVarDeclStat(fi *funcInfo, node *LocalVarDeclStat) {

fi.usedRegs = oldRegs
startPC := fi.pc() + 1
for _, name := range node.NameList {
fi.addLocVar(name, startPC)
for i := range node.NameList {
fi.addLocVar(node.NameList[i], startPC)
}
}

Expand All @@ -232,14 +232,14 @@ func cgAssignStat(fi *funcInfo, node *AssignStat) {
vRegs := make([]int, nVars)
oldRegs := fi.usedRegs

for i, exp := range node.VarList {
if taExp, ok := exp.(*TableAccessExp); ok {
for i := range node.VarList {
if taExp, ok := node.VarList[i].(*TableAccessExp); ok {
tRegs[i] = fi.allocReg()
cgExp(fi, taExp.PrefixExp, tRegs[i], 1)
kRegs[i] = fi.allocReg()
cgExp(fi, taExp.KeyExp, kRegs[i], 1)
} else {
name := exp.(*NameExp).Name
name := node.VarList[i].(*NameExp).Name
if fi.slotOfLocVar(name) < 0 && fi.indexOfUpval(name) < 0 {
// global var
kRegs[i] = -1
Expand All @@ -254,25 +254,25 @@ func cgAssignStat(fi *funcInfo, node *AssignStat) {
}

if nExps >= nVars {
for i, exp := range exps {
for i := range exps {
a := fi.allocReg()
if i >= nVars && i == nExps-1 && isVarargOrFuncCall(exp) {
cgExp(fi, exp, a, 0)
if i >= nVars && i == nExps-1 && isVarargOrFuncCall(exps[i]) {
cgExp(fi, exps[i], a, 0)
} else {
cgExp(fi, exp, a, 1)
cgExp(fi, exps[i], a, 1)
}
}
} else { // nVars > nExps
multRet := false
for i, exp := range exps {
for i := range exps {
a := fi.allocReg()
if i == nExps-1 && isVarargOrFuncCall(exp) {
if i == nExps-1 && isVarargOrFuncCall(exps[i]) {
multRet = true
n := nVars - nExps + 1
cgExp(fi, exp, a, n)
cgExp(fi, exps[i], a, n)
fi.allocRegs(n - 1)
} else {
cgExp(fi, exp, a, 1)
cgExp(fi, exps[i], a, 1)
}
}
if !multRet {
Expand All @@ -283,8 +283,8 @@ func cgAssignStat(fi *funcInfo, node *AssignStat) {
}

lastLine := node.LastLine
for i, exp := range node.VarList {
if nameExp, ok := exp.(*NameExp); ok {
for i := range node.VarList {
if nameExp, ok := node.VarList[i].(*NameExp); ok {
varName := nameExp.Name
if a := fi.slotOfLocVar(varName); a >= 0 {
fi.emitMove(lastLine, a, vRegs[i])
Expand Down
Loading

0 comments on commit 1af6dc7

Please sign in to comment.