1
0

Compiler-Semantic.st 11 KB

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