tree.pas 66 KB

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