compiler.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package goja
  2. import (
  3. "fmt"
  4. "github.com/dop251/goja/ast"
  5. "github.com/dop251/goja/file"
  6. "sort"
  7. "strconv"
  8. )
  9. const (
  10. blockLoop = iota
  11. blockTry
  12. blockBranch
  13. blockSwitch
  14. blockWith
  15. )
  16. type CompilerError struct {
  17. Message string
  18. File *SrcFile
  19. Offset int
  20. }
  21. type CompilerSyntaxError struct {
  22. CompilerError
  23. }
  24. type CompilerReferenceError struct {
  25. CompilerError
  26. }
  27. type srcMapItem struct {
  28. pc int
  29. srcPos int
  30. }
  31. type Program struct {
  32. code []instruction
  33. values []Value
  34. funcName string
  35. src *SrcFile
  36. srcMap []srcMapItem
  37. }
  38. type compiler struct {
  39. p *Program
  40. scope *scope
  41. block *block
  42. blockStart int
  43. enumGetExpr compiledEnumGetExpr
  44. evalVM *vm
  45. }
  46. type scope struct {
  47. names map[string]uint32
  48. outer *scope
  49. strict bool
  50. eval bool
  51. lexical bool
  52. dynamic bool
  53. accessed bool
  54. argsNeeded bool
  55. thisNeeded bool
  56. namesMap map[string]string
  57. lastFreeTmp int
  58. }
  59. type block struct {
  60. typ int
  61. label string
  62. needResult bool
  63. cont int
  64. breaks []int
  65. conts []int
  66. outer *block
  67. }
  68. func (c *compiler) leaveBlock() {
  69. lbl := len(c.p.code)
  70. for _, item := range c.block.breaks {
  71. c.p.code[item] = jump(lbl - item)
  72. }
  73. if c.block.typ == blockLoop {
  74. for _, item := range c.block.conts {
  75. c.p.code[item] = jump(c.block.cont - item)
  76. }
  77. }
  78. c.block = c.block.outer
  79. }
  80. func (e *CompilerSyntaxError) Error() string {
  81. if e.File != nil {
  82. return fmt.Sprintf("SyntaxError: %s at %s", e.Message, e.File.Position(e.Offset))
  83. }
  84. return fmt.Sprintf("SyntaxError: %s", e.Message)
  85. }
  86. func (e *CompilerReferenceError) Error() string {
  87. return fmt.Sprintf("ReferenceError: %s", e.Message)
  88. }
  89. func (c *compiler) newScope() {
  90. strict := false
  91. if c.scope != nil {
  92. strict = c.scope.strict
  93. }
  94. c.scope = &scope{
  95. outer: c.scope,
  96. names: make(map[string]uint32),
  97. strict: strict,
  98. namesMap: make(map[string]string),
  99. }
  100. }
  101. func (c *compiler) popScope() {
  102. c.scope = c.scope.outer
  103. }
  104. func newCompiler() *compiler {
  105. c := &compiler{
  106. p: &Program{},
  107. }
  108. c.enumGetExpr.init(c, file.Idx(0))
  109. c.newScope()
  110. c.scope.dynamic = true
  111. return c
  112. }
  113. func (p *Program) defineLiteralValue(val Value) uint32 {
  114. for idx, v := range p.values {
  115. if v.SameAs(val) {
  116. return uint32(idx)
  117. }
  118. }
  119. idx := uint32(len(p.values))
  120. p.values = append(p.values, val)
  121. return idx
  122. }
  123. func (p *Program) dumpCode(logger func(format string, args ...interface{})) {
  124. p._dumpCode("", logger)
  125. }
  126. func (p *Program) _dumpCode(indent string, logger func(format string, args ...interface{})) {
  127. logger("values: %+v", p.values)
  128. for pc, ins := range p.code {
  129. logger("%s %d: %T(%v)", indent, pc, ins, ins)
  130. if f, ok := ins.(*newFunc); ok {
  131. f.prg._dumpCode(indent+">", logger)
  132. }
  133. }
  134. }
  135. func (p *Program) sourceOffset(pc int) int {
  136. i := sort.Search(len(p.srcMap), func(idx int) bool {
  137. return p.srcMap[idx].pc > pc
  138. }) - 1
  139. if i >= 0 {
  140. return p.srcMap[i].srcPos
  141. }
  142. return 0
  143. }
  144. func (s *scope) isFunction() bool {
  145. if !s.lexical {
  146. return s.outer != nil
  147. }
  148. return s.outer.isFunction()
  149. }
  150. func (s *scope) lookupName(name string) (idx uint32, found, noDynamics bool) {
  151. var level uint32 = 0
  152. noDynamics = true
  153. for curScope := s; curScope != nil; curScope = curScope.outer {
  154. if curScope != s {
  155. curScope.accessed = true
  156. }
  157. if curScope.dynamic {
  158. noDynamics = false
  159. } else {
  160. var mapped string
  161. if m, exists := curScope.namesMap[name]; exists {
  162. mapped = m
  163. } else {
  164. mapped = name
  165. }
  166. if i, exists := curScope.names[mapped]; exists {
  167. idx = i | (level << 24)
  168. found = true
  169. return
  170. }
  171. }
  172. if name == "arguments" && !s.lexical && s.isFunction() {
  173. s.argsNeeded = true
  174. s.accessed = true
  175. idx, _ = s.bindName(name)
  176. found = true
  177. return
  178. }
  179. level++
  180. }
  181. return
  182. }
  183. func (s *scope) bindName(name string) (uint32, bool) {
  184. if s.lexical {
  185. return s.outer.bindName(name)
  186. }
  187. if idx, exists := s.names[name]; exists {
  188. return idx, false
  189. }
  190. idx := uint32(len(s.names))
  191. s.names[name] = idx
  192. return idx, true
  193. }
  194. func (s *scope) bindNameShadow(name string) (uint32, bool) {
  195. if s.lexical {
  196. return s.outer.bindName(name)
  197. }
  198. unique := true
  199. if idx, exists := s.names[name]; exists {
  200. unique = false
  201. // shadow the var
  202. delete(s.names, name)
  203. n := strconv.Itoa(int(idx))
  204. s.names[n] = idx
  205. }
  206. idx := uint32(len(s.names))
  207. s.names[name] = idx
  208. return idx, unique
  209. }
  210. func (c *compiler) markBlockStart() {
  211. c.blockStart = len(c.p.code)
  212. }
  213. func (c *compiler) compile(in *ast.Program) {
  214. c.p.src = NewSrcFile(in.File.Name(), in.File.Source())
  215. if len(in.Body) > 0 {
  216. if !c.scope.strict {
  217. c.scope.strict = c.isStrict(in.Body)
  218. }
  219. }
  220. c.compileDeclList(in.DeclarationList, false)
  221. c.compileFunctions(in.DeclarationList)
  222. if len(in.Body) > 0 {
  223. for _, st := range in.Body[:len(in.Body)-1] {
  224. c.compileStatement(st, false)
  225. }
  226. c.compileStatement(in.Body[len(in.Body)-1], true)
  227. } else {
  228. c.compileStatement(&ast.EmptyStatement{}, true)
  229. }
  230. c.p.code = append(c.p.code, halt)
  231. code := c.p.code
  232. c.p.code = make([]instruction, 0, len(code)+len(c.scope.names)+2)
  233. if c.scope.eval {
  234. if !c.scope.strict {
  235. c.emit(jne(2), newStash)
  236. } else {
  237. c.emit(pop, newStash)
  238. }
  239. }
  240. l := len(c.p.code)
  241. c.p.code = c.p.code[:l+len(c.scope.names)]
  242. for name, nameIdx := range c.scope.names {
  243. c.p.code[l+int(nameIdx)] = bindName(name)
  244. }
  245. c.p.code = append(c.p.code, code...)
  246. for i, _ := range c.p.srcMap {
  247. c.p.srcMap[i].pc += len(c.scope.names)
  248. }
  249. }
  250. func (c *compiler) compileDeclList(v []ast.Declaration, inFunc bool) {
  251. for _, value := range v {
  252. switch value := value.(type) {
  253. case *ast.FunctionDeclaration:
  254. c.compileFunctionDecl(value)
  255. case *ast.VariableDeclaration:
  256. c.compileVarDecl(value, inFunc)
  257. default:
  258. panic(fmt.Errorf("Unsupported declaration: %T", value))
  259. }
  260. }
  261. }
  262. func (c *compiler) compileFunctions(v []ast.Declaration) {
  263. for _, value := range v {
  264. if value, ok := value.(*ast.FunctionDeclaration); ok {
  265. c.compileFunction(value)
  266. }
  267. }
  268. }
  269. func (c *compiler) compileVarDecl(v *ast.VariableDeclaration, inFunc bool) {
  270. for _, item := range v.List {
  271. if c.scope.strict {
  272. c.checkIdentifierLName(item.Name, int(item.Idx)-1)
  273. c.checkIdentifierName(item.Name, int(item.Idx)-1)
  274. }
  275. if !inFunc || item.Name != "arguments" {
  276. idx, ok := c.scope.bindName(item.Name)
  277. _ = idx
  278. //log.Printf("Define var: %s: %x", item.Name, idx)
  279. if !ok {
  280. // TODO: error
  281. }
  282. }
  283. }
  284. }
  285. func (c *compiler) addDecls() []instruction {
  286. code := make([]instruction, len(c.scope.names))
  287. for name, nameIdx := range c.scope.names {
  288. code[nameIdx] = bindName(name)
  289. }
  290. return code
  291. }
  292. func (c *compiler) convertInstrToStashless(instr uint32, args int) (newIdx int, convert bool) {
  293. level := instr >> 24
  294. idx := instr & 0x00FFFFFF
  295. if level > 0 {
  296. level--
  297. newIdx = int((level << 24) | idx)
  298. } else {
  299. iidx := int(idx)
  300. if iidx < args {
  301. newIdx = -iidx - 1
  302. } else {
  303. newIdx = iidx - args + 1
  304. }
  305. convert = true
  306. }
  307. return
  308. }
  309. func (c *compiler) convertFunctionToStashless(code []instruction, args int) {
  310. code[0] = enterFuncStashless{stackSize: uint32(len(c.scope.names) - args), args: uint32(args)}
  311. for pc := 1; pc < len(code); pc++ {
  312. instr := code[pc]
  313. if instr == ret {
  314. code[pc] = retStashless
  315. }
  316. switch instr := instr.(type) {
  317. case getLocal:
  318. if newIdx, convert := c.convertInstrToStashless(uint32(instr), args); convert {
  319. code[pc] = loadStack(newIdx)
  320. } else {
  321. code[pc] = getLocal(newIdx)
  322. }
  323. case setLocal:
  324. if newIdx, convert := c.convertInstrToStashless(uint32(instr), args); convert {
  325. code[pc] = storeStack(newIdx)
  326. } else {
  327. code[pc] = setLocal(newIdx)
  328. }
  329. case setLocalP:
  330. if newIdx, convert := c.convertInstrToStashless(uint32(instr), args); convert {
  331. code[pc] = storeStackP(newIdx)
  332. } else {
  333. code[pc] = setLocalP(newIdx)
  334. }
  335. case getVar:
  336. level := instr.idx >> 24
  337. idx := instr.idx & 0x00FFFFFF
  338. level--
  339. instr.idx = level<<24 | idx
  340. code[pc] = instr
  341. case setVar:
  342. level := instr.idx >> 24
  343. idx := instr.idx & 0x00FFFFFF
  344. level--
  345. instr.idx = level<<24 | idx
  346. code[pc] = instr
  347. }
  348. }
  349. }
  350. func (c *compiler) compileFunctionDecl(v *ast.FunctionDeclaration) {
  351. idx, ok := c.scope.bindName(v.Function.Name.Name)
  352. if !ok {
  353. // TODO: error
  354. }
  355. _ = idx
  356. // log.Printf("Define function: %s: %x", v.Function.Name.Name, idx)
  357. }
  358. func (c *compiler) compileFunction(v *ast.FunctionDeclaration) {
  359. e := &compiledIdentifierExpr{
  360. name: v.Function.Name.Name,
  361. }
  362. e.init(c, v.Function.Idx0())
  363. e.emitSetter(c.compileFunctionLiteral(v.Function, false))
  364. c.emit(pop)
  365. }
  366. func (c *compiler) emit(instructions ...instruction) {
  367. c.p.code = append(c.p.code, instructions...)
  368. }
  369. func (c *compiler) throwSyntaxError(offset int, format string, args ...interface{}) {
  370. panic(&CompilerSyntaxError{
  371. CompilerError: CompilerError{
  372. File: c.p.src,
  373. Offset: offset,
  374. Message: fmt.Sprintf(format, args...),
  375. },
  376. })
  377. }
  378. func (c *compiler) isStrict(list []ast.Statement) bool {
  379. for _, st := range list {
  380. if st, ok := st.(*ast.ExpressionStatement); ok {
  381. if e, ok := st.Expression.(*ast.StringLiteral); ok {
  382. if e.Literal == `"use strict"` || e.Literal == `'use strict'` {
  383. return true
  384. }
  385. } else {
  386. break
  387. }
  388. } else {
  389. break
  390. }
  391. }
  392. return false
  393. }
  394. func (c *compiler) isStrictStatement(s ast.Statement) bool {
  395. if s, ok := s.(*ast.BlockStatement); ok {
  396. return c.isStrict(s.List)
  397. }
  398. return false
  399. }
  400. func (c *compiler) checkIdentifierName(name string, offset int) {
  401. switch name {
  402. case "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield":
  403. c.throwSyntaxError(offset, "Unexpected strict mode reserved word")
  404. }
  405. }
  406. func (c *compiler) checkIdentifierLName(name string, offset int) {
  407. switch name {
  408. case "eval", "arguments":
  409. c.throwSyntaxError(offset, "Assignment to eval or arguments is not allowed in strict mode")
  410. }
  411. }