tree.pas 65 KB

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