Compiler.st 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. Smalltalk current createPackage: 'Compiler' properties: #{}!
  2. Object subclass: #ChunkParser
  3. instanceVariableNames: 'stream'
  4. package: 'Compiler'!
  5. !ChunkParser methodsFor: 'accessing'!
  6. stream: aStream
  7. stream := aStream
  8. ! !
  9. !ChunkParser methodsFor: 'reading'!
  10. nextChunk
  11. "The chunk format (Smalltalk Interchange Format or Fileout format)
  12. is a trivial format but can be a bit tricky to understand:
  13. - Uses the exclamation mark as delimiter of chunks.
  14. - Inside a chunk a normal exclamation mark must be doubled.
  15. - A non empty chunk must be a valid Smalltalk expression.
  16. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  17. - The object created by the expression then takes over reading chunks.
  18. This metod returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  19. | char result chunk |
  20. result := '' writeStream.
  21. [char := stream next.
  22. char notNil] whileTrue: [
  23. char = '!!' ifTrue: [
  24. stream peek = '!!'
  25. ifTrue: [stream next "skipping the escape double"]
  26. ifFalse: [^result contents trimBoth "chunk end marker found"]].
  27. result nextPut: char].
  28. ^nil "a chunk needs to end with !!"
  29. ! !
  30. !ChunkParser class methodsFor: 'not yet classified'!
  31. on: aStream
  32. ^self new stream: aStream
  33. ! !
  34. Object subclass: #Compiler
  35. instanceVariableNames: 'currentClass source unknownVariables codeGeneratorClass'
  36. package: 'Compiler'!
  37. !Compiler methodsFor: 'accessing'!
  38. codeGeneratorClass
  39. ^codeGeneratorClass ifNil: [FunCodeGenerator]
  40. !
  41. codeGeneratorClass: aClass
  42. codeGeneratorClass := aClass
  43. !
  44. currentClass
  45. ^currentClass
  46. !
  47. currentClass: aClass
  48. currentClass := aClass
  49. !
  50. source
  51. ^source ifNil: ['']
  52. !
  53. source: aString
  54. source := aString
  55. !
  56. unknownVariables
  57. ^unknownVariables
  58. !
  59. unknownVariables: aCollection
  60. unknownVariables := aCollection
  61. ! !
  62. !Compiler methodsFor: 'compiling'!
  63. compile: aString
  64. ^self compileNode: (self parse: aString)
  65. !
  66. compile: aString forClass: aClass
  67. self currentClass: aClass.
  68. self source: aString.
  69. ^self compile: aString
  70. !
  71. compileExpression: aString
  72. self currentClass: DoIt.
  73. self source: 'doIt ^[', aString, '] value'.
  74. ^self compileNode: (self parse: self source)
  75. !
  76. compileNode: aNode
  77. | generator result |
  78. generator := self codeGeneratorClass new.
  79. generator
  80. source: self source;
  81. currentClass: self currentClass.
  82. result := generator compileNode: aNode.
  83. self unknownVariables: generator unknownVariables.
  84. ^result
  85. !
  86. eval: aString
  87. <return eval(aString)>
  88. !
  89. evaluateExpression: aString
  90. "Unlike #eval: evaluate a Smalltalk expression and answer the returned object"
  91. | result |
  92. DoIt addCompiledMethod: (self eval: (self compileExpression: aString)).
  93. result := DoIt new doIt.
  94. DoIt removeCompiledMethod: (DoIt methodDictionary at: 'doIt').
  95. ^result
  96. !
  97. install: aString forClass: aBehavior category: anotherString
  98. | compiled |
  99. compiled := self eval: (self compile: aString forClass: aBehavior).
  100. compiled category: anotherString.
  101. aBehavior addCompiledMethod: compiled.
  102. ^compiled
  103. !
  104. parse: aString
  105. ^Smalltalk current parse: aString
  106. !
  107. parseExpression: aString
  108. ^self parse: 'doIt ^[', aString, '] value'
  109. !
  110. recompile: aClass
  111. aClass methodDictionary do: [:each |
  112. self install: each source forClass: aClass category: each category].
  113. self setupClass: aClass.
  114. aClass isMetaclass ifFalse: [self recompile: aClass class]
  115. !
  116. recompileAll
  117. Smalltalk current classes do: [:each |
  118. Transcript show: each; cr.
  119. [self recompile: each] valueWithTimeout: 100]
  120. !
  121. setupClass: aClass
  122. <smalltalk.init(aClass)>
  123. ! !
  124. !Compiler class methodsFor: 'compiling'!
  125. recompile: aClass
  126. self new recompile: aClass
  127. !
  128. recompileAll
  129. Smalltalk current classes do: [:each |
  130. self recompile: each]
  131. ! !
  132. Object subclass: #DoIt
  133. instanceVariableNames: ''
  134. package: 'Compiler'!
  135. Object subclass: #Exporter
  136. instanceVariableNames: ''
  137. package: 'Compiler'!
  138. !Exporter methodsFor: 'fileOut'!
  139. exportAll
  140. "Export all packages in the system."
  141. ^String streamContents: [:stream |
  142. Smalltalk current packages do: [:pkg |
  143. stream nextPutAll: (self exportPackage: pkg name)]]
  144. !
  145. exportClass: aClass
  146. "Export a single class. Subclasses override these methods."
  147. ^String streamContents: [:stream |
  148. self exportDefinitionOf: aClass on: stream.
  149. self exportMethodsOf: aClass on: stream.
  150. self exportMetaDefinitionOf: aClass on: stream.
  151. self exportMethodsOf: aClass class on: stream]
  152. !
  153. exportPackage: packageName
  154. "Export a given package by name."
  155. | package |
  156. ^String streamContents: [:stream |
  157. package := Smalltalk current packageAt: packageName.
  158. self exportPackageDefinitionOf: package on: stream.
  159. "Export classes in dependency order.
  160. Update (issue #171): Remove duplicates for export"
  161. package sortedClasses asSet do: [:each |
  162. stream nextPutAll: (self exportClass: each)].
  163. self exportPackageExtensionsOf: package on: stream]
  164. ! !
  165. !Exporter methodsFor: 'private'!
  166. classNameFor: aClass
  167. ^aClass isMetaclass
  168. ifTrue: [aClass instanceClass name, '.klass']
  169. ifFalse: [
  170. aClass isNil
  171. ifTrue: ['nil']
  172. ifFalse: [aClass name]]
  173. !
  174. exportDefinitionOf: aClass on: aStream
  175. aStream
  176. nextPutAll: 'smalltalk.addClass(';
  177. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  178. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  179. nextPutAll: ', ['.
  180. aClass instanceVariableNames
  181. do: [:each | aStream nextPutAll: '''', each, '''']
  182. separatedBy: [aStream nextPutAll: ', '].
  183. aStream
  184. nextPutAll: '], ''';
  185. nextPutAll: aClass category, '''';
  186. nextPutAll: ');'.
  187. aClass comment notEmpty ifTrue: [
  188. aStream
  189. lf;
  190. nextPutAll: 'smalltalk.';
  191. nextPutAll: (self classNameFor: aClass);
  192. nextPutAll: '.comment=';
  193. nextPutAll: aClass comment asJavascript].
  194. aStream lf
  195. !
  196. exportMetaDefinitionOf: aClass on: aStream
  197. aClass class instanceVariableNames isEmpty ifFalse: [
  198. aStream
  199. nextPutAll: 'smalltalk.', (self classNameFor: aClass class);
  200. nextPutAll: '.iVarNames = ['.
  201. aClass class instanceVariableNames
  202. do: [:each | aStream nextPutAll: '''', each, '''']
  203. separatedBy: [aStream nextPutAll: ','].
  204. aStream nextPutAll: '];', String lf]
  205. !
  206. exportMethod: aMethod of: aClass on: aStream
  207. aStream
  208. nextPutAll: 'smalltalk.addMethod(';lf;
  209. nextPutAll: aMethod selector asSelector asJavascript, ',';lf;
  210. nextPutAll: 'smalltalk.method({';lf;
  211. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  212. nextPutAll: 'category: ''', aMethod category, ''',';lf;
  213. nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;
  214. nextPutAll: 'args: ', aMethod arguments asJavascript, ','; lf;
  215. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf;
  216. nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;
  217. nextPutAll: 'referencedClasses: ', aMethod referencedClasses asJavascript.
  218. aStream
  219. lf;
  220. nextPutAll: '}),';lf;
  221. nextPutAll: 'smalltalk.', (self classNameFor: aClass);
  222. nextPutAll: ');';lf;lf
  223. !
  224. exportMethodsOf: aClass on: aStream
  225. "Issue #143: sort methods alphabetically"
  226. ((aClass methodDictionary values) sorted: [:a :b | a selector <= b selector]) do: [:each |
  227. (each category match: '^\*') ifFalse: [
  228. self exportMethod: each of: aClass on: aStream]].
  229. aStream lf
  230. !
  231. exportPackageDefinitionOf: package on: aStream
  232. aStream
  233. nextPutAll: 'smalltalk.addPackage(';
  234. nextPutAll: '''', package name, ''', ', package propertiesAsJSON , ');'.
  235. aStream lf
  236. !
  237. exportPackageExtensionsOf: package on: aStream
  238. "Issue #143: sort classes and methods alphabetically"
  239. | name |
  240. name := package name.
  241. (Package sortedClasses: Smalltalk current classes) do: [:each |
  242. {each. each class} do: [:aClass |
  243. ((aClass methodDictionary values) sorted: [:a :b | a selector <= b selector]) do: [:method |
  244. (method category match: '^\*', name) ifTrue: [
  245. self exportMethod: method of: aClass on: aStream ]]]]
  246. ! !
  247. Exporter subclass: #ChunkExporter
  248. instanceVariableNames: ''
  249. package: 'Compiler'!
  250. !ChunkExporter methodsFor: 'not yet classified'!
  251. chunkEscape: aString
  252. "Replace all occurrences of !! with !!!! and trim at both ends."
  253. ^(aString replace: '!!' with: '!!!!') trimBoth
  254. !
  255. classNameFor: aClass
  256. ^aClass isMetaclass
  257. ifTrue: [aClass instanceClass name, ' class']
  258. ifFalse: [
  259. aClass isNil
  260. ifTrue: ['nil']
  261. ifFalse: [aClass name]]
  262. !
  263. exportDefinitionOf: aClass on: aStream
  264. "Chunk format."
  265. aStream
  266. nextPutAll: (self classNameFor: aClass superclass);
  267. nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;
  268. nextPutAll: ' instanceVariableNames: '''.
  269. aClass instanceVariableNames
  270. do: [:each | aStream nextPutAll: each]
  271. separatedBy: [aStream nextPutAll: ' '].
  272. aStream
  273. nextPutAll: ''''; lf;
  274. nextPutAll: ' package: ''', aClass category, '''!!'; lf.
  275. aClass comment notEmpty ifTrue: [
  276. aStream
  277. nextPutAll: '!!', (self classNameFor: aClass), ' commentStamp!!';lf;
  278. nextPutAll: (self chunkEscape: aClass comment), '!!';lf].
  279. aStream lf
  280. !
  281. exportMetaDefinitionOf: aClass on: aStream
  282. aClass class instanceVariableNames isEmpty ifFalse: [
  283. aStream
  284. nextPutAll: (self classNameFor: aClass class);
  285. nextPutAll: ' instanceVariableNames: '''.
  286. aClass class instanceVariableNames
  287. do: [:each | aStream nextPutAll: each]
  288. separatedBy: [aStream nextPutAll: ' '].
  289. aStream
  290. nextPutAll: '''!!'; lf; lf]
  291. !
  292. exportMethod: aMethod of: aClass on: aStream
  293. aStream
  294. lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;
  295. nextPutAll: '!!'
  296. !
  297. exportMethods: methods category: category of: aClass on: aStream
  298. "Issue #143: sort methods alphabetically"
  299. aStream
  300. nextPutAll: '!!', (self classNameFor: aClass);
  301. nextPutAll: ' methodsFor: ''', category, '''!!'.
  302. (methods sorted: [:a :b | a selector <= b selector]) do: [:each |
  303. self exportMethod: each of: aClass on: aStream].
  304. aStream nextPutAll: ' !!'; lf; lf
  305. !
  306. exportMethodsOf: aClass on: aStream
  307. "Issue #143: sort protocol alphabetically"
  308. | map |
  309. map := Dictionary new.
  310. aClass protocolsDo: [:category :methods |
  311. (category match: '^\*') ifFalse: [ map at: category put: methods ]].
  312. (map keys sorted: [:a :b | a <= b ]) do: [:category | | methods |
  313. methods := map at: category.
  314. self
  315. exportMethods: methods
  316. category: category
  317. of: aClass
  318. on: aStream ]
  319. !
  320. exportPackageDefinitionOf: package on: aStream
  321. "Chunk format."
  322. aStream
  323. nextPutAll: 'Smalltalk current createPackage: ''', package name,
  324. ''' properties: ', package properties storeString, '!!'; lf.
  325. !
  326. exportPackageExtensionsOf: package on: aStream
  327. "We need to override this one too since we need to group
  328. all methods in a given protocol under a leading methodsFor: chunk
  329. for that class."
  330. "Issue #143: sort protocol alphabetically"
  331. | name map |
  332. name := package name.
  333. (Package sortedClasses: Smalltalk current classes) do: [:each |
  334. {each. each class} do: [:aClass |
  335. map := Dictionary new.
  336. aClass protocolsDo: [:category :methods |
  337. (category match: '^\*', name) ifTrue: [ map at: category put: methods ]].
  338. (map keys sorted: [:a :b | a <= b ]) do: [:category | | methods |
  339. methods := map at: category.
  340. self exportMethods: methods category: category of: aClass on: aStream ]]]
  341. ! !
  342. Exporter subclass: #StrippedExporter
  343. instanceVariableNames: ''
  344. package: 'Compiler'!
  345. !StrippedExporter methodsFor: 'private'!
  346. exportDefinitionOf: aClass on: aStream
  347. aStream
  348. nextPutAll: 'smalltalk.addClass(';
  349. nextPutAll: '''', (self classNameFor: aClass), ''', ';
  350. nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);
  351. nextPutAll: ', ['.
  352. aClass instanceVariableNames
  353. do: [:each | aStream nextPutAll: '''', each, '''']
  354. separatedBy: [aStream nextPutAll: ', '].
  355. aStream
  356. nextPutAll: '], ''';
  357. nextPutAll: aClass category, '''';
  358. nextPutAll: ');'.
  359. aStream lf
  360. !
  361. exportMethod: aMethod of: aClass on: aStream
  362. aStream
  363. nextPutAll: 'smalltalk.addMethod(';lf;
  364. nextPutAll: aMethod selector asSelector asJavascript, ',';lf;
  365. nextPutAll: 'smalltalk.method({';lf;
  366. nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;
  367. nextPutAll: 'fn: ', aMethod fn compiledSource;lf;
  368. nextPutAll: '}),';lf;
  369. nextPutAll: 'smalltalk.', (self classNameFor: aClass);
  370. nextPutAll: ');';lf;lf
  371. ! !
  372. Object subclass: #Importer
  373. instanceVariableNames: ''
  374. package: 'Compiler'!
  375. !Importer methodsFor: 'fileIn'!
  376. import: aStream
  377. | chunk result parser lastEmpty |
  378. parser := ChunkParser on: aStream.
  379. lastEmpty := false.
  380. [chunk := parser nextChunk.
  381. chunk isNil] whileFalse: [
  382. chunk isEmpty
  383. ifTrue: [lastEmpty := true]
  384. ifFalse: [
  385. result := Compiler new evaluateExpression: chunk.
  386. lastEmpty
  387. ifTrue: [
  388. lastEmpty := false.
  389. result scanFrom: parser]]]
  390. ! !
  391. Object subclass: #Node
  392. instanceVariableNames: 'nodes'
  393. package: 'Compiler'!
  394. !Node methodsFor: 'accessing'!
  395. addNode: aNode
  396. self nodes add: aNode
  397. !
  398. nodes
  399. ^nodes ifNil: [nodes := Array new]
  400. ! !
  401. !Node methodsFor: 'building'!
  402. nodes: aCollection
  403. nodes := aCollection
  404. ! !
  405. !Node methodsFor: 'testing'!
  406. isBlockNode
  407. ^false
  408. !
  409. isBlockSequenceNode
  410. ^false
  411. !
  412. isValueNode
  413. ^false
  414. ! !
  415. !Node methodsFor: 'visiting'!
  416. accept: aVisitor
  417. aVisitor visitNode: self
  418. ! !
  419. Node subclass: #AssignmentNode
  420. instanceVariableNames: 'left right'
  421. package: 'Compiler'!
  422. !AssignmentNode methodsFor: 'accessing'!
  423. left
  424. ^left
  425. !
  426. left: aNode
  427. left := aNode.
  428. left assigned: true
  429. !
  430. right
  431. ^right
  432. !
  433. right: aNode
  434. right := aNode
  435. ! !
  436. !AssignmentNode methodsFor: 'visiting'!
  437. accept: aVisitor
  438. aVisitor visitAssignmentNode: self
  439. ! !
  440. Node subclass: #BlockNode
  441. instanceVariableNames: 'parameters inlined'
  442. package: 'Compiler'!
  443. !BlockNode methodsFor: 'accessing'!
  444. inlined
  445. ^inlined ifNil: [false]
  446. !
  447. inlined: aBoolean
  448. inlined := aBoolean
  449. !
  450. parameters
  451. ^parameters ifNil: [parameters := Array new]
  452. !
  453. parameters: aCollection
  454. parameters := aCollection
  455. ! !
  456. !BlockNode methodsFor: 'testing'!
  457. isBlockNode
  458. ^true
  459. ! !
  460. !BlockNode methodsFor: 'visiting'!
  461. accept: aVisitor
  462. aVisitor visitBlockNode: self
  463. ! !
  464. Node subclass: #CascadeNode
  465. instanceVariableNames: 'receiver'
  466. package: 'Compiler'!
  467. !CascadeNode methodsFor: 'accessing'!
  468. receiver
  469. ^receiver
  470. !
  471. receiver: aNode
  472. receiver := aNode
  473. ! !
  474. !CascadeNode methodsFor: 'visiting'!
  475. accept: aVisitor
  476. aVisitor visitCascadeNode: self
  477. ! !
  478. Node subclass: #DynamicArrayNode
  479. instanceVariableNames: ''
  480. package: 'Compiler'!
  481. !DynamicArrayNode methodsFor: 'visiting'!
  482. accept: aVisitor
  483. aVisitor visitDynamicArrayNode: self
  484. ! !
  485. Node subclass: #DynamicDictionaryNode
  486. instanceVariableNames: ''
  487. package: 'Compiler'!
  488. !DynamicDictionaryNode methodsFor: 'visiting'!
  489. accept: aVisitor
  490. aVisitor visitDynamicDictionaryNode: self
  491. ! !
  492. Node subclass: #JSStatementNode
  493. instanceVariableNames: 'source'
  494. package: 'Compiler'!
  495. !JSStatementNode methodsFor: 'accessing'!
  496. source
  497. ^source ifNil: ['']
  498. !
  499. source: aString
  500. source := aString
  501. ! !
  502. !JSStatementNode methodsFor: 'visiting'!
  503. accept: aVisitor
  504. aVisitor visitJSStatementNode: self
  505. ! !
  506. Node subclass: #MethodNode
  507. instanceVariableNames: 'selector arguments source'
  508. package: 'Compiler'!
  509. !MethodNode methodsFor: 'accessing'!
  510. arguments
  511. ^arguments ifNil: [#()]
  512. !
  513. arguments: aCollection
  514. arguments := aCollection
  515. !
  516. selector
  517. ^selector
  518. !
  519. selector: aString
  520. selector := aString
  521. !
  522. source
  523. ^source
  524. !
  525. source: aString
  526. source := aString
  527. ! !
  528. !MethodNode methodsFor: 'visiting'!
  529. accept: aVisitor
  530. aVisitor visitMethodNode: self
  531. ! !
  532. Node subclass: #ReturnNode
  533. instanceVariableNames: ''
  534. package: 'Compiler'!
  535. !ReturnNode methodsFor: 'visiting'!
  536. accept: aVisitor
  537. aVisitor visitReturnNode: self
  538. ! !
  539. Node subclass: #SendNode
  540. instanceVariableNames: 'selector arguments receiver'
  541. package: 'Compiler'!
  542. !SendNode methodsFor: 'accessing'!
  543. arguments
  544. ^arguments ifNil: [arguments := #()]
  545. !
  546. arguments: aCollection
  547. arguments := aCollection
  548. !
  549. cascadeNodeWithMessages: aCollection
  550. | first |
  551. first := SendNode new
  552. selector: self selector;
  553. arguments: self arguments;
  554. yourself.
  555. ^CascadeNode new
  556. receiver: self receiver;
  557. nodes: (Array with: first), aCollection;
  558. yourself
  559. !
  560. receiver
  561. ^receiver
  562. !
  563. receiver: aNode
  564. receiver := aNode
  565. !
  566. selector
  567. ^selector
  568. !
  569. selector: aString
  570. selector := aString
  571. !
  572. valueForReceiver: anObject
  573. ^SendNode new
  574. receiver: (self receiver
  575. ifNil: [anObject]
  576. ifNotNil: [self receiver valueForReceiver: anObject]);
  577. selector: self selector;
  578. arguments: self arguments;
  579. yourself
  580. ! !
  581. !SendNode methodsFor: 'visiting'!
  582. accept: aVisitor
  583. aVisitor visitSendNode: self
  584. ! !
  585. Node subclass: #SequenceNode
  586. instanceVariableNames: 'temps'
  587. package: 'Compiler'!
  588. !SequenceNode methodsFor: 'accessing'!
  589. temps
  590. ^temps ifNil: [#()]
  591. !
  592. temps: aCollection
  593. temps := aCollection
  594. ! !
  595. !SequenceNode methodsFor: 'testing'!
  596. asBlockSequenceNode
  597. ^BlockSequenceNode new
  598. nodes: self nodes;
  599. temps: self temps;
  600. yourself
  601. ! !
  602. !SequenceNode methodsFor: 'visiting'!
  603. accept: aVisitor
  604. aVisitor visitSequenceNode: self
  605. ! !
  606. SequenceNode subclass: #BlockSequenceNode
  607. instanceVariableNames: ''
  608. package: 'Compiler'!
  609. !BlockSequenceNode methodsFor: 'testing'!
  610. isBlockSequenceNode
  611. ^true
  612. ! !
  613. !BlockSequenceNode methodsFor: 'visiting'!
  614. accept: aVisitor
  615. aVisitor visitBlockSequenceNode: self
  616. ! !
  617. Node subclass: #ValueNode
  618. instanceVariableNames: 'value'
  619. package: 'Compiler'!
  620. !ValueNode methodsFor: 'accessing'!
  621. value
  622. ^value
  623. !
  624. value: anObject
  625. value := anObject
  626. ! !
  627. !ValueNode methodsFor: 'testing'!
  628. isValueNode
  629. ^true
  630. ! !
  631. !ValueNode methodsFor: 'visiting'!
  632. accept: aVisitor
  633. aVisitor visitValueNode: self
  634. ! !
  635. ValueNode subclass: #VariableNode
  636. instanceVariableNames: 'assigned'
  637. package: 'Compiler'!
  638. !VariableNode methodsFor: 'accessing'!
  639. assigned
  640. ^assigned ifNil: [false]
  641. !
  642. assigned: aBoolean
  643. assigned := aBoolean
  644. ! !
  645. !VariableNode methodsFor: 'visiting'!
  646. accept: aVisitor
  647. aVisitor visitVariableNode: self
  648. ! !
  649. VariableNode subclass: #ClassReferenceNode
  650. instanceVariableNames: ''
  651. package: 'Compiler'!
  652. !ClassReferenceNode methodsFor: 'visiting'!
  653. accept: aVisitor
  654. aVisitor visitClassReferenceNode: self
  655. ! !
  656. Node subclass: #VerbatimNode
  657. instanceVariableNames: 'value'
  658. package: 'Compiler'!
  659. !VerbatimNode methodsFor: 'accessing'!
  660. value
  661. ^value
  662. !
  663. value: anObject
  664. value := anObject
  665. ! !
  666. !VerbatimNode methodsFor: 'visiting'!
  667. accept: aVisitor
  668. aVisitor visitVerbatimNode: self
  669. ! !
  670. Object subclass: #NodeVisitor
  671. instanceVariableNames: ''
  672. package: 'Compiler'!
  673. !NodeVisitor methodsFor: 'visiting'!
  674. visit: aNode
  675. aNode accept: self
  676. !
  677. visitAssignmentNode: aNode
  678. self visitNode: aNode
  679. !
  680. visitBlockNode: aNode
  681. self visitNode: aNode
  682. !
  683. visitBlockSequenceNode: aNode
  684. self visitNode: aNode
  685. !
  686. visitCascadeNode: aNode
  687. self visitNode: aNode
  688. !
  689. visitClassReferenceNode: aNode
  690. self visitNode: aNode
  691. !
  692. visitDynamicArrayNode: aNode
  693. self visitNode: aNode
  694. !
  695. visitDynamicDictionaryNode: aNode
  696. self visitNode: aNode
  697. !
  698. visitJSStatementNode: aNode
  699. self visitNode: aNode
  700. !
  701. visitMethodNode: aNode
  702. self visitNode: aNode
  703. !
  704. visitNode: aNode
  705. !
  706. visitReturnNode: aNode
  707. self visitNode: aNode
  708. !
  709. visitSendNode: aNode
  710. self visitNode: aNode
  711. !
  712. visitSequenceNode: aNode
  713. self visitNode: aNode
  714. !
  715. visitValueNode: aNode
  716. self visitNode: aNode
  717. !
  718. visitVariableNode: aNode
  719. self visitNode: aNode
  720. !
  721. visitVerbatimNode: aNode
  722. self visitNode: aNode
  723. ! !
  724. NodeVisitor subclass: #AbstractCodeGenerator
  725. instanceVariableNames: 'currentClass source'
  726. package: 'Compiler'!
  727. !AbstractCodeGenerator methodsFor: 'accessing'!
  728. classNameFor: aClass
  729. ^aClass isMetaclass
  730. ifTrue: [aClass instanceClass name, '.klass']
  731. ifFalse: [
  732. aClass isNil
  733. ifTrue: ['nil']
  734. ifFalse: [aClass name]]
  735. !
  736. currentClass
  737. ^currentClass
  738. !
  739. currentClass: aClass
  740. currentClass := aClass
  741. !
  742. pseudoVariables
  743. ^#('self' 'super' 'true' 'false' 'nil' 'thisContext')
  744. !
  745. safeVariableNameFor: aString
  746. ^(Smalltalk current reservedWords includes: aString)
  747. ifTrue: [aString, '_']
  748. ifFalse: [aString]
  749. !
  750. source
  751. ^source ifNil: ['']
  752. !
  753. source: aString
  754. source := aString
  755. ! !
  756. !AbstractCodeGenerator methodsFor: 'compiling'!
  757. compileNode: aNode
  758. self subclassResponsibility
  759. ! !
  760. AbstractCodeGenerator subclass: #FunCodeGenerator
  761. instanceVariableNames: 'stream nestedBlocks earlyReturn currentSelector unknownVariables tempVariables messageSends referencedClasses classReferenced argVariables'
  762. package: 'Compiler'!
  763. !FunCodeGenerator methodsFor: 'accessing'!
  764. argVariables
  765. ^argVariables copy
  766. !
  767. knownVariables
  768. ^self pseudoVariables
  769. addAll: self tempVariables;
  770. addAll: self argVariables;
  771. yourself
  772. !
  773. tempVariables
  774. ^tempVariables copy
  775. !
  776. unknownVariables
  777. ^unknownVariables copy
  778. ! !
  779. !FunCodeGenerator methodsFor: 'compiling'!
  780. compileNode: aNode
  781. stream := '' writeStream.
  782. self visit: aNode.
  783. ^stream contents
  784. ! !
  785. !FunCodeGenerator methodsFor: 'initialization'!
  786. initialize
  787. super initialize.
  788. stream := '' writeStream.
  789. unknownVariables := #().
  790. tempVariables := #().
  791. argVariables := #().
  792. messageSends := #().
  793. classReferenced := #()
  794. ! !
  795. !FunCodeGenerator methodsFor: 'optimizations'!
  796. checkClass: aClassName for: receiver
  797. stream nextPutAll: '((($receiver = ', receiver, ').klass === smalltalk.', aClassName, ') ? '
  798. !
  799. inline: aSelector receiver: receiver argumentNodes: aCollection
  800. | inlined |
  801. inlined := false.
  802. "-- Booleans --"
  803. (aSelector = 'ifFalse:') ifTrue: [
  804. aCollection first isBlockNode ifTrue: [
  805. self checkClass: 'Boolean' for: receiver.
  806. stream nextPutAll: '(!! $receiver ? '.
  807. self visit: aCollection first.
  808. stream nextPutAll: '() : nil)'.
  809. inlined := true]].
  810. (aSelector = 'ifTrue:') ifTrue: [
  811. aCollection first isBlockNode ifTrue: [
  812. self checkClass: 'Boolean' for: receiver.
  813. stream nextPutAll: '($receiver ? '.
  814. self visit: aCollection first.
  815. stream nextPutAll: '() : nil)'.
  816. inlined := true]].
  817. (aSelector = 'ifTrue:ifFalse:') ifTrue: [
  818. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  819. self checkClass: 'Boolean' for: receiver.
  820. stream nextPutAll: '($receiver ? '.
  821. self visit: aCollection first.
  822. stream nextPutAll: '() : '.
  823. self visit: aCollection second.
  824. stream nextPutAll: '())'.
  825. inlined := true]].
  826. (aSelector = 'ifFalse:ifTrue:') ifTrue: [
  827. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  828. self checkClass: 'Boolean' for: receiver.
  829. stream nextPutAll: '(!! $receiver ? '.
  830. self visit: aCollection first.
  831. stream nextPutAll: '() : '.
  832. self visit: aCollection second.
  833. stream nextPutAll: '())'.
  834. inlined := true]].
  835. "-- Numbers --"
  836. (aSelector = '<') ifTrue: [
  837. self checkClass: 'Number' for: receiver.
  838. stream nextPutAll: '$receiver <'.
  839. self visit: aCollection first.
  840. inlined := true].
  841. (aSelector = '<=') ifTrue: [
  842. self checkClass: 'Number' for: receiver.
  843. stream nextPutAll: '$receiver <='.
  844. self visit: aCollection first.
  845. inlined := true].
  846. (aSelector = '>') ifTrue: [
  847. self checkClass: 'Number' for: receiver.
  848. stream nextPutAll: '$receiver >'.
  849. self visit: aCollection first.
  850. inlined := true].
  851. (aSelector = '>=') ifTrue: [
  852. self checkClass: 'Number' for: receiver.
  853. stream nextPutAll: '$receiver >='.
  854. self visit: aCollection first.
  855. inlined := true].
  856. (aSelector = '+') ifTrue: [
  857. self checkClass: 'Number' for: receiver.
  858. stream nextPutAll: '$receiver +'.
  859. self visit: aCollection first.
  860. inlined := true].
  861. (aSelector = '-') ifTrue: [
  862. self checkClass: 'Number' for: receiver.
  863. stream nextPutAll: '$receiver -'.
  864. self visit: aCollection first.
  865. inlined := true].
  866. (aSelector = '*') ifTrue: [
  867. self checkClass: 'Number' for: receiver.
  868. stream nextPutAll: '$receiver *'.
  869. self visit: aCollection first.
  870. inlined := true].
  871. (aSelector = '/') ifTrue: [
  872. self checkClass: 'Number' for: receiver.
  873. stream nextPutAll: '$receiver /'.
  874. self visit: aCollection first.
  875. inlined := true].
  876. ^inlined
  877. !
  878. inlineLiteral: aSelector receiverNode: anObject argumentNodes: aCollection
  879. | inlined |
  880. inlined := false.
  881. "-- BlockClosures --"
  882. (aSelector = 'whileTrue:') ifTrue: [
  883. (anObject isBlockNode and: [aCollection first isBlockNode]) ifTrue: [
  884. stream nextPutAll: '(function(){while('.
  885. self visit: anObject.
  886. stream nextPutAll: '()) {'.
  887. self visit: aCollection first.
  888. stream nextPutAll: '()}})()'.
  889. inlined := true]].
  890. (aSelector = 'whileFalse:') ifTrue: [
  891. (anObject isBlockNode and: [aCollection first isBlockNode]) ifTrue: [
  892. stream nextPutAll: '(function(){while(!!'.
  893. self visit: anObject.
  894. stream nextPutAll: '()) {'.
  895. self visit: aCollection first.
  896. stream nextPutAll: '()}})()'.
  897. inlined := true]].
  898. (aSelector = 'whileTrue') ifTrue: [
  899. anObject isBlockNode ifTrue: [
  900. stream nextPutAll: '(function(){while('.
  901. self visit: anObject.
  902. stream nextPutAll: '()) {}})()'.
  903. inlined := true]].
  904. (aSelector = 'whileFalse') ifTrue: [
  905. anObject isBlockNode ifTrue: [
  906. stream nextPutAll: '(function(){while(!!'.
  907. self visit: anObject.
  908. stream nextPutAll: '()) {}})()'.
  909. inlined := true]].
  910. "-- Numbers --"
  911. (aSelector = '+') ifTrue: [
  912. (self isNode: anObject ofClass: Number) ifTrue: [
  913. self visit: anObject.
  914. stream nextPutAll: ' + '.
  915. self visit: aCollection first.
  916. inlined := true]].
  917. (aSelector = '-') ifTrue: [
  918. (self isNode: anObject ofClass: Number) ifTrue: [
  919. self visit: anObject.
  920. stream nextPutAll: ' - '.
  921. self visit: aCollection first.
  922. inlined := true]].
  923. (aSelector = '*') ifTrue: [
  924. (self isNode: anObject ofClass: Number) ifTrue: [
  925. self visit: anObject.
  926. stream nextPutAll: ' * '.
  927. self visit: aCollection first.
  928. inlined := true]].
  929. (aSelector = '/') ifTrue: [
  930. (self isNode: anObject ofClass: Number) ifTrue: [
  931. self visit: anObject.
  932. stream nextPutAll: ' / '.
  933. self visit: aCollection first.
  934. inlined := true]].
  935. (aSelector = '<') ifTrue: [
  936. (self isNode: anObject ofClass: Number) ifTrue: [
  937. self visit: anObject.
  938. stream nextPutAll: ' < '.
  939. self visit: aCollection first.
  940. inlined := true]].
  941. (aSelector = '<=') ifTrue: [
  942. (self isNode: anObject ofClass: Number) ifTrue: [
  943. self visit: anObject.
  944. stream nextPutAll: ' <= '.
  945. self visit: aCollection first.
  946. inlined := true]].
  947. (aSelector = '>') ifTrue: [
  948. (self isNode: anObject ofClass: Number) ifTrue: [
  949. self visit: anObject.
  950. stream nextPutAll: ' > '.
  951. self visit: aCollection first.
  952. inlined := true]].
  953. (aSelector = '>=') ifTrue: [
  954. (self isNode: anObject ofClass: Number) ifTrue: [
  955. self visit: anObject.
  956. stream nextPutAll: ' >= '.
  957. self visit: aCollection first.
  958. inlined := true]].
  959. "-- UndefinedObject --"
  960. (aSelector = 'ifNil:') ifTrue: [
  961. aCollection first isBlockNode ifTrue: [
  962. stream nextPutAll: '(($receiver = '.
  963. self visit: anObject.
  964. stream nextPutAll: ') == nil || $receiver == undefined) ? '.
  965. self visit: aCollection first.
  966. stream nextPutAll: '() : $receiver'.
  967. inlined := true]].
  968. (aSelector = 'ifNotNil:') ifTrue: [
  969. aCollection first isBlockNode ifTrue: [
  970. stream nextPutAll: '(($receiver = '.
  971. self visit: anObject.
  972. stream nextPutAll: ') !!= nil && $receiver !!= undefined) ? '.
  973. self visit: aCollection first.
  974. stream nextPutAll: '() : nil'.
  975. inlined := true]].
  976. (aSelector = 'ifNil:ifNotNil:') ifTrue: [
  977. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  978. stream nextPutAll: '(($receiver = '.
  979. self visit: anObject.
  980. stream nextPutAll: ') == nil || $receiver == undefined) ? '.
  981. self visit: aCollection first.
  982. stream nextPutAll: '() : '.
  983. self visit: aCollection second.
  984. stream nextPutAll: '()'.
  985. inlined := true]].
  986. (aSelector = 'ifNotNil:ifNil:') ifTrue: [
  987. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  988. stream nextPutAll: '(($receiver = '.
  989. self visit: anObject.
  990. stream nextPutAll: ') == nil || $receiver == undefined) ? '.
  991. self visit: aCollection second.
  992. stream nextPutAll: '() : '.
  993. self visit: aCollection first.
  994. stream nextPutAll: '()'.
  995. inlined := true]].
  996. ^inlined
  997. !
  998. isNode: aNode ofClass: aClass
  999. ^aNode isValueNode and: [
  1000. aNode value class = aClass or: [
  1001. aNode value = 'self' and: [self currentClass = aClass]]]
  1002. ! !
  1003. !FunCodeGenerator methodsFor: 'testing'!
  1004. performOptimizations
  1005. ^self class performOptimizations
  1006. ! !
  1007. !FunCodeGenerator methodsFor: 'visiting'!
  1008. send: aSelector to: aReceiver arguments: aCollection superSend: aBoolean
  1009. ^String streamContents: [:str || tmp |
  1010. tmp := stream.
  1011. str nextPutAll: 'smalltalk.send('.
  1012. str nextPutAll: aReceiver.
  1013. str nextPutAll: ', "', aSelector asSelector, '", ['.
  1014. stream := str.
  1015. aCollection
  1016. do: [:each | self visit: each]
  1017. separatedBy: [stream nextPutAll: ', '].
  1018. stream := tmp.
  1019. str nextPutAll: ']'.
  1020. aBoolean ifTrue: [
  1021. str nextPutAll: ', smalltalk.', (self classNameFor: self currentClass), '.superclass || nil'].
  1022. str nextPutAll: ')']
  1023. !
  1024. visit: aNode
  1025. aNode accept: self
  1026. !
  1027. visitAssignmentNode: aNode
  1028. stream nextPutAll: '('.
  1029. self visit: aNode left.
  1030. stream nextPutAll: '='.
  1031. self visit: aNode right.
  1032. stream nextPutAll: ')'
  1033. !
  1034. visitBlockNode: aNode
  1035. stream nextPutAll: '(function('.
  1036. aNode parameters
  1037. do: [:each |
  1038. tempVariables add: each.
  1039. stream nextPutAll: each]
  1040. separatedBy: [stream nextPutAll: ', '].
  1041. stream nextPutAll: '){'.
  1042. aNode nodes do: [:each | self visit: each].
  1043. stream nextPutAll: '})'
  1044. !
  1045. visitBlockSequenceNode: aNode
  1046. | index |
  1047. nestedBlocks := nestedBlocks + 1.
  1048. aNode nodes isEmpty
  1049. ifTrue: [
  1050. stream nextPutAll: 'return nil;']
  1051. ifFalse: [
  1052. aNode temps do: [:each | | temp |
  1053. temp := self safeVariableNameFor: each.
  1054. tempVariables add: temp.
  1055. stream nextPutAll: 'var ', temp, '=nil;'; lf].
  1056. index := 0.
  1057. aNode nodes do: [:each |
  1058. index := index + 1.
  1059. index = aNode nodes size ifTrue: [
  1060. stream nextPutAll: 'return '].
  1061. self visit: each.
  1062. stream nextPutAll: ';']].
  1063. nestedBlocks := nestedBlocks - 1
  1064. !
  1065. visitCascadeNode: aNode
  1066. | index |
  1067. index := 0.
  1068. (tempVariables includes: '$rec') ifFalse: [
  1069. tempVariables add: '$rec'].
  1070. stream nextPutAll: '(function($rec){'.
  1071. aNode nodes do: [:each |
  1072. index := index + 1.
  1073. index = aNode nodes size ifTrue: [
  1074. stream nextPutAll: 'return '].
  1075. each receiver: (VariableNode new value: '$rec').
  1076. self visit: each.
  1077. stream nextPutAll: ';'].
  1078. stream nextPutAll: '})('.
  1079. self visit: aNode receiver.
  1080. stream nextPutAll: ')'
  1081. !
  1082. visitClassReferenceNode: aNode
  1083. (referencedClasses includes: aNode value) ifFalse: [
  1084. referencedClasses add: aNode value].
  1085. stream nextPutAll: '(smalltalk.', aNode value, ' || ', aNode value, ')'
  1086. !
  1087. visitDynamicArrayNode: aNode
  1088. stream nextPutAll: '['.
  1089. aNode nodes
  1090. do: [:each | self visit: each]
  1091. separatedBy: [stream nextPutAll: ','].
  1092. stream nextPutAll: ']'
  1093. !
  1094. visitDynamicDictionaryNode: aNode
  1095. stream nextPutAll: 'smalltalk.HashedCollection._fromPairs_(['.
  1096. aNode nodes
  1097. do: [:each | self visit: each]
  1098. separatedBy: [stream nextPutAll: ','].
  1099. stream nextPutAll: '])'
  1100. !
  1101. visitFailure: aFailure
  1102. self error: aFailure asString
  1103. !
  1104. visitJSStatementNode: aNode
  1105. stream nextPutAll: aNode source
  1106. !
  1107. visitMethodNode: aNode
  1108. | str currentSelector |
  1109. currentSelector := aNode selector asSelector.
  1110. nestedBlocks := 0.
  1111. earlyReturn := false.
  1112. messageSends := #().
  1113. referencedClasses := #().
  1114. unknownVariables := #().
  1115. tempVariables := #().
  1116. argVariables := #().
  1117. stream
  1118. nextPutAll: 'smalltalk.method({'; lf;
  1119. nextPutAll: 'selector: "', aNode selector, '",'; lf.
  1120. stream nextPutAll: 'source: ', self source asJavascript, ',';lf.
  1121. stream nextPutAll: 'fn: function('.
  1122. aNode arguments
  1123. do: [:each |
  1124. argVariables add: each.
  1125. stream nextPutAll: each]
  1126. separatedBy: [stream nextPutAll: ', '].
  1127. stream
  1128. nextPutAll: '){'; lf;
  1129. nextPutAll: 'var self=this;'; lf.
  1130. str := stream.
  1131. stream := '' writeStream.
  1132. aNode nodes do: [:each |
  1133. self visit: each].
  1134. earlyReturn ifTrue: [
  1135. str nextPutAll: 'var $early={};'; lf; nextPutAll: 'try{'].
  1136. str nextPutAll: stream contents.
  1137. stream := str.
  1138. stream
  1139. lf;
  1140. nextPutAll: 'return self;'.
  1141. earlyReturn ifTrue: [
  1142. stream lf; nextPutAll: '} catch(e) {if(e===$early)return e[0]; throw e}'].
  1143. stream nextPutAll: '}'.
  1144. stream
  1145. nextPutAll: ',', String lf, 'messageSends: ';
  1146. nextPutAll: messageSends asJavascript, ','; lf;
  1147. nextPutAll: 'args: ', argVariables asJavascript, ','; lf;
  1148. nextPutAll: 'referencedClasses: ['.
  1149. referencedClasses
  1150. do: [:each | stream nextPutAll: each printString]
  1151. separatedBy: [stream nextPutAll: ','].
  1152. stream nextPutAll: ']'.
  1153. stream nextPutAll: '})'
  1154. !
  1155. visitReturnNode: aNode
  1156. nestedBlocks > 0 ifTrue: [
  1157. earlyReturn := true].
  1158. nestedBlocks > 0
  1159. ifTrue: [
  1160. stream
  1161. nextPutAll: '(function(){throw $early=[']
  1162. ifFalse: [stream nextPutAll: 'return '].
  1163. aNode nodes do: [:each |
  1164. self visit: each].
  1165. nestedBlocks > 0 ifTrue: [
  1166. stream nextPutAll: ']})()']
  1167. !
  1168. visitSendNode: aNode
  1169. | str receiver superSend inlined |
  1170. str := stream.
  1171. (messageSends includes: aNode selector) ifFalse: [
  1172. messageSends add: aNode selector].
  1173. stream := '' writeStream.
  1174. self visit: aNode receiver.
  1175. superSend := stream contents = 'super'.
  1176. receiver := superSend ifTrue: ['self'] ifFalse: [stream contents].
  1177. stream := str.
  1178. self performOptimizations
  1179. ifTrue: [
  1180. (self inlineLiteral: aNode selector receiverNode: aNode receiver argumentNodes: aNode arguments) ifFalse: [
  1181. (self inline: aNode selector receiver: receiver argumentNodes: aNode arguments)
  1182. ifTrue: [stream nextPutAll: ' : ', (self send: aNode selector to: '$receiver' arguments: aNode arguments superSend: superSend), ')']
  1183. ifFalse: [stream nextPutAll: (self send: aNode selector to: receiver arguments: aNode arguments superSend: superSend)]]]
  1184. ifFalse: [stream nextPutAll: (self send: aNode selector to: receiver arguments: aNode arguments superSend: superSend)]
  1185. !
  1186. visitSequenceNode: aNode
  1187. aNode temps do: [:each || temp |
  1188. temp := self safeVariableNameFor: each.
  1189. tempVariables add: temp.
  1190. stream nextPutAll: 'var ', temp, '=nil;'; lf].
  1191. aNode nodes do: [:each |
  1192. self visit: each.
  1193. stream nextPutAll: ';']
  1194. separatedBy: [stream lf]
  1195. !
  1196. visitValueNode: aNode
  1197. stream nextPutAll: aNode value asJavascript
  1198. !
  1199. visitVariableNode: aNode
  1200. | varName |
  1201. (self currentClass allInstanceVariableNames includes: aNode value)
  1202. ifTrue: [stream nextPutAll: 'self[''@', aNode value, ''']']
  1203. ifFalse: [
  1204. varName := self safeVariableNameFor: aNode value.
  1205. (self knownVariables includes: varName)
  1206. ifFalse: [
  1207. unknownVariables add: aNode value.
  1208. aNode assigned
  1209. ifTrue: [stream nextPutAll: varName]
  1210. ifFalse: [stream nextPutAll: '(typeof ', varName, ' == ''undefined'' ? nil : ', varName, ')']]
  1211. ifTrue: [
  1212. aNode value = 'thisContext'
  1213. ifTrue: [stream nextPutAll: '(smalltalk.getThisContext())']
  1214. ifFalse: [stream nextPutAll: varName]]]
  1215. ! !
  1216. FunCodeGenerator class instanceVariableNames: 'performOptimizations'!
  1217. !FunCodeGenerator class methodsFor: 'accessing'!
  1218. performOptimizations
  1219. ^performOptimizations ifNil: [true]
  1220. !
  1221. performOptimizations: aBoolean
  1222. performOptimizations := aBoolean
  1223. ! !
  1224. AbstractCodeGenerator subclass: #ImpCodeGenerator
  1225. instanceVariableNames: 'stream nestedBlocks earlyReturn currentSelector unknownVariables tempVariables messageSends referencedClasses classReferenced argVariables mutables assigned target lazyVars'
  1226. package: 'Compiler'!
  1227. !ImpCodeGenerator methodsFor: 'accessing'!
  1228. argVariables
  1229. ^argVariables copy
  1230. !
  1231. knownVariables
  1232. ^self pseudoVariables
  1233. addAll: self tempVariables;
  1234. addAll: self argVariables;
  1235. yourself
  1236. !
  1237. tempVariables
  1238. ^tempVariables copy
  1239. !
  1240. unknownVariables
  1241. ^unknownVariables copy
  1242. ! !
  1243. !ImpCodeGenerator methodsFor: 'compilation DSL'!
  1244. alias: aString
  1245. self alias: aString mutable: false
  1246. !
  1247. alias: aString mutable: aBoolean
  1248. (lazyVars includesKey: target)
  1249. ifTrue: [ lazyVars at: target put: aString. aBoolean ifTrue: [ mutables add: target ] ]
  1250. ifFalse: [ self assign: aString ]
  1251. !
  1252. aliasMutable: aString
  1253. self alias: aString mutable: true
  1254. !
  1255. assign: aString
  1256. | closer |
  1257. aString ifNotEmpty: [
  1258. self disarmAll.
  1259. closer := ''.
  1260. self ifValueWanted: [ stream nextPutAll:
  1261. (target = '^' ifTrue: ['return '] ifFalse: [
  1262. target = '!!' ifTrue: [ closer := ']'. 'throw $early=['] ifFalse: [
  1263. target, '=']]) ].
  1264. self makeAssigned.
  1265. stream nextPutAll: aString, closer, ';', self mylf ]
  1266. !
  1267. disarmAll
  1268. | list old |
  1269. list := mutables.
  1270. mutables := Set new.
  1271. old := self switchTarget: nil.
  1272. list do: [ :each | | value |
  1273. self switchTarget: each.
  1274. self assign: (lazyVars at: each)
  1275. ].
  1276. self switchTarget: old
  1277. !
  1278. ifValueWanted: aBlock
  1279. target ifNotNil: aBlock
  1280. !
  1281. isolate: aBlock
  1282. | old |
  1283. old := self switchTarget: self nextLazyvarName.
  1284. aBlock value.
  1285. ^self switchTarget: old
  1286. !
  1287. isolated: node
  1288. ^ self visit: node targetBeing: self nextLazyvarName
  1289. !
  1290. isolatedUse: node
  1291. | old |
  1292. old := self switchTarget: self nextLazyvarName.
  1293. self visit: node.
  1294. ^self useValueNamed: (self switchTarget: old)
  1295. !
  1296. makeAssigned
  1297. (lazyVars includesKey: target) ifTrue: [
  1298. lazyVars removeKey: target.
  1299. lazyVars at: 'assigned ',target put: nil. "<-- only to retain size, it is used in nextLazyvarName"
  1300. assigned add: target ].
  1301. !
  1302. nextLazyvarName
  1303. | name |
  1304. name := '$', lazyVars size asString.
  1305. lazyVars at: name put: name.
  1306. ^name
  1307. !
  1308. nilIfValueWanted
  1309. target ifNotNil: [ self alias: 'nil' ]
  1310. !
  1311. switchTarget: aString
  1312. | old |
  1313. old := target.
  1314. target := aString.
  1315. ^old
  1316. !
  1317. useValueNamed: key
  1318. | val |
  1319. (assigned includes: key) ifTrue: [ ^key ].
  1320. mutables remove: key.
  1321. ^lazyVars at: key
  1322. !
  1323. visit: aNode targetBeing: aString
  1324. | old |
  1325. old := self switchTarget: aString.
  1326. self visit: aNode.
  1327. ^ self switchTarget: old.
  1328. ! !
  1329. !ImpCodeGenerator methodsFor: 'compiling'!
  1330. compileNode: aNode
  1331. stream := '' writeStream.
  1332. self visit: aNode.
  1333. ^stream contents
  1334. ! !
  1335. !ImpCodeGenerator methodsFor: 'initialization'!
  1336. initialize
  1337. super initialize.
  1338. stream := '' writeStream.
  1339. unknownVariables := #().
  1340. tempVariables := #().
  1341. argVariables := #().
  1342. messageSends := #().
  1343. classReferenced := #().
  1344. mutables := Set new.
  1345. assigned := Set new.
  1346. lazyVars := HashedCollection new.
  1347. target := nil
  1348. ! !
  1349. !ImpCodeGenerator methodsFor: 'optimizations'!
  1350. checkClass: aClassName for: receiver
  1351. self prvCheckClass: aClassName for: receiver.
  1352. stream nextPutAll: '{'
  1353. !
  1354. checkClass: aClassName for: receiver includeIf: aBoolean
  1355. self prvCheckClass: aClassName for: receiver.
  1356. stream nextPutAll: (aBoolean ifTrue: ['if(('] ifFalse: ['if(!!(']), (self useValueNamed: receiver), ')) {'
  1357. !
  1358. inline: aSelector receiver: receiver argumentNodes: aCollection
  1359. "-- Booleans --"
  1360. (aSelector = 'ifFalse:') ifTrue: [
  1361. aCollection first isBlockNode ifTrue: [
  1362. self checkClass: 'Boolean' for: receiver includeIf: false.
  1363. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1364. self prvPutAndElse: [ self nilIfValueWanted ].
  1365. ^true]].
  1366. (aSelector = 'ifTrue:') ifTrue: [
  1367. aCollection first isBlockNode ifTrue: [
  1368. self checkClass: 'Boolean' for: receiver includeIf: true.
  1369. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1370. self prvPutAndElse: [ self nilIfValueWanted ].
  1371. ^true]].
  1372. (aSelector = 'ifTrue:ifFalse:') ifTrue: [
  1373. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  1374. self checkClass: 'Boolean' for: receiver includeIf: true.
  1375. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1376. self prvPutAndElse: [ self visit: aCollection second nodes first ].
  1377. ^true]].
  1378. (aSelector = 'ifFalse:ifTrue:') ifTrue: [
  1379. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  1380. self checkClass: 'Boolean' for: receiver includeIf: false.
  1381. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1382. self prvPutAndElse: [ self visit: aCollection second nodes first ].
  1383. ^true]].
  1384. "-- Numbers --"
  1385. (aSelector = '<') ifTrue: [ | operand |
  1386. operand := self isolatedUse: aCollection first.
  1387. self checkClass: 'Number' for: receiver.
  1388. self prvPutAndElse: [
  1389. self aliasMutable: '(', (self useValueNamed: receiver), '<', operand, ')' ].
  1390. ^{ VerbatimNode new value: operand }].
  1391. (aSelector = '<=') ifTrue: [ | operand |
  1392. operand := self isolatedUse: aCollection first.
  1393. self checkClass: 'Number' for: receiver.
  1394. self prvPutAndElse: [
  1395. self aliasMutable: '(', (self useValueNamed: receiver), '<=', operand, ')' ].
  1396. ^{ VerbatimNode new value: operand }].
  1397. (aSelector = '>') ifTrue: [ | operand |
  1398. operand := self isolatedUse: aCollection first.
  1399. self checkClass: 'Number' for: receiver.
  1400. self prvPutAndElse: [
  1401. self aliasMutable: '(', (self useValueNamed: receiver), '>', operand, ')' ].
  1402. ^{ VerbatimNode new value: operand }].
  1403. (aSelector = '>=') ifTrue: [ | operand |
  1404. operand := self isolatedUse: aCollection first.
  1405. self checkClass: 'Number' for: receiver.
  1406. self prvPutAndElse: [
  1407. self aliasMutable: '(', (self useValueNamed: receiver), '>=', operand, ')' ].
  1408. ^{ VerbatimNode new value: operand }].
  1409. (aSelector = '+') ifTrue: [ | operand |
  1410. operand := self isolatedUse: aCollection first.
  1411. self checkClass: 'Number' for: receiver.
  1412. self prvPutAndElse: [
  1413. self aliasMutable: '(', (self useValueNamed: receiver), '+', operand, ')' ].
  1414. ^{ VerbatimNode new value: operand }].
  1415. (aSelector = '-') ifTrue: [ | operand |
  1416. operand := self isolatedUse: aCollection first.
  1417. self checkClass: 'Number' for: receiver.
  1418. self prvPutAndElse: [
  1419. self aliasMutable: '(', (self useValueNamed: receiver), '-', operand, ')' ].
  1420. ^{ VerbatimNode new value: operand }].
  1421. (aSelector = '*') ifTrue: [ | operand |
  1422. operand := self isolatedUse: aCollection first.
  1423. self checkClass: 'Number' for: receiver.
  1424. self prvPutAndElse: [
  1425. self aliasMutable: '(', (self useValueNamed: receiver), '*', operand, ')' ].
  1426. ^{ VerbatimNode new value: operand }].
  1427. (aSelector = '/') ifTrue: [ | operand |
  1428. operand := self isolatedUse: aCollection first.
  1429. self checkClass: 'Number' for: receiver.
  1430. self prvPutAndElse: [
  1431. self aliasMutable: '(', (self useValueNamed: receiver), '/', operand, ')' ].
  1432. ^{ VerbatimNode new value: operand }].
  1433. ^nil
  1434. !
  1435. inlineLiteral: aSelector receiverNode: anObject argumentNodes: aCollection
  1436. | inlined |
  1437. inlined := false.
  1438. "-- BlockClosures --"
  1439. (aSelector = 'whileTrue:') ifTrue: [
  1440. (anObject isBlockNode and: [aCollection first isBlockNode]) ifTrue: [ | old |
  1441. self prvWhileConditionStatement: 'for(;;){' pre: 'if (!!(' condition: anObject post: ')) {'.
  1442. stream nextPutAll: 'break}', self mylf.
  1443. self prvPutAndClose: [ self visit: aCollection first nodes first targetBeing: nil ].
  1444. inlined := true]].
  1445. (aSelector = 'whileFalse:') ifTrue: [
  1446. (anObject isBlockNode and: [aCollection first isBlockNode]) ifTrue: [ | old |
  1447. self prvWhileConditionStatement: 'for(;;){' pre: 'if ((' condition: anObject post: ')) {'.
  1448. stream nextPutAll: 'break}', self mylf.
  1449. self prvPutAndClose: [ self visit: aCollection first nodes first targetBeing: nil ].
  1450. inlined := true]].
  1451. (aSelector = 'whileTrue') ifTrue: [
  1452. anObject isBlockNode ifTrue: [
  1453. self prvWhileConditionStatement: 'do{' pre: '}while((' condition: anObject post: '));', self mylf.
  1454. inlined := true]].
  1455. (aSelector = 'whileFalse') ifTrue: [
  1456. anObject isBlockNode ifTrue: [
  1457. self prvWhileConditionStatement: 'do{' pre: '}while(!!(' condition: anObject post: '));', self mylf.
  1458. inlined := true]].
  1459. "-- Numbers --"
  1460. (#('+' '-' '*' '/' '<' '<=' '>=' '>') includes: aSelector) ifTrue: [
  1461. (self prvInlineNumberOperator: aSelector on: anObject and: aCollection first) ifTrue: [
  1462. inlined := true]].
  1463. "-- UndefinedObject --"
  1464. (aSelector = 'ifNil:') ifTrue: [
  1465. aCollection first isBlockNode ifTrue: [ | rcv |
  1466. self disarmAll.
  1467. rcv := self isolatedUse: anObject.
  1468. rcv = 'super' ifTrue: [ rcv := 'self' ].
  1469. self makeAssigned.
  1470. stream nextPutAll: 'if((', rcv, ') === nil || (', rcv, ') == null) {'.
  1471. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1472. self prvPutAndClose: [ self alias: rcv ].
  1473. inlined := true]].
  1474. (aSelector = 'ifNotNil:') ifTrue: [
  1475. aCollection first isBlockNode ifTrue: [ | rcv |
  1476. self disarmAll.
  1477. rcv := self isolatedUse: anObject.
  1478. rcv = 'super' ifTrue: [ rcv := 'self' ].
  1479. self makeAssigned.
  1480. stream nextPutAll: 'if((', rcv, ') !!== nil && (', rcv, ') !!= null) {'.
  1481. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1482. self prvPutAndClose: [ self alias: rcv ].
  1483. inlined := true]].
  1484. (aSelector = 'ifNil:ifNotNil:') ifTrue: [
  1485. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [ | rcv |
  1486. self disarmAll.
  1487. rcv := self isolatedUse: anObject.
  1488. rcv = 'super' ifTrue: [ rcv := 'self' ].
  1489. self makeAssigned.
  1490. stream nextPutAll: 'if((', rcv, ') === nil || (', rcv, ') == null) {'.
  1491. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1492. self prvPutAndClose: [ self visit: aCollection second nodes first ].
  1493. inlined := true]].
  1494. (aSelector = 'ifNotNil:ifNil:') ifTrue: [
  1495. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [ | rcv |
  1496. self disarmAll.
  1497. rcv := self isolatedUse: anObject.
  1498. rcv = 'super' ifTrue: [ rcv := 'self' ].
  1499. self makeAssigned.
  1500. stream nextPutAll: 'if((', rcv, ') !!== nil && (', rcv, ') !!= null) {'.
  1501. self prvPutAndElse: [ self visit: aCollection first nodes first ].
  1502. self prvPutAndClose: [ self visit: aCollection second nodes first ].
  1503. inlined := true]].
  1504. (aSelector = 'isNil') ifTrue: [ | rcv |
  1505. rcv := self isolatedUse: anObject.
  1506. rcv = 'super' ifTrue: [ rcv := 'self' ].
  1507. self alias: '((', rcv, ') === nil || (', rcv, ') == null)'.
  1508. inlined := true].
  1509. (aSelector = 'notNil') ifTrue: [ | rcv |
  1510. rcv := self isolatedUse: anObject.
  1511. rcv = 'super' ifTrue: [ rcv := 'self' ].
  1512. self alias: '((', rcv, ') !!== nil && (', rcv, ') !!= null)'.
  1513. inlined := true].
  1514. ^inlined
  1515. !
  1516. isNode: aNode ofClass: aClass
  1517. ^aNode isValueNode and: [
  1518. aNode value class = aClass or: [
  1519. aNode value = 'self' and: [self currentClass = aClass]]]
  1520. !
  1521. prvCheckClass: aClassName for: receiver
  1522. self makeAssigned.
  1523. self disarmAll.
  1524. stream nextPutAll: 'if((', (self useValueNamed: receiver), ').klass === smalltalk.', aClassName, ') '
  1525. !
  1526. prvInlineNumberOperator: aSelector on: receiverNode and: operandNode
  1527. (aSelector = aSelector) ifTrue: [
  1528. (self isNode: receiverNode ofClass: Number) ifTrue: [
  1529. | rcv operand |
  1530. rcv := self isolated: receiverNode.
  1531. operand := self isolated: operandNode.
  1532. self alias: ((self useValueNamed: rcv), aSelector, (self useValueNamed: operand)).
  1533. ^true]].
  1534. ^false
  1535. !
  1536. prvWhileConditionStatement: stmtString pre: preString condition: anObject post: postString
  1537. | x |
  1538. stream nextPutAll: stmtString.
  1539. x := self isolatedUse: anObject nodes first.
  1540. x ifEmpty: [ x := '"should not reach - receiver includes ^"' ].
  1541. stream nextPutAll: preString, x, postString.
  1542. self nilIfValueWanted
  1543. ! !
  1544. !ImpCodeGenerator methodsFor: 'output'!
  1545. mylf
  1546. ^String lf, ((Array new: nestedBlocks+2) join: String tab)
  1547. !
  1548. prvPutAndClose: aBlock
  1549. aBlock value.
  1550. stream nextPutAll: '}', self mylf
  1551. !
  1552. prvPutAndElse: aBlock
  1553. aBlock value.
  1554. stream nextPutAll: '} else {'
  1555. !
  1556. putTemps: temps
  1557. temps ifNotEmpty: [
  1558. stream nextPutAll: 'var '.
  1559. temps do: [:each | | temp |
  1560. temp := self safeVariableNameFor: each.
  1561. tempVariables add: temp.
  1562. stream nextPutAll: temp, '=nil'] separatedBy: [ stream nextPutAll: ',' ].
  1563. stream nextPutAll: ';', self mylf
  1564. ]
  1565. ! !
  1566. !ImpCodeGenerator methodsFor: 'testing'!
  1567. assert: aBoolean
  1568. aBoolean ifFalse: [ self error: 'assertion failed' ]
  1569. !
  1570. performOptimizations
  1571. ^self class performOptimizations
  1572. ! !
  1573. !ImpCodeGenerator methodsFor: 'visiting'!
  1574. arrayOfValues: nodes
  1575. | args |
  1576. args :=nodes collect: [ :node | self isolated: node ].
  1577. self alias: (String streamContents: [ :str |
  1578. str nextPutAll: '['.
  1579. args
  1580. do: [:each | str nextPutAll: (self useValueNamed: each) ]
  1581. separatedBy: [str nextPutAll: ', '].
  1582. str nextPutAll: ']'
  1583. ])
  1584. !
  1585. send: aSelector to: aReceiver arguments: aCollection superSend: aBoolean
  1586. | args |
  1587. args := self isolate: [ self arrayOfValues: aCollection ].
  1588. self aliasMutable: (String streamContents: [ :str |
  1589. str nextPutAll: 'smalltalk.send('.
  1590. str nextPutAll: (self useValueNamed: aReceiver).
  1591. str nextPutAll: ', "', aSelector asSelector, '", '.
  1592. str nextPutAll: (self useValueNamed: args).
  1593. aBoolean ifTrue: [
  1594. str nextPutAll: ', smalltalk.', (self classNameFor: self currentClass superclass)].
  1595. str nextPutAll: ')'
  1596. ])
  1597. !
  1598. sequenceOfNodes: nodes temps: temps
  1599. nodes isEmpty
  1600. ifFalse: [ | old index |
  1601. self putTemps: temps.
  1602. old :=self switchTarget: nil.
  1603. index := 0.
  1604. nodes do: [:each |
  1605. index := index + 1.
  1606. index = nodes size ifTrue: [ self switchTarget: old ].
  1607. self visit: each ]]
  1608. ifTrue: [ self nilIfValueWanted ]
  1609. !
  1610. visit: aNode
  1611. aNode accept: self
  1612. !
  1613. visitAssignmentNode: aNode
  1614. | olds oldt |
  1615. olds := stream.
  1616. stream := '' writeStream.
  1617. oldt := self switchTarget: self nextLazyvarName.
  1618. self visit: aNode left.
  1619. self assert: (lazyVars at: target) ~= target.
  1620. self switchTarget: (self useValueNamed: (self switchTarget: nil)).
  1621. self assert: (lazyVars includesKey: target) not.
  1622. stream := olds.
  1623. self visit: aNode right.
  1624. olds := self switchTarget: oldt.
  1625. self ifValueWanted: [ self aliasMutable: olds ]
  1626. !
  1627. visitBlockNode: aNode
  1628. | oldt olds oldm |
  1629. self assert: aNode nodes size = 1.
  1630. oldt := self switchTarget: '^'.
  1631. olds := stream.
  1632. stream := '' writeStream.
  1633. stream nextPutAll: '(function('.
  1634. aNode parameters
  1635. do: [:each |
  1636. tempVariables add: each.
  1637. stream nextPutAll: each]
  1638. separatedBy: [stream nextPutAll: ', '].
  1639. stream nextPutAll: '){'.
  1640. nestedBlocks := nestedBlocks + 1.
  1641. oldm := mutables.
  1642. mutables := Set new.
  1643. self visit: aNode nodes first.
  1644. self assert: mutables isEmpty.
  1645. mutables := oldm.
  1646. nestedBlocks := nestedBlocks - 1.
  1647. stream nextPutAll: '})'.
  1648. self switchTarget: oldt.
  1649. oldt := stream contents.
  1650. stream := olds.
  1651. self aliasMutable: oldt
  1652. !
  1653. visitBlockSequenceNode: aNode
  1654. self sequenceOfNodes: aNode nodes temps: aNode temps
  1655. !
  1656. visitCascadeNode: aNode
  1657. | rcv |
  1658. rcv := self isolated: aNode receiver.
  1659. self disarmAll.
  1660. rcv := self useValueNamed: rcv.
  1661. aNode nodes do: [:each |
  1662. each receiver: (VerbatimNode new value: rcv) ].
  1663. self sequenceOfNodes: aNode nodes temps: #()
  1664. !
  1665. visitClassReferenceNode: aNode
  1666. (referencedClasses includes: aNode value) ifFalse: [
  1667. referencedClasses add: aNode value].
  1668. self aliasMutable: '(smalltalk.', aNode value, ' || ', aNode value, ')'
  1669. !
  1670. visitDynamicArrayNode: aNode
  1671. self arrayOfValues: aNode nodes
  1672. !
  1673. visitDynamicDictionaryNode: aNode
  1674. | elements |
  1675. elements := self isolate: [ self arrayOfValues: aNode nodes ].
  1676. self alias: 'smalltalk.HashedCollection._fromPairs_(', (self useValueNamed: elements), ')'
  1677. !
  1678. visitFailure: aFailure
  1679. self error: aFailure asString
  1680. !
  1681. visitJSStatementNode: aNode
  1682. self disarmAll.
  1683. stream nextPutAll: ';', (aNode source replace: '>>' with: '>'), ';', self mylf
  1684. !
  1685. visitMethodNode: aNode
  1686. | str currentSelector |
  1687. currentSelector := aNode selector asSelector.
  1688. nestedBlocks := 0.
  1689. earlyReturn := false.
  1690. messageSends := #().
  1691. referencedClasses := #().
  1692. unknownVariables := #().
  1693. tempVariables := #().
  1694. argVariables := #().
  1695. lazyVars := HashedCollection new.
  1696. mutables := Set new.
  1697. assigned := Set new.
  1698. stream
  1699. nextPutAll: 'smalltalk.method({'; lf;
  1700. nextPutAll: 'selector: "', aNode selector, '",'; lf.
  1701. stream nextPutAll: 'source: ', self source asJavascript, ',';lf.
  1702. stream nextPutAll: 'fn: function('.
  1703. aNode arguments
  1704. do: [:each |
  1705. argVariables add: each.
  1706. stream nextPutAll: each]
  1707. separatedBy: [stream nextPutAll: ', '].
  1708. stream
  1709. nextPutAll: '){var self=this;', self mylf.
  1710. str := stream.
  1711. stream := '' writeStream.
  1712. self switchTarget: nil.
  1713. self assert: aNode nodes size = 1.
  1714. self visit: aNode nodes first.
  1715. assigned ifNotEmpty: [ str nextPutAll: 'var ', (assigned asArray join: ','), ';', self mylf ].
  1716. earlyReturn ifTrue: [
  1717. str nextPutAll: 'var $early={}; try{', self mylf].
  1718. str nextPutAll: stream contents.
  1719. stream := str.
  1720. (aNode nodes first nodes notEmpty and: [ |checker|
  1721. checker := ReturnNodeChecker new.
  1722. checker visit: aNode nodes first nodes last.
  1723. checker wasReturnNode]) ifFalse: [ self switchTarget: '^'. self alias: 'self'. self switchTarget: nil ].
  1724. earlyReturn ifTrue: [
  1725. stream nextPutAll: '} catch(e) {if(e===$early) return e[0]; throw e}'].
  1726. stream nextPutAll: '}'.
  1727. stream
  1728. nextPutAll: ',', String lf, 'messageSends: ';
  1729. nextPutAll: messageSends asJavascript, ','; lf;
  1730. nextPutAll: 'args: ', argVariables asJavascript, ','; lf;
  1731. nextPutAll: 'referencedClasses: ['.
  1732. referencedClasses
  1733. do: [:each | stream nextPutAll: each printString]
  1734. separatedBy: [stream nextPutAll: ','].
  1735. stream nextPutAll: ']'.
  1736. stream nextPutAll: '})'.
  1737. self assert: mutables isEmpty
  1738. !
  1739. visitReturnNode: aNode
  1740. self assert: aNode nodes size = 1.
  1741. nestedBlocks > 0 ifTrue: [
  1742. earlyReturn := true].
  1743. self
  1744. visit: aNode nodes first
  1745. targetBeing: (nestedBlocks > 0 ifTrue: ['!!'] ifFalse: ['^']).
  1746. self alias: ''
  1747. !
  1748. visitSendNode: aNode
  1749. | receiver superSend rcv |
  1750. (messageSends includes: aNode selector) ifFalse: [
  1751. messageSends add: aNode selector].
  1752. self performOptimizations
  1753. ifTrue: [
  1754. (self inlineLiteral: aNode selector receiverNode: aNode receiver argumentNodes: aNode arguments) ifTrue: [ ^self ].
  1755. ].
  1756. rcv := self isolated: aNode receiver.
  1757. superSend := (lazyVars at: rcv ifAbsent: []) = 'super'.
  1758. superSend ifTrue: [ mutables remove: rcv. lazyVars at: rcv put: 'self' ].
  1759. self performOptimizations
  1760. ifTrue: [ | inline |
  1761. inline := self inline: aNode selector receiver: rcv argumentNodes: aNode arguments.
  1762. inline ifNotNil: [ | args |
  1763. args := inline = true ifTrue: [ aNode arguments ] ifFalse: [ inline ].
  1764. self prvPutAndClose: [ self send: aNode selector to: rcv arguments: args superSend: superSend ].
  1765. ^self ]].
  1766. self send: aNode selector to: rcv arguments: aNode arguments superSend: superSend
  1767. !
  1768. visitSequenceNode: aNode
  1769. aNode nodes isEmpty ifFalse: [
  1770. self sequenceOfNodes: aNode nodes temps: aNode temps ]
  1771. !
  1772. visitValueNode: aNode
  1773. self alias: aNode value asJavascript
  1774. !
  1775. visitVariableNode: aNode
  1776. | varName |
  1777. (self currentClass allInstanceVariableNames includes: aNode value)
  1778. ifTrue: [self aliasMutable: 'self[''@', aNode value, ''']']
  1779. ifFalse: [
  1780. varName := self safeVariableNameFor: aNode value.
  1781. (self knownVariables includes: varName)
  1782. ifFalse: [
  1783. unknownVariables add: aNode value.
  1784. aNode assigned
  1785. ifTrue: [self aliasMutable: varName]
  1786. ifFalse: [self aliasMutable: '(typeof ', varName, ' == ''undefined'' ? nil : ', varName, ')']]
  1787. ifTrue: [
  1788. aNode value = 'thisContext'
  1789. ifTrue: [self aliasMutable: '(smalltalk.getThisContext())']
  1790. ifFalse: [(self pseudoVariables includes: varName)
  1791. ifTrue: [ self alias: varName ]
  1792. ifFalse: [ self aliasMutable: varName]]]]
  1793. !
  1794. visitVerbatimNode: aNode
  1795. self alias: aNode value
  1796. ! !
  1797. ImpCodeGenerator class instanceVariableNames: 'performOptimizations'!
  1798. !ImpCodeGenerator class methodsFor: 'accessing'!
  1799. performOptimizations
  1800. ^performOptimizations ifNil: [true]
  1801. !
  1802. performOptimizations: aBoolean
  1803. performOptimizations := aBoolean
  1804. ! !
  1805. NodeVisitor subclass: #ReturnNodeChecker
  1806. instanceVariableNames: 'wasReturnNode'
  1807. package: 'Compiler'!
  1808. !ReturnNodeChecker methodsFor: 'accessing'!
  1809. wasReturnNode
  1810. ^wasReturnNode
  1811. ! !
  1812. !ReturnNodeChecker methodsFor: 'initializing'!
  1813. initialize
  1814. wasReturnNode := false
  1815. ! !
  1816. !ReturnNodeChecker methodsFor: 'visiting'!
  1817. visitReturnNode: aNode
  1818. wasReturnNode := true
  1819. ! !