MCPcopy Index your code
hub / github.com/yuin/gopher-lua / patchCode

Function patchCode

compile.go:1771–1847  ·  view source on GitHub ↗

}}}

(context *funcContext)

Source from the content-addressed store, hash-verified

1769} // }}}
1770
1771func patchCode(context *funcContext) { // {{{
1772 maxreg := 1
1773 if np := int(context.Proto.NumParameters); np > 1 {
1774 maxreg = np
1775 }
1776 moven := 0
1777 code := context.Code.List()
1778 for pc := 0; pc < len(code); pc++ {
1779 inst := code[pc]
1780 curop := opGetOpCode(inst)
1781 switch curop {
1782 case OP_CLOSURE:
1783 pc += int(context.Proto.FunctionPrototypes[opGetArgBx(inst)].NumUpvalues)
1784 moven = 0
1785 continue
1786 case OP_SETGLOBAL, OP_SETUPVAL, OP_EQ, OP_LT, OP_LE, OP_TEST,
1787 OP_TAILCALL, OP_RETURN, OP_FORPREP, OP_FORLOOP, OP_TFORLOOP,
1788 OP_SETLIST, OP_CLOSE:
1789 /* nothing to do */
1790 case OP_CALL:
1791 if reg := opGetArgA(inst) + opGetArgC(inst) - 2; reg > maxreg {
1792 maxreg = reg
1793 }
1794 case OP_VARARG:
1795 if reg := opGetArgA(inst) + opGetArgB(inst) - 1; reg > maxreg {
1796 maxreg = reg
1797 }
1798 case OP_SELF:
1799 if reg := opGetArgA(inst) + 1; reg > maxreg {
1800 maxreg = reg
1801 }
1802 case OP_LOADNIL:
1803 if reg := opGetArgB(inst); reg > maxreg {
1804 maxreg = reg
1805 }
1806 case OP_JMP: // jump to jump optimization
1807 distance := 0
1808 count := 0 // avoiding infinite loops
1809 for jmp := inst; opGetOpCode(jmp) == OP_JMP && count < 5; jmp = context.Code.At(pc + distance + 1) {
1810 d := context.GetLabelPc(opGetArgSbx(jmp)) - pc
1811 if d > opMaxArgSbx {
1812 if distance == 0 {
1813 raiseCompileError(context, context.Proto.LineDefined, "too long to jump.")
1814 }
1815 break
1816 }
1817 distance = d
1818 count++
1819 }
1820 if distance == 0 {
1821 context.Code.SetOpCode(pc, OP_NOP)
1822 } else {
1823 context.Code.SetSbx(pc, distance)
1824 }
1825 default:
1826 if reg := opGetArgA(inst); reg > maxreg {
1827 maxreg = reg
1828 }

Callers 1

compileFunctionExprFunction · 0.85

Calls 14

opGetOpCodeFunction · 0.85
opGetArgBxFunction · 0.85
opGetArgAFunction · 0.85
opGetArgCFunction · 0.85
opGetArgBFunction · 0.85
opGetArgSbxFunction · 0.85
raiseCompileErrorFunction · 0.85
intMinFunction · 0.85
GetLabelPcMethod · 0.80
SetOpCodeMethod · 0.80
SetSbxMethod · 0.80
SetCMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…