Compiler-Semantic.st 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. Smalltalk current createPackage: 'Compiler-Semantic' properties: #{}!
  2. Object subclass: #LexicalScope
  3. instanceVariableNames: 'node instruction temps args outerScope'
  4. package: 'Compiler-Semantic'!
  5. !LexicalScope commentStamp!
  6. I represent a lexical scope where variable names are associated with ScopeVars
  7. Instances are used for block scopes. Method scopes are instances of MethodLexicalScope.
  8. I am attached to a ScopeVar and method/block nodes.
  9. Each context (method/closure) get a fresh scope that inherits from its outer scope.!
  10. !LexicalScope methodsFor: 'accessing'!
  11. alias
  12. ^ '$ctx', self scopeLevel asString
  13. !
  14. allVariableNames
  15. ^ self args keys, self temps keys
  16. !
  17. args
  18. ^ args ifNil: [ args := Dictionary new ]
  19. !
  20. bindingFor: aStringOrNode
  21. ^ self pseudoVars at: aStringOrNode value ifAbsent: [
  22. self args at: aStringOrNode value ifAbsent: [
  23. self temps at: aStringOrNode value ifAbsent: [ nil ]]]
  24. !
  25. instruction
  26. ^ instruction
  27. !
  28. instruction: anIRInstruction
  29. instruction := anIRInstruction
  30. !
  31. lookupVariable: aNode
  32. | lookup |
  33. lookup := (self bindingFor: aNode).
  34. lookup ifNil: [
  35. lookup := self outerScope ifNotNil: [
  36. (self outerScope lookupVariable: aNode) ]].
  37. ^ lookup
  38. !
  39. methodScope
  40. ^ self outerScope ifNotNil: [
  41. self outerScope methodScope ]
  42. !
  43. node
  44. "Answer the node in which I am defined"
  45. ^ node
  46. !
  47. node: aNode
  48. node := aNode
  49. !
  50. outerScope
  51. ^ outerScope
  52. !
  53. outerScope: aLexicalScope
  54. outerScope := aLexicalScope
  55. !
  56. pseudoVars
  57. ^ self methodScope pseudoVars
  58. !
  59. scopeLevel
  60. self outerScope ifNil: [ ^ 1 ].
  61. self isInlined ifTrue: [ ^ self outerScope scopeLevel ].
  62. ^ self outerScope scopeLevel + 1
  63. !
  64. temps
  65. ^ temps ifNil: [ temps := Dictionary new ]
  66. ! !
  67. !LexicalScope methodsFor: 'adding'!
  68. addArg: aString
  69. self args at: aString put: (ArgVar on: aString).
  70. (self args at: aString) scope: self
  71. !
  72. addTemp: aString
  73. self temps at: aString put: (TempVar on: aString).
  74. (self temps at: aString) scope: self
  75. ! !
  76. !LexicalScope methodsFor: 'testing'!
  77. canInlineNonLocalReturns
  78. ^ self isInlined and: [ self outerScope canInlineNonLocalReturns ]
  79. !
  80. isBlockScope
  81. ^ self isMethodScope not
  82. !
  83. isInlined
  84. ^ self instruction notNil and: [
  85. self instruction isInlined ]
  86. !
  87. isMethodScope
  88. ^ false
  89. ! !
  90. LexicalScope subclass: #MethodLexicalScope
  91. instanceVariableNames: 'iVars pseudoVars unknownVariables localReturn nonLocalReturns'
  92. package: 'Compiler-Semantic'!
  93. !MethodLexicalScope commentStamp!
  94. I represent a method scope.!
  95. !MethodLexicalScope methodsFor: 'accessing'!
  96. allVariableNames
  97. ^ super allVariableNames, self iVars keys
  98. !
  99. bindingFor: aNode
  100. ^ (super bindingFor: aNode) ifNil: [
  101. self iVars at: aNode value ifAbsent: [ nil ]]
  102. !
  103. iVars
  104. ^ iVars ifNil: [ iVars := Dictionary new ]
  105. !
  106. localReturn
  107. ^ localReturn ifNil: [ false ]
  108. !
  109. localReturn: aBoolean
  110. localReturn := aBoolean
  111. !
  112. methodScope
  113. ^ self
  114. !
  115. nonLocalReturns
  116. ^ nonLocalReturns ifNil: [ nonLocalReturns := OrderedCollection new ]
  117. !
  118. pseudoVars
  119. pseudoVars ifNil: [
  120. pseudoVars := Dictionary new.
  121. Smalltalk current pseudoVariableNames do: [ :each |
  122. pseudoVars at: each put: ((PseudoVar on: each)
  123. scope: self methodScope;
  124. yourself) ]].
  125. ^ pseudoVars
  126. !
  127. unknownVariables
  128. ^ unknownVariables ifNil: [ unknownVariables := OrderedCollection new ]
  129. ! !
  130. !MethodLexicalScope methodsFor: 'adding'!
  131. addIVar: aString
  132. self iVars at: aString put: (InstanceVar on: aString).
  133. (self iVars at: aString) scope: self
  134. !
  135. addNonLocalReturn: aScope
  136. self nonLocalReturns add: aScope
  137. !
  138. removeNonLocalReturn: aScope
  139. self nonLocalReturns remove: aScope ifAbsent: []
  140. ! !
  141. !MethodLexicalScope methodsFor: 'testing'!
  142. canInlineNonLocalReturns
  143. ^ true
  144. !
  145. hasLocalReturn
  146. ^ self localReturn
  147. !
  148. hasNonLocalReturn
  149. ^ self nonLocalReturns notEmpty
  150. !
  151. isMethodScope
  152. ^ true
  153. ! !
  154. Object subclass: #ScopeVar
  155. instanceVariableNames: 'scope name'
  156. package: 'Compiler-Semantic'!
  157. !ScopeVar commentStamp!
  158. I am an entry in a LexicalScope that gets associated with variable nodes of the same name.
  159. There are 4 different subclasses of vars: temp vars, local vars, args, and unknown/global vars.!
  160. !ScopeVar methodsFor: 'accessing'!
  161. alias
  162. ^ self name asVariableName
  163. !
  164. name
  165. ^ name
  166. !
  167. name: aString
  168. name := aString
  169. !
  170. scope
  171. ^ scope
  172. !
  173. scope: aScope
  174. scope := aScope
  175. ! !
  176. !ScopeVar methodsFor: 'testing'!
  177. isArgVar
  178. ^ false
  179. !
  180. isClassRefVar
  181. ^ false
  182. !
  183. isInstanceVar
  184. ^ false
  185. !
  186. isPseudoVar
  187. ^ false
  188. !
  189. isTempVar
  190. ^ false
  191. !
  192. isUnknownVar
  193. ^ false
  194. !
  195. validateAssignment
  196. (self isArgVar or: [ self isPseudoVar ]) ifTrue: [
  197. InvalidAssignmentError new
  198. variableName: self name;
  199. signal]
  200. ! !
  201. !ScopeVar class methodsFor: 'instance creation'!
  202. on: aString
  203. ^ self new
  204. name: aString;
  205. yourself
  206. ! !
  207. ScopeVar subclass: #AliasVar
  208. instanceVariableNames: 'node'
  209. package: 'Compiler-Semantic'!
  210. !AliasVar commentStamp!
  211. I am an internally defined variable by the compiler!
  212. !AliasVar methodsFor: 'accessing'!
  213. node
  214. ^ node
  215. !
  216. node: aNode
  217. node := aNode
  218. ! !
  219. ScopeVar subclass: #ArgVar
  220. instanceVariableNames: ''
  221. package: 'Compiler-Semantic'!
  222. !ArgVar commentStamp!
  223. I am an argument of a method or block.!
  224. !ArgVar methodsFor: 'testing'!
  225. isArgVar
  226. ^ true
  227. ! !
  228. ScopeVar subclass: #ClassRefVar
  229. instanceVariableNames: ''
  230. package: 'Compiler-Semantic'!
  231. !ClassRefVar commentStamp!
  232. I am an class reference variable!
  233. !ClassRefVar methodsFor: 'accessing'!
  234. alias
  235. ^ '(smalltalk.', self name, ' || ', self name, ')'
  236. ! !
  237. !ClassRefVar methodsFor: 'testing'!
  238. isClassRefVar
  239. ^ true
  240. ! !
  241. ScopeVar subclass: #InstanceVar
  242. instanceVariableNames: ''
  243. package: 'Compiler-Semantic'!
  244. !InstanceVar commentStamp!
  245. I am an instance variable of a method or block.!
  246. !InstanceVar methodsFor: 'testing'!
  247. alias
  248. ^ 'self["@', self name, '"]'
  249. !
  250. isInstanceVar
  251. ^ true
  252. ! !
  253. ScopeVar subclass: #PseudoVar
  254. instanceVariableNames: ''
  255. package: 'Compiler-Semantic'!
  256. !PseudoVar commentStamp!
  257. I am an pseudo variable.
  258. The five Smalltalk pseudo variables are: 'self', 'super', 'nil', 'true' and 'false'!
  259. !PseudoVar methodsFor: 'accessing'!
  260. alias
  261. ^ self name
  262. ! !
  263. !PseudoVar methodsFor: 'testing'!
  264. isPseudoVar
  265. ^ true
  266. ! !
  267. ScopeVar subclass: #TempVar
  268. instanceVariableNames: ''
  269. package: 'Compiler-Semantic'!
  270. !TempVar commentStamp!
  271. I am an temporary variable of a method or block.!
  272. !TempVar methodsFor: 'testing'!
  273. isTempVar
  274. ^ true
  275. ! !
  276. ScopeVar subclass: #UnknownVar
  277. instanceVariableNames: ''
  278. package: 'Compiler-Semantic'!
  279. !UnknownVar commentStamp!
  280. I am an unknown variable. Amber uses unknown variables as JavaScript globals!
  281. !UnknownVar methodsFor: 'testing'!
  282. isUnknownVar
  283. ^ true
  284. ! !
  285. NodeVisitor subclass: #SemanticAnalyzer
  286. instanceVariableNames: 'currentScope theClass classReferences messageSends superSends'
  287. package: 'Compiler-Semantic'!
  288. !SemanticAnalyzer commentStamp!
  289. I semantically analyze the abstract syntax tree and annotate it with informations such as non local returns and variable scopes.!
  290. !SemanticAnalyzer methodsFor: 'accessing'!
  291. classReferences
  292. ^ classReferences ifNil: [ classReferences := Set new ]
  293. !
  294. messageSends
  295. ^ messageSends ifNil: [ messageSends := Dictionary new ]
  296. !
  297. superSends
  298. ^ superSends ifNil: [ superSends := Dictionary new ]
  299. !
  300. theClass
  301. ^ theClass
  302. !
  303. theClass: aClass
  304. theClass := aClass
  305. ! !
  306. !SemanticAnalyzer methodsFor: 'error handling'!
  307. errorShadowingVariable: aString
  308. ShadowingVariableError new
  309. variableName: aString;
  310. signal
  311. !
  312. errorUnknownVariable: aNode
  313. "Throw an error if the variable is undeclared in the global JS scope (i.e. window).
  314. We allow four variable names in addition: `jQuery`, `window`, `process` and `global`
  315. for nodejs and browser environments.
  316. This is only to make sure compilation works on both browser-based and nodejs environments.
  317. The ideal solution would be to use a pragma instead"
  318. | identifier |
  319. identifier := aNode value.
  320. ((#('jQuery' 'window' 'process' 'global') includes: identifier) not
  321. and: [ self isVariableGloballyUndefined: identifier ])
  322. ifTrue: [
  323. UnknownVariableError new
  324. variableName: aNode value;
  325. signal ]
  326. ifFalse: [
  327. currentScope methodScope unknownVariables add: aNode value ]
  328. ! !
  329. !SemanticAnalyzer methodsFor: 'factory'!
  330. newBlockScope
  331. ^ self newScopeOfClass: LexicalScope
  332. !
  333. newMethodScope
  334. ^ self newScopeOfClass: MethodLexicalScope
  335. !
  336. newScopeOfClass: aLexicalScopeClass
  337. ^ aLexicalScopeClass new
  338. outerScope: currentScope;
  339. yourself
  340. ! !
  341. !SemanticAnalyzer methodsFor: 'scope'!
  342. popScope
  343. currentScope ifNotNil: [
  344. currentScope := currentScope outerScope ]
  345. !
  346. pushScope: aScope
  347. aScope outerScope: currentScope.
  348. currentScope := aScope
  349. !
  350. validateVariableScope: aString
  351. "Validate the variable scope in by doing a recursive lookup, up to the method scope"
  352. (currentScope lookupVariable: aString) ifNotNil: [
  353. self errorShadowingVariable: aString ]
  354. ! !
  355. !SemanticAnalyzer methodsFor: 'testing'!
  356. isVariableGloballyUndefined: aString
  357. <return eval('typeof ' + aString + ' == "undefined"')>
  358. ! !
  359. !SemanticAnalyzer methodsFor: 'visiting'!
  360. visitAssignmentNode: aNode
  361. super visitAssignmentNode: aNode.
  362. aNode left beAssigned
  363. !
  364. visitBlockNode: aNode
  365. self pushScope: self newBlockScope.
  366. aNode scope: currentScope.
  367. currentScope node: aNode.
  368. aNode parameters do: [ :each |
  369. self validateVariableScope: each.
  370. currentScope addArg: each ].
  371. super visitBlockNode: aNode.
  372. self popScope
  373. !
  374. visitCascadeNode: aNode
  375. "Populate the receiver into all children"
  376. aNode nodes do: [ :each |
  377. each receiver: aNode receiver ].
  378. super visitCascadeNode: aNode.
  379. aNode nodes first superSend ifTrue: [
  380. aNode nodes do: [ :each | each superSend: true ]]
  381. !
  382. visitClassReferenceNode: aNode
  383. self classReferences add: aNode value.
  384. aNode binding: (ClassRefVar new name: aNode value; yourself)
  385. !
  386. visitMethodNode: aNode
  387. self pushScope: self newMethodScope.
  388. aNode scope: currentScope.
  389. currentScope node: aNode.
  390. self theClass allInstanceVariableNames do: [:each |
  391. currentScope addIVar: each ].
  392. aNode arguments do: [ :each |
  393. self validateVariableScope: each.
  394. currentScope addArg: each ].
  395. super visitMethodNode: aNode.
  396. aNode
  397. classReferences: self classReferences;
  398. messageSends: self messageSends keys;
  399. superSends: self superSends keys.
  400. self popScope
  401. !
  402. visitReturnNode: aNode
  403. aNode scope: currentScope.
  404. currentScope isMethodScope
  405. ifTrue: [ currentScope localReturn: true ]
  406. ifFalse: [ currentScope methodScope addNonLocalReturn: currentScope ].
  407. super visitReturnNode: aNode
  408. !
  409. visitSendNode: aNode
  410. aNode receiver value = 'super'
  411. ifTrue: [
  412. aNode superSend: true.
  413. aNode receiver value: 'self'.
  414. self superSends at: aNode selector ifAbsentPut: [ Set new ].
  415. (self superSends at: aNode selector) add: aNode ]
  416. ifFalse: [ (IRSendInliner inlinedSelectors includes: aNode selector) ifTrue: [
  417. aNode shouldBeInlined: true.
  418. aNode receiver shouldBeAliased: true ] ].
  419. self messageSends at: aNode selector ifAbsentPut: [ Set new ].
  420. (self messageSends at: aNode selector) add: aNode.
  421. aNode index: (self messageSends at: aNode selector) size.
  422. super visitSendNode: aNode
  423. !
  424. visitSequenceNode: aNode
  425. aNode temps do: [ :each |
  426. self validateVariableScope: each.
  427. currentScope addTemp: each ].
  428. super visitSequenceNode: aNode
  429. !
  430. visitVariableNode: aNode
  431. "Bind a ScopeVar to aNode by doing a lookup in the current scope.
  432. If no ScopeVar is found, bind a UnknowVar and throw an error"
  433. aNode binding: ((currentScope lookupVariable: aNode) ifNil: [
  434. self errorUnknownVariable: aNode.
  435. UnknownVar new name: aNode value; yourself ])
  436. ! !
  437. !SemanticAnalyzer class methodsFor: 'instance creation'!
  438. on: aClass
  439. ^ self new
  440. theClass: aClass;
  441. yourself
  442. ! !