tree.pas 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. This units exports some routines to manage the parse tree
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef tp}
  19. {$E+,N+}
  20. {$endif}
  21. unit tree;
  22. interface
  23. uses
  24. symconst,globtype,cobjects,symtable,aasm,cpuasm,cpubase;
  25. type
  26. pconstset = ^tconstset;
  27. tconstset = array[0..31] of byte;
  28. ttreetyp = (
  29. addn, {Represents the + operator.}
  30. muln, {Represents the * operator.}
  31. subn, {Represents the - operator.}
  32. divn, {Represents the div operator.}
  33. symdifn, {Represents the >< operator.}
  34. modn, {Represents the mod operator.}
  35. assignn, {Represents an assignment.}
  36. loadn, {Represents the use of a variabele.}
  37. rangen, {Represents a range (i.e. 0..9).}
  38. ltn, {Represents the < operator.}
  39. lten, {Represents the <= operator.}
  40. gtn, {Represents the > operator.}
  41. gten, {Represents the >= operator.}
  42. equaln, {Represents the = operator.}
  43. unequaln, {Represents the <> operator.}
  44. inn, {Represents the in operator.}
  45. orn, {Represents the or operator.}
  46. xorn, {Represents the xor operator.}
  47. shrn, {Represents the shr operator.}
  48. shln, {Represents the shl operator.}
  49. slashn, {Represents the / operator.}
  50. andn, {Represents the and operator.}
  51. subscriptn, {access to a record/class/object field}
  52. derefn, {Dereferences a pointer.}
  53. addrn, {Represents the @ operator.}
  54. doubleaddrn, {Represents the @@ operator.}
  55. ordconstn, {Represents an ordinal value.}
  56. typeconvn, {Represents type-conversion/typecast.}
  57. calln, {Represents a call node.}
  58. callparan, {Represents a parameter.}
  59. realconstn, {Represents a real value.}
  60. fixconstn, {Represents a fixed value.}
  61. umminusn, {Represents a sign change (i.e. -2).}
  62. asmn, {Represents an assembler node }
  63. vecn, {Represents array indexing.}
  64. stringconstn, {Represents a string constant.}
  65. funcretn, {Represents the function result var.}
  66. selfn, {Represents the self parameter.}
  67. notn, {Represents the not operator.}
  68. inlinen, {Internal procedures (i.e. writeln).}
  69. niln, {Represents the nil pointer.}
  70. errorn, {This part of the tree could not be
  71. parsed because of a compiler error.}
  72. typen, {A type name. Used for i.e. typeof(obj).}
  73. hnewn, {The new operation, constructor call.}
  74. hdisposen, {The dispose operation with destructor call.}
  75. newn, {The new operation, constructor call.}
  76. simpledisposen, {The dispose operation.}
  77. setelementn, {A set element(s) (i.e. [a,b] and also [a..b]).}
  78. setconstn, {A set constant (i.e. [1,2]).}
  79. blockn, {A block of statements.}
  80. statementn, {One statement in a block of nodes.}
  81. loopn, { used in genloopnode, must be converted }
  82. ifn, {An if statement.}
  83. breakn, {A break statement.}
  84. continuen, {A continue statement.}
  85. repeatn, {A repeat until block.}
  86. whilen, {A while do statement.}
  87. forn, {A for loop.}
  88. exitn, {An exit statement.}
  89. withn, {A with statement.}
  90. casen, {A case statement.}
  91. labeln, {A label.}
  92. goton, {A goto statement.}
  93. simplenewn, {The new operation.}
  94. tryexceptn, {A try except block.}
  95. raisen, {A raise statement.}
  96. switchesn, {??? Currently unused...}
  97. tryfinallyn, {A try finally statement.}
  98. onn, { for an on statement in exception code }
  99. isn, {Represents the is operator.}
  100. asn, {Represents the as typecast.}
  101. caretn, {Represents the ^ operator.}
  102. failn, {Represents the fail statement.}
  103. starstarn, {Represents the ** operator exponentiation }
  104. procinlinen, {Procedures that can be inlined }
  105. arrayconstructn, {Construction node for [...] parsing}
  106. arrayconstructrangen, {Range element to allow sets in array construction tree}
  107. { added for optimizations where we cannot suppress }
  108. nothingn,
  109. loadvmtn
  110. );
  111. tconverttype = (
  112. tc_equal,
  113. tc_not_possible,
  114. tc_string_2_string,
  115. tc_char_2_string,
  116. tc_pchar_2_string,
  117. tc_cchar_2_pchar,
  118. tc_cstring_2_pchar,
  119. tc_ansistring_2_pchar,
  120. tc_string_2_chararray,
  121. tc_chararray_2_string,
  122. tc_array_2_pointer,
  123. tc_pointer_2_array,
  124. tc_int_2_int,
  125. tc_bool_2_int,
  126. tc_int_2_bool,
  127. tc_real_2_real,
  128. tc_int_2_real,
  129. tc_int_2_fix,
  130. tc_real_2_fix,
  131. tc_fix_2_real,
  132. tc_proc_2_procvar,
  133. tc_arrayconstructor_2_set,
  134. tc_load_smallset,
  135. tc_bool_2_bool
  136. );
  137. { different assignment types }
  138. tassigntyp = (at_normal,at_plus,at_minus,at_star,at_slash);
  139. pcaserecord = ^tcaserecord;
  140. tcaserecord = record
  141. { range }
  142. _low,_high : longint;
  143. { only used by gentreejmp }
  144. _at : pasmlabel;
  145. { label of instruction }
  146. statement : pasmlabel;
  147. { is this the first of an case entry, needed to release statement
  148. label (PFV) }
  149. firstlabel : boolean;
  150. { left and right tree node }
  151. less,greater : pcaserecord;
  152. end;
  153. pnode = ^tnode;
  154. tnode = object
  155. treetype : ttreetyp;
  156. { the location of the result of this node }
  157. location : tlocation;
  158. { the number of registers needed to evalute the node }
  159. registersint,registersfpu : longint; { must be longint !!!! }
  160. {$ifdef SUPPORT_MMX}
  161. registersmmx,registerskni : longint;
  162. {$endif SUPPORT_MMX}
  163. resulttype : pdef;
  164. fileinfo : tfileposinfo;
  165. localswitches : tlocalswitches;
  166. {$ifdef extdebug}
  167. firstpasscount : longint;
  168. {$endif extdebug}
  169. error : boolean;
  170. constructor init;
  171. destructor done;virtual;
  172. { runs det_resulttype and det_temp }
  173. procedure pass_1;
  174. { dermines the resulttype of the node }
  175. procedure det_resulttype;virtual;
  176. { dermines the number of necessary temp. locations to evaluate
  177. the node }
  178. procedure det_temp;virtual;
  179. procedure secondpass;virtual;
  180. {$ifdef EXTDEBUG}
  181. { writes a node for debugging purpose, shouldn't be called }
  182. { direct, because there is no test for nil, use writenode }
  183. { to write a complete tree }
  184. procedure dowrite;virtual;
  185. {$endif EXTDEBUG}
  186. end;
  187. { allows to determine which elementes are to be replaced }
  188. tdisposetyp = (dt_nothing,dt_leftright,dt_left,
  189. dt_mbleft,dt_typeconv,dt_inlinen,
  190. dt_mbleft_and_method,dt_loop,dt_case,dt_with,dt_onn);
  191. ptree = ^ttree;
  192. ttree = record
  193. error : boolean;
  194. disposetyp : tdisposetyp;
  195. { is true, if the right and left operand are swaped }
  196. swaped : boolean;
  197. { the location of the result of this node }
  198. location : tlocation;
  199. { the number of registers needed to evalute the node }
  200. registers32,registersfpu : longint; { must be longint !!!! }
  201. {$ifdef SUPPORT_MMX}
  202. registersmmx : longint;
  203. {$endif SUPPORT_MMX}
  204. left,right : ptree;
  205. resulttype : pdef;
  206. fileinfo : tfileposinfo;
  207. localswitches : tlocalswitches;
  208. {$ifdef extdebug}
  209. firstpasscount : longint;
  210. {$endif extdebug}
  211. {$ifdef TEMPS_NOT_PUSH}
  212. temp_offset : longint;
  213. {$endif TEMPS_NOT_PUSH}
  214. case treetype : ttreetyp of
  215. addn : (use_strconcat : boolean;string_typ : tstringtype);
  216. callparan : (is_colon_para : boolean;exact_match_found,
  217. convlevel1found,convlevel2found:boolean;hightree:ptree);
  218. assignn : (assigntyp : tassigntyp;concat_string : boolean);
  219. loadn : (symtableentry : psym;symtable : psymtable;
  220. is_absolute,is_first : boolean);
  221. calln : (symtableprocentry : pprocsym;
  222. symtableproc : psymtable;procdefinition : pabstractprocdef;
  223. methodpointer : ptree;
  224. no_check,unit_specific,
  225. return_value_used,static_call : boolean);
  226. ordconstn : (value : longint);
  227. realconstn : (value_real : bestreal;lab_real : pasmlabel);
  228. fixconstn : (value_fix: longint);
  229. funcretn : (funcretprocinfo : pointer;retdef : pdef);
  230. subscriptn : (vs : pvarsym);
  231. vecn : (memindex,memseg:boolean;callunique : boolean);
  232. stringconstn : (value_str : pchar;length : longint; lab_str : pasmlabel;stringtype : tstringtype);
  233. typeconvn : (convtyp : tconverttype;explizit : boolean);
  234. typen : (typenodetype : pdef;typenodesym:ptypesym);
  235. inlinen : (inlinenumber : byte;inlineconst:boolean);
  236. procinlinen : (inlinetree:ptree;inlineprocsym:pprocsym;retoffset,para_offset,para_size : longint);
  237. setconstn : (value_set : pconstset;lab_set:pasmlabel);
  238. loopn : (t1,t2 : ptree;backward : boolean);
  239. asmn : (p_asm : paasmoutput;object_preserved : boolean);
  240. casen : (nodes : pcaserecord;elseblock : ptree);
  241. labeln,goton : (labelnr : pasmlabel);
  242. withn : (withsymtable : pwithsymtable;tablecount : longint;withreference:preference;islocal:boolean);
  243. onn : (exceptsymtable : psymtable;excepttype : pobjectdef);
  244. arrayconstructn : (cargs,cargswap: boolean);
  245. end;
  246. punarynode = ^tunarynode;
  247. tunarynode = object(tnode)
  248. left : pnode;
  249. {$ifdef extdebug}
  250. procedure dowrite;virtual;
  251. {$endif extdebug}
  252. constructor init(l : pnode);
  253. end;
  254. pbinarynode = ^tbinarynode;
  255. tbinarynode = object(tunarynode)
  256. right : pnode;
  257. constructor init(l,r : pnode);
  258. end;
  259. pbinopnode = ^tbinopnode;
  260. tbinopnode = object(tbinarynode)
  261. { is true, if the right and left operand are swaped }
  262. { against the original order }
  263. swaped : boolean;
  264. constructor init(l,r : pnode);
  265. end;
  266. {$ifdef dummy}
  267. case treetype : ttreetyp of
  268. addn : (use_strconcat : boolean;string_typ : tstringtype);
  269. callparan : (is_colon_para : boolean;exact_match_found : boolean);
  270. assignn : (assigntyp : tassigntyp;concat_string : boolean);
  271. calln : (symtableprocentry : psym;
  272. symtableproc : psymtable;procdefinition : pprocdef;
  273. methodpointer : ptree;
  274. no_check,unit_specific,return_value_used : boolean);
  275. ordconstn : (value : longint);
  276. realconstn : (value_real : bestreal;lab_real : plabel;realtyp : tait);
  277. fixconstn : (value_fix: longint);
  278. funcretn : (funcretprocinfo : pointer;retdef : pdef);
  279. subscriptn : (vs : pvarsym);
  280. vecn : (memindex,memseg:boolean;callunique : boolean);
  281. stringconstn : (value_str : pchar;length : longint; lab_str : plabel;stringtype : tstringtype);
  282. typeconvn : (convtyp : tconverttype;explizit : boolean);
  283. typen : (typenodetype : pdef);
  284. inlinen : (inlinenumber : byte;inlineconst:boolean);
  285. procinlinen : (inlineprocdef : pprocdef;
  286. retoffset,para_offset,para_size : longint);
  287. setconstn : (value_set : pconstset;lab_set:plabel);
  288. loopn : (t1,t2 : ptree;backward : boolean);
  289. casen : (nodes : pcaserecord;elseblock : ptree);
  290. labeln,goton : (labelnr : plabel);
  291. withn : (withsymtable : psymtable;tablecount : longint);
  292. onn : (exceptsymtable : psymtable;excepttype : pobjectdef);
  293. arrayconstructn : (cargs,cargswap: boolean);
  294. {$endif dummy}
  295. function gennode(t : ttreetyp;l,r : ptree) : ptree;
  296. function genlabelnode(t : ttreetyp;nr : pasmlabel) : ptree;
  297. function genloadnode(v : pvarsym;st : psymtable) : ptree;
  298. function genloadcallnode(v: pprocsym;st: psymtable): ptree;
  299. function gensinglenode(t : ttreetyp;l : ptree) : ptree;
  300. function gensubscriptnode(varsym : pvarsym;l : ptree) : ptree;
  301. function genordinalconstnode(v : longint;def : pdef) : ptree;
  302. function genfixconstnode(v : longint;def : pdef) : ptree;
  303. function gentypeconvnode(node : ptree;t : pdef) : ptree;
  304. function gentypenode(t : pdef;sym:ptypesym) : ptree;
  305. function gencallparanode(expr,next : ptree) : ptree;
  306. function genrealconstnode(v : bestreal;def : pdef) : ptree;
  307. function gencallnode(v : pprocsym;st : psymtable) : ptree;
  308. function genmethodcallnode(v : pprocsym;st : psymtable;mp : ptree) : ptree;
  309. function genloadmethodcallnode(v: pprocsym;st: psymtable; mp:ptree): ptree;
  310. { allow pchar or string for defining a pchar node }
  311. function genstringconstnode(const s : string) : ptree;
  312. { length is required for ansistrings }
  313. function genpcharconstnode(s : pchar;length : longint) : ptree;
  314. { helper routine for conststring node }
  315. function getpcharcopy(p : ptree) : pchar;
  316. function genzeronode(t : ttreetyp) : ptree;
  317. function geninlinenode(number : byte;is_const:boolean;l : ptree) : ptree;
  318. function genprocinlinenode(callp,code : ptree) : ptree;
  319. function gentypedconstloadnode(sym : ptypedconstsym;st : psymtable) : ptree;
  320. function genenumnode(v : penumsym) : ptree;
  321. function genselfnode(_class : pdef) : ptree;
  322. function gensetconstnode(s : pconstset;settype : psetdef) : ptree;
  323. function genloopnode(t : ttreetyp;l,r,n1: ptree;back : boolean) : ptree;
  324. function genasmnode(p_asm : paasmoutput) : ptree;
  325. function gencasenode(l,r : ptree;nodes : pcaserecord) : ptree;
  326. function genwithnode(symtable : psymtable;l,r : ptree;count : longint) : ptree;
  327. function getcopy(p : ptree) : ptree;
  328. function equal_trees(t1,t2 : ptree) : boolean;
  329. procedure swaptree(p:Ptree);
  330. procedure disposetree(p : ptree);
  331. procedure putnode(p : ptree);
  332. function getnode : ptree;
  333. procedure set_file_line(from,_to : ptree);
  334. procedure set_tree_filepos(p : ptree;const filepos : tfileposinfo);
  335. procedure set_location(var destloc,sourceloc : tlocation);
  336. {$ifdef EXTDEBUG}
  337. procedure compare_trees(oldp,p : ptree);
  338. const
  339. maxfirstpasscount : longint = 0;
  340. { writes a complete tree, checks for nil }
  341. procedure writenode(n : pnode);
  342. {$endif EXTDEBUG}
  343. { sets the callunique flag, if the node is a vecn, }
  344. { takes care of type casts etc. }
  345. procedure set_unique(p : ptree);
  346. { gibt den ordinalen Werten der Node zurueck oder falls sie }
  347. { keinen ordinalen Wert hat, wird ein Fehler erzeugt }
  348. function get_ordinal_value(p : ptree) : longint;
  349. function is_constnode(p : ptree) : boolean;
  350. { true, if p is a pointer to a const int value }
  351. function is_constintnode(p : ptree) : boolean;
  352. function is_constboolnode(p : ptree) : boolean;
  353. function is_constrealnode(p : ptree) : boolean;
  354. function is_constcharnode(p : ptree) : boolean;
  355. function str_length(p : ptree) : longint;
  356. function is_emptyset(p : ptree):boolean;
  357. {$I innr.inc}
  358. implementation
  359. uses
  360. systems,
  361. globals,verbose,files,types;
  362. {$ifdef EXTDEBUG}
  363. const
  364. indention : string = '';
  365. procedure writenode(n : pnode);
  366. begin
  367. if assigned(n) then
  368. n^.dowrite
  369. else
  370. writeln(indention,'nil');
  371. end;
  372. {$endif EXTDEBUG}
  373. {****************************************************************************
  374. TNODE
  375. ****************************************************************************}
  376. constructor tnode.init;
  377. begin
  378. treetype:=nothingn;
  379. { this allows easier error tracing }
  380. location.loc:=LOC_INVALID;
  381. { save local info }
  382. fileinfo:=aktfilepos;
  383. localswitches:=aktlocalswitches;
  384. resulttype:=nil;
  385. registersint:=0;
  386. registersfpu:=0;
  387. {$ifdef SUPPORT_MMX}
  388. registersmmx:=0;
  389. {$endif SUPPORT_MMX}
  390. end;
  391. destructor tnode.done;
  392. begin
  393. { reference info }
  394. if (location.loc in [LOC_MEM,LOC_REFERENCE]) and
  395. assigned(location.reference.symbol) then
  396. dispose(location.reference.symbol,done);
  397. {$ifdef EXTDEBUG}
  398. if firstpasscount>maxfirstpasscount then
  399. maxfirstpasscount:=firstpasscount;
  400. {$endif EXTDEBUG}
  401. end;
  402. procedure tnode.pass_1;
  403. begin
  404. if not(assigned(resulttype)) then
  405. det_resulttype;
  406. det_temp;
  407. end;
  408. procedure tnode.det_resulttype;
  409. begin
  410. abstract;
  411. end;
  412. procedure tnode.det_temp;
  413. begin
  414. abstract;
  415. end;
  416. procedure tnode.secondpass;
  417. begin
  418. abstract;
  419. end;
  420. {$ifdef EXTDEBUG}
  421. procedure tnode.dowrite;
  422. const treetype2str : array[ttreetyp] of string[20] = (
  423. 'addn',
  424. 'muln',
  425. 'subn',
  426. 'divn',
  427. 'symdifn',
  428. 'modn',
  429. 'assignn',
  430. 'loadn',
  431. 'rangen',
  432. 'ltn',
  433. 'lten',
  434. 'gtn',
  435. 'gten',
  436. 'equaln',
  437. 'unequaln',
  438. 'inn',
  439. 'orn',
  440. 'xorn',
  441. 'shrn',
  442. 'shln',
  443. 'slashn',
  444. 'andn',
  445. 'subscriptn',
  446. 'derefn',
  447. 'addrn',
  448. 'doubleaddrn',
  449. 'ordconstn',
  450. 'typeconvn',
  451. 'calln',
  452. 'callparan',
  453. 'realconstn',
  454. 'fixconstn',
  455. 'umminusn',
  456. 'asmn',
  457. 'vecn',
  458. 'stringconstn',
  459. 'funcretn',
  460. 'selfn',
  461. 'notn',
  462. 'inlinen',
  463. 'niln',
  464. 'errorn',
  465. 'typen',
  466. 'hnewn',
  467. 'hdisposen',
  468. 'newn',
  469. 'simpledisposen',
  470. 'setelementn',
  471. 'setconstn',
  472. 'blockn',
  473. 'statementn',
  474. 'loopn',
  475. 'ifn',
  476. 'breakn',
  477. 'continuen',
  478. 'repeatn',
  479. 'whilen',
  480. 'forn',
  481. 'exitn',
  482. 'withn',
  483. 'casen',
  484. 'labeln',
  485. 'goton',
  486. 'simplenewn',
  487. 'tryexceptn',
  488. 'raisen',
  489. 'switchesn',
  490. 'tryfinallyn',
  491. 'onn',
  492. 'isn',
  493. 'asn',
  494. 'caretn',
  495. 'failn',
  496. 'starstarn',
  497. 'procinlinen',
  498. 'arrayconstructn',
  499. 'arrayconstructrangen',
  500. 'nothingn',
  501. 'loadvmtn');
  502. begin
  503. write(indention,'(',treetype2str[treetype]);
  504. end;
  505. {$endif EXTDEBUG}
  506. {****************************************************************************
  507. TUNARYNODE
  508. ****************************************************************************}
  509. constructor tunarynode.init(l : pnode);
  510. begin
  511. inherited init;
  512. left:=l;
  513. end;
  514. {$ifdef extdebug}
  515. procedure tunarynode.dowrite;
  516. begin
  517. inherited dowrite;
  518. writeln(',');
  519. writenode(left);
  520. writeln(')');
  521. dec(byte(indention[0]),2);
  522. end;
  523. {$endif}
  524. {****************************************************************************
  525. TBINARYNODE
  526. ****************************************************************************}
  527. constructor tbinarynode.init(l,r : pnode);
  528. begin
  529. inherited init(l);
  530. right:=r
  531. end;
  532. {****************************************************************************
  533. TBINOPYNODE
  534. ****************************************************************************}
  535. constructor tbinopnode.init(l,r : pnode);
  536. begin
  537. inherited init(l,r);
  538. swaped:=false;
  539. end;
  540. {$ifdef dummy}
  541. { clean up the contents of a node }
  542. case p^.treetype of
  543. asmn : if assigned(p^.p_asm) then
  544. dispose(p^.p_asm,done);
  545. stringconstn : begin
  546. ansistringdispose(p^.value_str,p^.length);
  547. end;
  548. setconstn : begin
  549. if assigned(p^.value_set) then
  550. dispose(p^.value_set);
  551. end;
  552. end;
  553. {$endif dummy}
  554. procedure deletecaselabels(p : pcaserecord);
  555. begin
  556. if assigned(p^.greater) then
  557. deletecaselabels(p^.greater);
  558. if assigned(p^.less) then
  559. deletecaselabels(p^.less);
  560. freelabel(p^._at);
  561. if p^.firstlabel then
  562. freelabel(p^.statement);
  563. dispose(p);
  564. end;
  565. procedure swaptree(p:Ptree);
  566. var swapp:Ptree;
  567. begin
  568. swapp:=p^.right;
  569. p^.right:=p^.left;
  570. p^.left:=swapp;
  571. p^.swaped:=not(p^.swaped);
  572. end;
  573. function gennode(t : ttreetyp;l,r : ptree) : ptree;
  574. var
  575. p : ptree;
  576. begin
  577. p:=getnode;
  578. p^.disposetyp:=dt_leftright;
  579. p^.treetype:=t;
  580. p^.left:=l;
  581. p^.right:=r;
  582. p^.registers32:=0;
  583. { p^.registers16:=0;
  584. p^.registers8:=0; }
  585. p^.registersfpu:=0;
  586. {$ifdef SUPPORT_MMX}
  587. p^.registersmmx:=0;
  588. {$endif SUPPORT_MMX}
  589. p^.resulttype:=nil;
  590. gennode:=p;
  591. end;
  592. procedure disposetree(p : ptree);
  593. var
  594. symt : psymtable;
  595. i : longint;
  596. begin
  597. if not(assigned(p)) then
  598. exit;
  599. case p^.disposetyp of
  600. dt_leftright :
  601. begin
  602. if assigned(p^.left) then
  603. disposetree(p^.left);
  604. if assigned(p^.right) then
  605. disposetree(p^.right);
  606. end;
  607. dt_case :
  608. begin
  609. if assigned(p^.left) then
  610. disposetree(p^.left);
  611. if assigned(p^.right) then
  612. disposetree(p^.right);
  613. if assigned(p^.nodes) then
  614. deletecaselabels(p^.nodes);
  615. if assigned(p^.elseblock) then
  616. disposetree(p^.elseblock);
  617. end;
  618. dt_nothing : ;
  619. dt_left :
  620. if assigned(p^.left) then
  621. disposetree(p^.left);
  622. dt_mbleft :
  623. if assigned(p^.left) then
  624. disposetree(p^.left);
  625. dt_mbleft_and_method :
  626. begin
  627. if assigned(p^.left) then disposetree(p^.left);
  628. disposetree(p^.methodpointer);
  629. end;
  630. dt_typeconv : disposetree(p^.left);
  631. dt_inlinen :
  632. if assigned(p^.left) then
  633. disposetree(p^.left);
  634. dt_loop :
  635. begin
  636. if assigned(p^.left) then
  637. disposetree(p^.left);
  638. if assigned(p^.right) then
  639. disposetree(p^.right);
  640. if assigned(p^.t1) then
  641. disposetree(p^.t1);
  642. if assigned(p^.t2) then
  643. disposetree(p^.t2);
  644. end;
  645. dt_onn:
  646. begin
  647. if assigned(p^.left) then
  648. disposetree(p^.left);
  649. if assigned(p^.right) then
  650. disposetree(p^.right);
  651. if assigned(p^.exceptsymtable) then
  652. dispose(p^.exceptsymtable,done);
  653. end;
  654. dt_with :
  655. begin
  656. if assigned(p^.left) then
  657. disposetree(p^.left);
  658. if assigned(p^.right) then
  659. disposetree(p^.right);
  660. symt:=p^.withsymtable;
  661. for i:=1 to p^.tablecount do
  662. begin
  663. if assigned(symt) then
  664. begin
  665. p^.withsymtable:=pwithsymtable(symt^.next);
  666. dispose(symt,done);
  667. end;
  668. symt:=p^.withsymtable;
  669. end;
  670. end;
  671. else internalerror(12);
  672. end;
  673. putnode(p);
  674. end;
  675. procedure set_file_line(from,_to : ptree);
  676. begin
  677. if assigned(from) then
  678. _to^.fileinfo:=from^.fileinfo;
  679. end;
  680. procedure set_tree_filepos(p : ptree;const filepos : tfileposinfo);
  681. begin
  682. p^.fileinfo:=filepos;
  683. end;
  684. function getnode : ptree;
  685. var
  686. hp : ptree;
  687. begin
  688. new(hp);
  689. { makes error tracking easier }
  690. fillchar(hp^,sizeof(ttree),0);
  691. { reset }
  692. hp^.location.loc:=LOC_INVALID;
  693. { save local info }
  694. hp^.fileinfo:=aktfilepos;
  695. hp^.localswitches:=aktlocalswitches;
  696. getnode:=hp;
  697. end;
  698. procedure putnode(p : ptree);
  699. begin
  700. { clean up the contents of a node }
  701. case p^.treetype of
  702. asmn : if assigned(p^.p_asm) then
  703. dispose(p^.p_asm,done);
  704. stringconstn : begin
  705. ansistringdispose(p^.value_str,p^.length);
  706. end;
  707. setconstn : begin
  708. if assigned(p^.value_set) then
  709. dispose(p^.value_set);
  710. end;
  711. end;
  712. { reference info }
  713. if (p^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  714. assigned(p^.location.reference.symbol) then
  715. dispose(p^.location.reference.symbol,done);
  716. {$ifdef extdebug}
  717. if p^.firstpasscount>maxfirstpasscount then
  718. maxfirstpasscount:=p^.firstpasscount;
  719. {$endif extdebug}
  720. dispose(p);
  721. end;
  722. function getcopy(p : ptree) : ptree;
  723. var
  724. hp : ptree;
  725. begin
  726. hp:=getnode;
  727. hp^:=p^;
  728. if assigned(p^.location.reference.symbol) then
  729. hp^.location.reference.symbol:=p^.location.reference.symbol;
  730. case p^.disposetyp of
  731. dt_leftright :
  732. begin
  733. if assigned(p^.left) then
  734. hp^.left:=getcopy(p^.left);
  735. if assigned(p^.right) then
  736. hp^.right:=getcopy(p^.right);
  737. end;
  738. dt_nothing : ;
  739. dt_left :
  740. if assigned(p^.left) then
  741. hp^.left:=getcopy(p^.left);
  742. dt_mbleft :
  743. if assigned(p^.left) then
  744. hp^.left:=getcopy(p^.left);
  745. dt_mbleft_and_method :
  746. begin
  747. if assigned(p^.left) then
  748. hp^.left:=getcopy(p^.left);
  749. hp^.methodpointer:=getcopy(p^.methodpointer);
  750. end;
  751. dt_loop :
  752. begin
  753. if assigned(p^.left) then
  754. hp^.left:=getcopy(p^.left);
  755. if assigned(p^.right) then
  756. hp^.right:=getcopy(p^.right);
  757. if assigned(p^.t1) then
  758. hp^.t1:=getcopy(p^.t1);
  759. if assigned(p^.t2) then
  760. hp^.t2:=getcopy(p^.t2);
  761. end;
  762. dt_typeconv : hp^.left:=getcopy(p^.left);
  763. dt_inlinen :
  764. if assigned(p^.left) then
  765. hp^.left:=getcopy(p^.left);
  766. else internalerror(11);
  767. end;
  768. { now check treetype }
  769. case p^.treetype of
  770. stringconstn : begin
  771. hp^.value_str:=getpcharcopy(p);
  772. hp^.length:=p^.length;
  773. end;
  774. setconstn : begin
  775. new(hp^.value_set);
  776. hp^.value_set:=p^.value_set;
  777. end;
  778. end;
  779. getcopy:=hp;
  780. end;
  781. function genloadnode(v : pvarsym;st : psymtable) : ptree;
  782. var
  783. p : ptree;
  784. begin
  785. p:=getnode;
  786. p^.registers32:=0;
  787. { p^.registers16:=0;
  788. p^.registers8:=0; }
  789. p^.registersfpu:=0;
  790. {$ifdef SUPPORT_MMX}
  791. p^.registersmmx:=0;
  792. {$endif SUPPORT_MMX}
  793. p^.treetype:=loadn;
  794. p^.resulttype:=v^.definition;
  795. p^.symtableentry:=v;
  796. p^.symtable:=st;
  797. p^.is_first := False;
  798. { method pointer load nodes can use the left subtree }
  799. p^.disposetyp:=dt_left;
  800. p^.left:=nil;
  801. genloadnode:=p;
  802. end;
  803. function genwithnode(symtable : psymtable;l,r : ptree;count : longint) : ptree;
  804. var
  805. p : ptree;
  806. begin
  807. p:=getnode;
  808. p^.disposetyp:=dt_with;
  809. p^.treetype:=withn;
  810. p^.left:=l;
  811. p^.right:=r;
  812. p^.registers32:=0;
  813. { p^.registers16:=0;
  814. p^.registers8:=0; }
  815. p^.registersfpu:=0;
  816. {$ifdef SUPPORT_MMX}
  817. p^.registersmmx:=0;
  818. {$endif SUPPORT_MMX}
  819. p^.resulttype:=nil;
  820. p^.withsymtable:=pwithsymtable(symtable);
  821. p^.tablecount:=count;
  822. set_file_line(l,p);
  823. genwithnode:=p;
  824. end;
  825. function genfixconstnode(v : longint;def : pdef) : ptree;
  826. var
  827. p : ptree;
  828. begin
  829. p:=getnode;
  830. p^.disposetyp:=dt_nothing;
  831. p^.treetype:=fixconstn;
  832. p^.registers32:=0;
  833. { p^.registers16:=0;
  834. p^.registers8:=0; }
  835. p^.registersfpu:=0;
  836. {$ifdef SUPPORT_MMX}
  837. p^.registersmmx:=0;
  838. {$endif SUPPORT_MMX}
  839. p^.resulttype:=def;
  840. p^.value:=v;
  841. genfixconstnode:=p;
  842. end;
  843. function gencallparanode(expr,next : ptree) : ptree;
  844. var
  845. p : ptree;
  846. begin
  847. p:=getnode;
  848. p^.disposetyp:=dt_leftright;
  849. p^.treetype:=callparan;
  850. p^.left:=expr;
  851. p^.right:=next;
  852. p^.registers32:=0;
  853. { p^.registers16:=0;
  854. p^.registers8:=0; }
  855. {$ifdef SUPPORT_MMX}
  856. p^.registersmmx:=0;
  857. {$endif SUPPORT_MMX}
  858. p^.registersfpu:=0;
  859. p^.resulttype:=nil;
  860. p^.exact_match_found:=false;
  861. p^.is_colon_para:=false;
  862. set_file_line(expr,p);
  863. gencallparanode:=p;
  864. end;
  865. function gencasenode(l,r : ptree;nodes : pcaserecord) : ptree;
  866. var
  867. p : ptree;
  868. begin
  869. p:=getnode;
  870. p^.disposetyp:=dt_case;
  871. p^.treetype:=casen;
  872. p^.left:=l;
  873. p^.right:=r;
  874. p^.nodes:=nodes;
  875. p^.registers32:=0;
  876. p^.registersfpu:=0;
  877. {$ifdef SUPPORT_MMX}
  878. p^.registersmmx:=0;
  879. {$endif SUPPORT_MMX}
  880. p^.resulttype:=nil;
  881. set_file_line(l,p);
  882. gencasenode:=p;
  883. end;
  884. function genloopnode(t : ttreetyp;l,r,n1 : ptree;back : boolean) : ptree;
  885. var
  886. p : ptree;
  887. begin
  888. p:=getnode;
  889. p^.disposetyp:=dt_loop;
  890. p^.treetype:=t;
  891. p^.left:=l;
  892. p^.right:=r;
  893. p^.t1:=n1;
  894. p^.t2:=nil;
  895. p^.registers32:=0;
  896. p^.backward:=back;
  897. { p^.registers16:=0;
  898. p^.registers8:=0; }
  899. p^.registersfpu:=0;
  900. {$ifdef SUPPORT_MMX}
  901. p^.registersmmx:=0;
  902. {$endif SUPPORT_MMX}
  903. p^.resulttype:=nil;
  904. set_file_line(l,p);
  905. genloopnode:=p;
  906. end;
  907. function genordinalconstnode(v : longint;def : pdef) : ptree;
  908. var
  909. p : ptree;
  910. begin
  911. p:=getnode;
  912. p^.disposetyp:=dt_nothing;
  913. p^.treetype:=ordconstn;
  914. p^.registers32:=0;
  915. { p^.registers16:=0;
  916. p^.registers8:=0; }
  917. p^.registersfpu:=0;
  918. {$ifdef SUPPORT_MMX}
  919. p^.registersmmx:=0;
  920. {$endif SUPPORT_MMX}
  921. p^.resulttype:=def;
  922. p^.value:=v;
  923. if p^.resulttype^.deftype=orddef then
  924. testrange(p^.resulttype,p^.value);
  925. genordinalconstnode:=p;
  926. end;
  927. function genenumnode(v : penumsym) : ptree;
  928. var
  929. p : ptree;
  930. begin
  931. p:=getnode;
  932. p^.disposetyp:=dt_nothing;
  933. p^.treetype:=ordconstn;
  934. p^.registers32:=0;
  935. { p^.registers16:=0;
  936. p^.registers8:=0; }
  937. p^.registersfpu:=0;
  938. {$ifdef SUPPORT_MMX}
  939. p^.registersmmx:=0;
  940. {$endif SUPPORT_MMX}
  941. p^.resulttype:=v^.definition;
  942. p^.value:=v^.value;
  943. testrange(p^.resulttype,p^.value);
  944. genenumnode:=p;
  945. end;
  946. function genrealconstnode(v : bestreal;def : pdef) : ptree;
  947. var
  948. p : ptree;
  949. begin
  950. p:=getnode;
  951. p^.disposetyp:=dt_nothing;
  952. p^.treetype:=realconstn;
  953. p^.registers32:=0;
  954. { p^.registers16:=0;
  955. p^.registers8:=0; }
  956. p^.registersfpu:=0;
  957. {$ifdef SUPPORT_MMX}
  958. p^.registersmmx:=0;
  959. {$endif SUPPORT_MMX}
  960. p^.resulttype:=def;
  961. p^.value_real:=v;
  962. p^.lab_real:=nil;
  963. genrealconstnode:=p;
  964. end;
  965. function genstringconstnode(const s : string) : ptree;
  966. var
  967. p : ptree;
  968. l : longint;
  969. begin
  970. p:=getnode;
  971. p^.disposetyp:=dt_nothing;
  972. p^.treetype:=stringconstn;
  973. p^.registers32:=0;
  974. { p^.registers16:=0;
  975. p^.registers8:=0; }
  976. p^.registersfpu:=0;
  977. {$ifdef SUPPORT_MMX}
  978. p^.registersmmx:=0;
  979. {$endif SUPPORT_MMX}
  980. l:=length(s);
  981. p^.length:=l;
  982. { stringdup write even past a #0 }
  983. getmem(p^.value_str,l+1);
  984. move(s[1],p^.value_str^,l);
  985. p^.value_str[l]:=#0;
  986. p^.lab_str:=nil;
  987. if cs_ansistrings in aktlocalswitches then
  988. begin
  989. p^.stringtype:=st_ansistring;
  990. p^.resulttype:=cansistringdef;
  991. end
  992. else
  993. begin
  994. p^.stringtype:=st_shortstring;
  995. p^.resulttype:=cshortstringdef;
  996. end;
  997. genstringconstnode:=p;
  998. end;
  999. function getpcharcopy(p : ptree) : pchar;
  1000. var
  1001. pc : pchar;
  1002. begin
  1003. pc:=nil;
  1004. getmem(pc,p^.length+1);
  1005. if pc=nil then
  1006. Message(general_f_no_memory_left);
  1007. move(p^.value_str^,pc^,p^.length+1);
  1008. getpcharcopy:=pc;
  1009. end;
  1010. function genpcharconstnode(s : pchar;length : longint) : ptree;
  1011. var
  1012. p : ptree;
  1013. begin
  1014. p:=getnode;
  1015. p^.disposetyp:=dt_nothing;
  1016. p^.treetype:=stringconstn;
  1017. p^.registers32:=0;
  1018. { p^.registers16:=0;
  1019. p^.registers8:=0; }
  1020. p^.registersfpu:=0;
  1021. {$ifdef SUPPORT_MMX}
  1022. p^.registersmmx:=0;
  1023. {$endif SUPPORT_MMX}
  1024. p^.resulttype:=cshortstringdef;
  1025. p^.length:=length;
  1026. p^.value_str:=s;
  1027. p^.lab_str:=nil;
  1028. genpcharconstnode:=p;
  1029. end;
  1030. function gensinglenode(t : ttreetyp;l : ptree) : ptree;
  1031. var
  1032. p : ptree;
  1033. begin
  1034. p:=getnode;
  1035. p^.disposetyp:=dt_left;
  1036. p^.treetype:=t;
  1037. p^.left:=l;
  1038. p^.registers32:=0;
  1039. { p^.registers16:=0;
  1040. p^.registers8:=0; }
  1041. p^.registersfpu:=0;
  1042. {$ifdef SUPPORT_MMX}
  1043. p^.registersmmx:=0;
  1044. {$endif SUPPORT_MMX}
  1045. p^.resulttype:=nil;
  1046. gensinglenode:=p;
  1047. end;
  1048. function genasmnode(p_asm : paasmoutput) : ptree;
  1049. var
  1050. p : ptree;
  1051. begin
  1052. p:=getnode;
  1053. p^.disposetyp:=dt_nothing;
  1054. p^.treetype:=asmn;
  1055. p^.registers32:=4;
  1056. p^.p_asm:=p_asm;
  1057. p^.object_preserved:=false;
  1058. { p^.registers16:=0;
  1059. p^.registers8:=0; }
  1060. p^.registersfpu:=8;
  1061. {$ifdef SUPPORT_MMX}
  1062. p^.registersmmx:=8;
  1063. {$endif SUPPORT_MMX}
  1064. p^.resulttype:=nil;
  1065. genasmnode:=p;
  1066. end;
  1067. function genloadcallnode(v: pprocsym;st: psymtable): ptree;
  1068. var
  1069. p : ptree;
  1070. begin
  1071. p:=getnode;
  1072. p^.registers32:=0;
  1073. { p^.registers16:=0;
  1074. p^.registers8:=0; }
  1075. p^.registersfpu:=0;
  1076. {$ifdef SUPPORT_MMX}
  1077. p^.registersmmx:=0;
  1078. {$endif SUPPORT_MMX}
  1079. p^.treetype:=loadn;
  1080. p^.resulttype:=v^.definition;
  1081. p^.symtableentry:=v;
  1082. p^.symtable:=st;
  1083. p^.is_first := False;
  1084. p^.disposetyp:=dt_nothing;
  1085. genloadcallnode:=p;
  1086. end;
  1087. function gentypedconstloadnode(sym : ptypedconstsym;st : psymtable) : ptree;
  1088. var
  1089. p : ptree;
  1090. begin
  1091. p:=getnode;
  1092. p^.registers32:=0;
  1093. { p^.registers16:=0;
  1094. p^.registers8:=0; }
  1095. p^.registersfpu:=0;
  1096. {$ifdef SUPPORT_MMX}
  1097. p^.registersmmx:=0;
  1098. {$endif SUPPORT_MMX}
  1099. p^.treetype:=loadn;
  1100. p^.resulttype:=sym^.definition;
  1101. p^.symtableentry:=pvarsym(sym);
  1102. p^.symtable:=st;
  1103. p^.disposetyp:=dt_nothing;
  1104. gentypedconstloadnode:=p;
  1105. end;
  1106. function gentypeconvnode(node : ptree;t : pdef) : ptree;
  1107. var
  1108. p : ptree;
  1109. begin
  1110. p:=getnode;
  1111. p^.disposetyp:=dt_typeconv;
  1112. p^.treetype:=typeconvn;
  1113. p^.left:=node;
  1114. p^.registers32:=0;
  1115. { p^.registers16:=0;
  1116. p^.registers8:=0; }
  1117. p^.convtyp:=tc_equal;
  1118. p^.registersfpu:=0;
  1119. {$ifdef SUPPORT_MMX}
  1120. p^.registersmmx:=0;
  1121. {$endif SUPPORT_MMX}
  1122. p^.resulttype:=t;
  1123. p^.explizit:=false;
  1124. set_file_line(node,p);
  1125. gentypeconvnode:=p;
  1126. end;
  1127. function gencallnode(v : pprocsym;st : psymtable) : ptree;
  1128. var
  1129. p : ptree;
  1130. begin
  1131. p:=getnode;
  1132. p^.registers32:=0;
  1133. { p^.registers16:=0;
  1134. p^.registers8:=0; }
  1135. p^.registersfpu:=0;
  1136. {$ifdef SUPPORT_MMX}
  1137. p^.registersmmx:=0;
  1138. {$endif SUPPORT_MMX}
  1139. p^.treetype:=calln;
  1140. p^.symtableprocentry:=v;
  1141. p^.symtableproc:=st;
  1142. p^.unit_specific:=false;
  1143. p^.no_check:=false;
  1144. p^.return_value_used:=true;
  1145. p^.disposetyp := dt_leftright;
  1146. p^.methodpointer:=nil;
  1147. p^.left:=nil;
  1148. p^.right:=nil;
  1149. p^.procdefinition:=nil;
  1150. gencallnode:=p;
  1151. end;
  1152. function genmethodcallnode(v : pprocsym;st : psymtable;mp : ptree) : ptree;
  1153. var
  1154. p : ptree;
  1155. begin
  1156. p:=getnode;
  1157. p^.registers32:=0;
  1158. { p^.registers16:=0;
  1159. p^.registers8:=0; }
  1160. p^.registersfpu:=0;
  1161. {$ifdef SUPPORT_MMX}
  1162. p^.registersmmx:=0;
  1163. {$endif SUPPORT_MMX}
  1164. p^.treetype:=calln;
  1165. p^.return_value_used:=true;
  1166. p^.symtableprocentry:=v;
  1167. p^.symtableproc:=st;
  1168. p^.disposetyp:=dt_mbleft_and_method;
  1169. p^.left:=nil;
  1170. p^.right:=nil;
  1171. p^.methodpointer:=mp;
  1172. p^.procdefinition:=nil;
  1173. genmethodcallnode:=p;
  1174. end;
  1175. function gensubscriptnode(varsym : pvarsym;l : ptree) : ptree;
  1176. var
  1177. p : ptree;
  1178. begin
  1179. p:=getnode;
  1180. p^.disposetyp:=dt_left;
  1181. p^.treetype:=subscriptn;
  1182. p^.left:=l;
  1183. p^.registers32:=0;
  1184. p^.vs:=varsym;
  1185. { p^.registers16:=0;
  1186. p^.registers8:=0; }
  1187. p^.registersfpu:=0;
  1188. {$ifdef SUPPORT_MMX}
  1189. p^.registersmmx:=0;
  1190. {$endif SUPPORT_MMX}
  1191. p^.resulttype:=nil;
  1192. gensubscriptnode:=p;
  1193. end;
  1194. function genzeronode(t : ttreetyp) : ptree;
  1195. var
  1196. p : ptree;
  1197. begin
  1198. p:=getnode;
  1199. p^.disposetyp:=dt_nothing;
  1200. p^.treetype:=t;
  1201. p^.registers32:=0;
  1202. { p^.registers16:=0;
  1203. p^.registers8:=0; }
  1204. p^.registersfpu:=0;
  1205. {$ifdef SUPPORT_MMX}
  1206. p^.registersmmx:=0;
  1207. {$endif SUPPORT_MMX}
  1208. p^.resulttype:=nil;
  1209. genzeronode:=p;
  1210. end;
  1211. function genlabelnode(t : ttreetyp;nr : pasmlabel) : ptree;
  1212. var
  1213. p : ptree;
  1214. begin
  1215. p:=getnode;
  1216. p^.disposetyp:=dt_nothing;
  1217. p^.treetype:=t;
  1218. p^.registers32:=0;
  1219. { p^.registers16:=0;
  1220. p^.registers8:=0; }
  1221. p^.registersfpu:=0;
  1222. {$ifdef SUPPORT_MMX}
  1223. p^.registersmmx:=0;
  1224. {$endif SUPPORT_MMX}
  1225. p^.resulttype:=nil;
  1226. { for security }
  1227. { nr^.is_used:=true;}
  1228. p^.labelnr:=nr;
  1229. genlabelnode:=p;
  1230. end;
  1231. function genselfnode(_class : pdef) : ptree;
  1232. var
  1233. p : ptree;
  1234. begin
  1235. p:=getnode;
  1236. p^.disposetyp:=dt_nothing;
  1237. p^.treetype:=selfn;
  1238. p^.registers32:=0;
  1239. { p^.registers16:=0;
  1240. p^.registers8:=0; }
  1241. p^.registersfpu:=0;
  1242. {$ifdef SUPPORT_MMX}
  1243. p^.registersmmx:=0;
  1244. {$endif SUPPORT_MMX}
  1245. p^.resulttype:=_class;
  1246. genselfnode:=p;
  1247. end;
  1248. function geninlinenode(number : byte;is_const:boolean;l : ptree) : ptree;
  1249. var
  1250. p : ptree;
  1251. begin
  1252. p:=getnode;
  1253. p^.disposetyp:=dt_inlinen;
  1254. p^.treetype:=inlinen;
  1255. p^.left:=l;
  1256. p^.inlinenumber:=number;
  1257. p^.inlineconst:=is_const;
  1258. p^.registers32:=0;
  1259. { p^.registers16:=0;
  1260. p^.registers8:=0; }
  1261. p^.registersfpu:=0;
  1262. {$ifdef SUPPORT_MMX}
  1263. p^.registersmmx:=0;
  1264. {$endif SUPPORT_MMX}
  1265. p^.resulttype:=nil;
  1266. geninlinenode:=p;
  1267. end;
  1268. function gentypenode(t : pdef;sym:ptypesym) : ptree;
  1269. var
  1270. p : ptree;
  1271. begin
  1272. p:=getnode;
  1273. p^.disposetyp:=dt_nothing;
  1274. p^.treetype:=typen;
  1275. p^.registers32:=0;
  1276. { p^.registers16:=0;
  1277. p^.registers8:=0; }
  1278. p^.registersfpu:=0;
  1279. {$ifdef SUPPORT_MMX}
  1280. p^.registersmmx:=0;
  1281. {$endif SUPPORT_MMX}
  1282. p^.resulttype:=generrordef;
  1283. p^.typenodetype:=t;
  1284. p^.typenodesym:=sym;
  1285. gentypenode:=p;
  1286. end;
  1287. function genloadmethodcallnode(v: pprocsym;st: psymtable; mp:ptree): ptree;
  1288. var
  1289. p : ptree;
  1290. begin
  1291. p:=getnode;
  1292. p^.registers32:=0;
  1293. { p^.registers16:=0;
  1294. p^.registers8:=0; }
  1295. p^.registersfpu:=0;
  1296. {$ifdef SUPPORT_MMX}
  1297. p^.registersmmx:=0;
  1298. {$endif SUPPORT_MMX}
  1299. p^.treetype:=loadn;
  1300. p^.left:=nil;
  1301. p^.resulttype:=v^.definition;
  1302. p^.symtableentry:=v;
  1303. p^.symtable:=st;
  1304. p^.is_first := False;
  1305. p^.disposetyp:=dt_left;
  1306. p^.left:=mp;
  1307. genloadmethodcallnode:=p;
  1308. end;
  1309. { uses the callnode to create the new procinline node }
  1310. function genprocinlinenode(callp,code : ptree) : ptree;
  1311. var
  1312. p : ptree;
  1313. begin
  1314. genprocinlinenode:=p;
  1315. end;
  1316. function gensetconstnode(s : pconstset;settype : psetdef) : ptree;
  1317. var
  1318. p : ptree;
  1319. begin
  1320. p:=getnode;
  1321. p^.disposetyp:=dt_nothing;
  1322. p^.treetype:=setconstn;
  1323. p^.registers32:=0;
  1324. p^.registersfpu:=0;
  1325. {$ifdef SUPPORT_MMX}
  1326. p^.registersmmx:=0;
  1327. {$endif SUPPORT_MMX}
  1328. p^.resulttype:=settype;
  1329. p^.left:=nil;
  1330. new(p^.value_set);
  1331. p^.value_set^:=s^;
  1332. gensetconstnode:=p;
  1333. end;
  1334. procedure set_location(var destloc,sourceloc : tlocation);
  1335. begin
  1336. destloc:= sourceloc;
  1337. end;
  1338. {$ifdef extdebug}
  1339. procedure compare_trees(oldp,p : ptree);
  1340. var
  1341. error_found : boolean;
  1342. begin
  1343. if oldp^.resulttype<>p^.resulttype then
  1344. begin
  1345. error_found:=true;
  1346. if is_equal(oldp^.resulttype,p^.resulttype) then
  1347. comment(v_debug,'resulttype fields are different but equal')
  1348. else
  1349. comment(v_warning,'resulttype fields are really different');
  1350. end;
  1351. if oldp^.treetype<>p^.treetype then
  1352. begin
  1353. comment(v_warning,'treetype field different');
  1354. error_found:=true;
  1355. end
  1356. else
  1357. comment(v_debug,' treetype '+tostr(longint(oldp^.treetype)));
  1358. if oldp^.error<>p^.error then
  1359. begin
  1360. comment(v_warning,'error field different');
  1361. error_found:=true;
  1362. end;
  1363. if oldp^.disposetyp<>p^.disposetyp then
  1364. begin
  1365. comment(v_warning,'disposetyp field different');
  1366. error_found:=true;
  1367. end;
  1368. { is true, if the right and left operand are swaped }
  1369. if oldp^.swaped<>p^.swaped then
  1370. begin
  1371. comment(v_warning,'swaped field different');
  1372. error_found:=true;
  1373. end;
  1374. { the location of the result of this node }
  1375. if oldp^.location.loc<>p^.location.loc then
  1376. begin
  1377. comment(v_warning,'location.loc field different');
  1378. error_found:=true;
  1379. end;
  1380. { the number of registers needed to evalute the node }
  1381. if oldp^.registers32<>p^.registers32 then
  1382. begin
  1383. comment(v_warning,'registers32 field different');
  1384. comment(v_warning,' old '+tostr(oldp^.registers32)+'<> new '+tostr(p^.registers32));
  1385. error_found:=true;
  1386. end;
  1387. if oldp^.registersfpu<>p^.registersfpu then
  1388. begin
  1389. comment(v_warning,'registersfpu field different');
  1390. error_found:=true;
  1391. end;
  1392. {$ifdef SUPPORT_MMX}
  1393. if oldp^.registersmmx<>p^.registersmmx then
  1394. begin
  1395. comment(v_warning,'registersmmx field different');
  1396. error_found:=true;
  1397. end;
  1398. {$endif SUPPORT_MMX}
  1399. if oldp^.left<>p^.left then
  1400. begin
  1401. comment(v_warning,'left field different');
  1402. error_found:=true;
  1403. end;
  1404. if oldp^.right<>p^.right then
  1405. begin
  1406. comment(v_warning,'right field different');
  1407. error_found:=true;
  1408. end;
  1409. if oldp^.fileinfo.line<>p^.fileinfo.line then
  1410. begin
  1411. comment(v_warning,'fileinfo.line field different');
  1412. error_found:=true;
  1413. end;
  1414. if oldp^.fileinfo.column<>p^.fileinfo.column then
  1415. begin
  1416. comment(v_warning,'fileinfo.column field different');
  1417. error_found:=true;
  1418. end;
  1419. if oldp^.fileinfo.fileindex<>p^.fileinfo.fileindex then
  1420. begin
  1421. comment(v_warning,'fileinfo.fileindex field different');
  1422. error_found:=true;
  1423. end;
  1424. if oldp^.localswitches<>p^.localswitches then
  1425. begin
  1426. comment(v_warning,'localswitches field different');
  1427. error_found:=true;
  1428. end;
  1429. {$ifdef extdebug}
  1430. if oldp^.firstpasscount<>p^.firstpasscount then
  1431. begin
  1432. comment(v_warning,'firstpasscount field different');
  1433. error_found:=true;
  1434. end;
  1435. {$endif extdebug}
  1436. if oldp^.treetype=p^.treetype then
  1437. case oldp^.treetype of
  1438. addn :
  1439. begin
  1440. if oldp^.use_strconcat<>p^.use_strconcat then
  1441. begin
  1442. comment(v_warning,'use_strconcat field different');
  1443. error_found:=true;
  1444. end;
  1445. if oldp^.string_typ<>p^.string_typ then
  1446. begin
  1447. comment(v_warning,'stringtyp field different');
  1448. error_found:=true;
  1449. end;
  1450. end;
  1451. callparan :
  1452. {(is_colon_para : boolean;exact_match_found : boolean);}
  1453. begin
  1454. if oldp^.is_colon_para<>p^.is_colon_para then
  1455. begin
  1456. comment(v_warning,'use_strconcat field different');
  1457. error_found:=true;
  1458. end;
  1459. if oldp^.exact_match_found<>p^.exact_match_found then
  1460. begin
  1461. comment(v_warning,'exact_match_found field different');
  1462. error_found:=true;
  1463. end;
  1464. end;
  1465. assignn :
  1466. {(assigntyp : tassigntyp;concat_string : boolean);}
  1467. begin
  1468. if oldp^.assigntyp<>p^.assigntyp then
  1469. begin
  1470. comment(v_warning,'assigntyp field different');
  1471. error_found:=true;
  1472. end;
  1473. if oldp^.concat_string<>p^.concat_string then
  1474. begin
  1475. comment(v_warning,'concat_string field different');
  1476. error_found:=true;
  1477. end;
  1478. end;
  1479. loadn :
  1480. {(symtableentry : psym;symtable : psymtable;
  1481. is_absolute,is_first : boolean);}
  1482. begin
  1483. if oldp^.symtableentry<>p^.symtableentry then
  1484. begin
  1485. comment(v_warning,'symtableentry field different');
  1486. error_found:=true;
  1487. end;
  1488. if oldp^.symtable<>p^.symtable then
  1489. begin
  1490. comment(v_warning,'symtable field different');
  1491. error_found:=true;
  1492. end;
  1493. if oldp^.is_absolute<>p^.is_absolute then
  1494. begin
  1495. comment(v_warning,'is_absolute field different');
  1496. error_found:=true;
  1497. end;
  1498. if oldp^.is_first<>p^.is_first then
  1499. begin
  1500. comment(v_warning,'is_first field different');
  1501. error_found:=true;
  1502. end;
  1503. end;
  1504. calln :
  1505. {(symtableprocentry : pprocsym;
  1506. symtableproc : psymtable;procdefinition : pprocdef;
  1507. methodpointer : ptree;
  1508. no_check,unit_specific : boolean);}
  1509. begin
  1510. if oldp^.symtableprocentry<>p^.symtableprocentry then
  1511. begin
  1512. comment(v_warning,'symtableprocentry field different');
  1513. error_found:=true;
  1514. end;
  1515. if oldp^.symtableproc<>p^.symtableproc then
  1516. begin
  1517. comment(v_warning,'symtableproc field different');
  1518. error_found:=true;
  1519. end;
  1520. if oldp^.procdefinition<>p^.procdefinition then
  1521. begin
  1522. comment(v_warning,'procdefinition field different');
  1523. error_found:=true;
  1524. end;
  1525. if oldp^.methodpointer<>p^.methodpointer then
  1526. begin
  1527. comment(v_warning,'methodpointer field different');
  1528. error_found:=true;
  1529. end;
  1530. if oldp^.no_check<>p^.no_check then
  1531. begin
  1532. comment(v_warning,'no_check field different');
  1533. error_found:=true;
  1534. end;
  1535. if oldp^.unit_specific<>p^.unit_specific then
  1536. begin
  1537. error_found:=true;
  1538. comment(v_warning,'unit_specific field different');
  1539. end;
  1540. end;
  1541. ordconstn :
  1542. begin
  1543. if oldp^.value<>p^.value then
  1544. begin
  1545. comment(v_warning,'value field different');
  1546. error_found:=true;
  1547. end;
  1548. end;
  1549. realconstn :
  1550. begin
  1551. if oldp^.value_real<>p^.value_real then
  1552. begin
  1553. comment(v_warning,'valued field different');
  1554. error_found:=true;
  1555. end;
  1556. if oldp^.lab_real<>p^.lab_real then
  1557. begin
  1558. comment(v_warning,'labnumber field different');
  1559. error_found:=true;
  1560. end;
  1561. end;
  1562. end;
  1563. if not error_found then
  1564. comment(v_warning,'did not find difference in trees');
  1565. end;
  1566. {$endif extdebug}
  1567. function equal_trees(t1,t2 : ptree) : boolean;
  1568. begin
  1569. if t1^.treetype=t2^.treetype then
  1570. begin
  1571. case t1^.treetype of
  1572. addn,
  1573. muln,
  1574. equaln,
  1575. orn,
  1576. xorn,
  1577. andn,
  1578. unequaln:
  1579. begin
  1580. equal_trees:=(equal_trees(t1^.left,t2^.left) and
  1581. equal_trees(t1^.right,t2^.right)) or
  1582. (equal_trees(t1^.right,t2^.left) and
  1583. equal_trees(t1^.left,t2^.right));
  1584. end;
  1585. subn,
  1586. divn,
  1587. modn,
  1588. assignn,
  1589. ltn,
  1590. lten,
  1591. gtn,
  1592. gten,
  1593. inn,
  1594. shrn,
  1595. shln,
  1596. slashn,
  1597. rangen:
  1598. begin
  1599. equal_trees:=(equal_trees(t1^.left,t2^.left) and
  1600. equal_trees(t1^.right,t2^.right));
  1601. end;
  1602. umminusn,
  1603. notn,
  1604. derefn,
  1605. addrn:
  1606. begin
  1607. equal_trees:=(equal_trees(t1^.left,t2^.left));
  1608. end;
  1609. loadn:
  1610. begin
  1611. equal_trees:=(t1^.symtableentry=t2^.symtableentry)
  1612. { not necessary
  1613. and (t1^.symtable=t2^.symtable)};
  1614. end;
  1615. {
  1616. subscriptn,
  1617. ordconstn,typeconvn,calln,callparan,
  1618. realconstn,asmn,vecn,
  1619. stringconstn,funcretn,selfn,
  1620. inlinen,niln,errorn,
  1621. typen,hnewn,hdisposen,newn,
  1622. disposen,setelen,setconstrn
  1623. }
  1624. else equal_trees:=false;
  1625. end;
  1626. end
  1627. else
  1628. equal_trees:=false;
  1629. end;
  1630. procedure set_unique(p : ptree);
  1631. begin
  1632. if assigned(p) then
  1633. begin
  1634. case p^.treetype of
  1635. vecn:
  1636. p^.callunique:=true;
  1637. typeconvn:
  1638. set_unique(p^.left);
  1639. end;
  1640. end;
  1641. end;
  1642. function get_ordinal_value(p : ptree) : longint;
  1643. begin
  1644. if p^.treetype=ordconstn then
  1645. get_ordinal_value:=p^.value
  1646. else
  1647. Message(type_e_ordinal_expr_expected);
  1648. end;
  1649. function is_constnode(p : ptree) : boolean;
  1650. begin
  1651. is_constnode:=(p^.treetype in [ordconstn,realconstn,stringconstn,fixconstn,setconstn]);
  1652. end;
  1653. function is_constintnode(p : ptree) : boolean;
  1654. begin
  1655. is_constintnode:=(p^.treetype=ordconstn) and is_integer(p^.resulttype);
  1656. end;
  1657. function is_constcharnode(p : ptree) : boolean;
  1658. begin
  1659. is_constcharnode:=((p^.treetype=ordconstn) and
  1660. (p^.resulttype^.deftype=orddef) and
  1661. (porddef(p^.resulttype)^.typ=uchar));
  1662. end;
  1663. function is_constrealnode(p : ptree) : boolean;
  1664. begin
  1665. is_constrealnode:=(p^.treetype=realconstn);
  1666. end;
  1667. function is_constboolnode(p : ptree) : boolean;
  1668. begin
  1669. is_constboolnode:=((p^.treetype=ordconstn) and
  1670. (p^.resulttype^.deftype=orddef) and
  1671. (porddef(p^.resulttype)^.typ in [bool8bit,bool16bit,bool32bit]));
  1672. end;
  1673. function str_length(p : ptree) : longint;
  1674. begin
  1675. str_length:=p^.length;
  1676. end;
  1677. function is_emptyset(p : ptree):boolean;
  1678. {
  1679. return true if set s is empty
  1680. }
  1681. var
  1682. i : longint;
  1683. begin
  1684. i:=0;
  1685. if p^.treetype=setconstn then
  1686. begin
  1687. while (i<32) and (p^.value_set^[i]=0) do
  1688. inc(i);
  1689. end;
  1690. is_emptyset:=(i=32);
  1691. end;
  1692. end.
  1693. {
  1694. $Log$
  1695. Revision 1.11 1999-08-04 00:23:59 florian
  1696. * renamed i386asm and i386base to cpuasm and cpubase
  1697. Revision 1.10 1999/08/02 17:14:12 florian
  1698. + changed the temp. generator to an object
  1699. Revision 1.9 1999/08/01 23:19:58 florian
  1700. + make a new makefile using the old compiler makefile
  1701. Revision 1.8 1999/08/01 23:04:52 michael
  1702. + Changes for Alpha
  1703. Revision 1.7 1999/08/01 18:22:39 florian
  1704. * made it again compilable
  1705. Revision 1.6 1999/01/24 22:32:36 florian
  1706. * well, more changes, especially parts of secondload ported
  1707. Revision 1.5 1999/01/23 23:29:49 florian
  1708. * first running version of the new code generator
  1709. * when compiling exceptions under Linux fixed
  1710. Revision 1.4 1999/01/19 10:19:06 florian
  1711. * bug with mul. of dwords fixed, reported by Alexander Stohr
  1712. * some changes to compile with TP
  1713. + small enhancements for the new code generator
  1714. Revision 1.3 1999/01/13 22:52:40 florian
  1715. + YES, finally the new code generator is compilable, but it doesn't run yet :(
  1716. Revision 1.2 1998/12/26 15:20:32 florian
  1717. + more changes for the new version
  1718. Revision 1.1 1998/12/15 22:21:53 florian
  1719. * first rough conversion
  1720. }