ncal.pas 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248
  1. {
  2. This file implements the node for sub procedure calling.
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. globtype,
  23. paramgr,parabase,
  24. node,nbas,nutils,
  25. {$ifdef state_tracking}
  26. nstate,
  27. {$endif state_tracking}
  28. symbase,symtype,symsym,symdef,symtable;
  29. type
  30. tcallnodeflag = (
  31. cnf_typedefset,
  32. cnf_return_value_used,
  33. cnf_inherited,
  34. cnf_anon_inherited,
  35. cnf_new_call,
  36. cnf_dispose_call,
  37. cnf_member_call, { called with implicit methodpointer tree }
  38. cnf_uses_varargs, { varargs are used in the declaration }
  39. cnf_create_failed { exception thrown in constructor -> don't call beforedestruction }
  40. );
  41. tcallnodeflags = set of tcallnodeflag;
  42. tcallnode = class(tbinarynode)
  43. private
  44. { info for inlining }
  45. inlinelocals: TFPObjectList;
  46. { number of parameters passed from the source, this does not include the hidden parameters }
  47. paralength : smallint;
  48. function gen_self_tree_methodpointer:tnode;
  49. function gen_self_tree:tnode;
  50. function gen_vmt_tree:tnode;
  51. procedure bind_parasym;
  52. { function return node, this is used to pass the data for a
  53. ret_in_param return value }
  54. _funcretnode : tnode;
  55. procedure setfuncretnode(const returnnode: tnode);
  56. procedure convert_carg_array_of_const;
  57. procedure order_parameters;
  58. procedure createinlineparas(var createstatement, deletestatement: tstatementnode);
  59. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  60. procedure createlocaltemps(p:TObject;arg:pointer);
  61. function pass1_inline:tnode;
  62. function getfuncretassignment(inlineblock: tblocknode): tnode;
  63. protected
  64. pushedparasize : longint;
  65. public
  66. { the symbol containing the definition of the procedure }
  67. { to call }
  68. symtableprocentry : tprocsym;
  69. symtableprocentryderef : tderef;
  70. { symtable where the entry was found, needed for with support }
  71. symtableproc : TSymtable;
  72. { the definition of the procedure to call }
  73. procdefinition : tabstractprocdef;
  74. procdefinitionderef : tderef;
  75. methodpointerinit,
  76. methodpointerdone : tblocknode;
  77. { tree that contains the pointer to the object for this method }
  78. methodpointer : tnode;
  79. { varargs parasyms }
  80. varargsparas : tvarargsparalist;
  81. { node that specifies where the result should be put for calls }
  82. { that return their result in a parameter }
  83. property funcretnode: tnode read _funcretnode write setfuncretnode;
  84. { separately specified resultdef for some compilerprocs (e.g. }
  85. { you can't have a function with an "array of char" resultdef }
  86. { the RTL) (JM) }
  87. typedef: tdef;
  88. callnodeflags : tcallnodeflags;
  89. { only the processor specific nodes need to override this }
  90. { constructor }
  91. constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);virtual;
  92. constructor create_procvar(l,r:tnode);
  93. constructor createintern(const name: string; params: tnode);
  94. constructor createinternres(const name: string; params: tnode; res:tdef);
  95. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  96. destructor destroy;override;
  97. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  98. procedure ppuwrite(ppufile:tcompilerppufile);override;
  99. procedure derefnode;override;
  100. procedure buildderefimpl;override;
  101. procedure derefimpl;override;
  102. function dogetcopy : tnode;override;
  103. { Goes through all symbols in a class and subclasses and calls
  104. verify abstract for each .
  105. }
  106. procedure verifyabstractcalls;
  107. { called for each definition in a class and verifies if a method
  108. is abstract or not, if it is abstract, give out a warning
  109. }
  110. procedure verifyabstract(sym:TObject;arg:pointer);
  111. procedure insertintolist(l : tnodelist);override;
  112. function pass_1 : tnode;override;
  113. function pass_typecheck:tnode;override;
  114. {$ifdef state_tracking}
  115. function track_state_pass(exec_known:boolean):boolean;override;
  116. {$endif state_tracking}
  117. function docompare(p: tnode): boolean; override;
  118. procedure printnodedata(var t:text);override;
  119. function para_count:longint;
  120. function get_load_methodpointer:tnode;
  121. { checks if there are any parameters which end up at the stack, i.e.
  122. which have LOC_REFERENCE and set pi_has_stackparameter if this applies }
  123. procedure check_stack_parameters;
  124. property parameters : tnode read left write left;
  125. private
  126. AbstractMethodsList : TFPHashList;
  127. end;
  128. tcallnodeclass = class of tcallnode;
  129. tcallparaflag = (
  130. cpf_is_colon_para,
  131. cpf_varargs_para { belongs this para to varargs }
  132. );
  133. tcallparaflags = set of tcallparaflag;
  134. tcallparanode = class(ttertiarynode)
  135. public
  136. callparaflags : tcallparaflags;
  137. parasym : tparavarsym;
  138. used_by_callnode : boolean;
  139. { only the processor specific nodes need to override this }
  140. { constructor }
  141. constructor create(expr,next : tnode);virtual;
  142. destructor destroy;override;
  143. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. function dogetcopy : tnode;override;
  146. procedure insertintolist(l : tnodelist);override;
  147. procedure get_paratype;
  148. procedure insert_typeconv(do_count : boolean);
  149. procedure det_registers;
  150. procedure firstcallparan;
  151. procedure secondcallparan;virtual;abstract;
  152. function docompare(p: tnode): boolean; override;
  153. procedure printnodetree(var t:text);override;
  154. { returns whether a parameter contains a type conversion from }
  155. { a refcounted into a non-refcounted type }
  156. function contains_unsafe_typeconversion: boolean;
  157. property value : tnode read left write left;
  158. property nextpara : tnode read right write right;
  159. property parametername : tnode read third write third;
  160. end;
  161. tcallparanodeclass = class of tcallparanode;
  162. function reverseparameters(p: tcallparanode): tcallparanode;
  163. function translate_disp_call(selfnode,parametersnode : tnode;methodname : ansistring = '';dispid : longint = 0;useresult : boolean = false) : tnode;
  164. var
  165. ccallnode : tcallnodeclass;
  166. ccallparanode : tcallparanodeclass;
  167. { Current callnode, this is needed for having a link
  168. between the callparanodes and the callnode they belong to }
  169. aktcallnode : tcallnode;
  170. implementation
  171. uses
  172. systems,
  173. verbose,globals,
  174. symconst,defutil,defcmp,
  175. htypechk,pass_1,
  176. ncnv,nld,ninl,nadd,ncon,nmem,nset,
  177. procinfo,
  178. cgbase
  179. ;
  180. type
  181. tobjectinfoitem = class(tlinkedlistitem)
  182. objinfo : tobjectdef;
  183. constructor create(def : tobjectdef);
  184. end;
  185. {****************************************************************************
  186. HELPERS
  187. ****************************************************************************}
  188. function reverseparameters(p: tcallparanode): tcallparanode;
  189. var
  190. hp1, hp2: tcallparanode;
  191. begin
  192. hp1:=nil;
  193. while assigned(p) do
  194. begin
  195. { pull out }
  196. hp2:=p;
  197. p:=tcallparanode(p.right);
  198. { pull in }
  199. hp2.right:=hp1;
  200. hp1:=hp2;
  201. end;
  202. reverseparameters:=hp1;
  203. end;
  204. function translate_disp_call(selfnode,parametersnode : tnode;methodname : ansistring = '';dispid : longint = 0;useresult : boolean = false) : tnode;
  205. const
  206. DISPATCH_METHOD = $1;
  207. DISPATCH_PROPERTYGET = $2;
  208. DISPATCH_PROPERTYPUT = $4;
  209. DISPATCH_PROPERTYPUTREF = $8;
  210. DISPATCH_CONSTRUCT = $4000;
  211. var
  212. statements : tstatementnode;
  213. result_data,
  214. params : ttempcreatenode;
  215. paramssize : longint;
  216. calldescnode : tdataconstnode;
  217. resultvalue : tnode;
  218. para : tcallparanode;
  219. currargpos,
  220. namedparacount,
  221. paracount : longint;
  222. assignmenttype,
  223. vardatadef,
  224. pvardatadef : tdef;
  225. dispatchbyref : boolean;
  226. calldesc : packed record
  227. calltype,argcount,namedargcount : byte;
  228. { size of argtypes is unknown at compile time
  229. so this is basically a dummy }
  230. argtypes : array[0..255] of byte;
  231. { argtypes is followed by method name
  232. names of named parameters, each being
  233. a zero terminated string
  234. }
  235. end;
  236. names : ansistring;
  237. dispintfinvoke,
  238. variantdispatch : boolean;
  239. procedure increase_paramssize;
  240. begin
  241. { for now we pass everything by reference
  242. case para.value.resultdef.typ of
  243. variantdef:
  244. inc(paramssize,para.value.resultdef.size);
  245. else
  246. }
  247. inc(paramssize,sizeof(voidpointertype.size ));
  248. {
  249. end;
  250. }
  251. end;
  252. begin
  253. variantdispatch:=selfnode.resultdef.typ=variantdef;
  254. dispintfinvoke:=not(variantdispatch);
  255. result:=internalstatements(statements);
  256. fillchar(calldesc,sizeof(calldesc),0);
  257. if useresult then
  258. begin
  259. { get temp for the result }
  260. result_data:=ctempcreatenode.create(colevarianttype,colevarianttype.size,tt_persistent,true);
  261. addstatement(statements,result_data);
  262. end;
  263. { build parameters }
  264. { first, count and check parameters }
  265. // p2:=reverseparameters(tcallparanode(p2));
  266. para:=tcallparanode(parametersnode);
  267. paracount:=0;
  268. namedparacount:=0;
  269. paramssize:=0;
  270. while assigned(para) do
  271. begin
  272. inc(paracount);
  273. typecheckpass(para.value);
  274. { insert some extra casts }
  275. if is_constintnode(para.value) and not(is_64bitint(para.value.resultdef)) then
  276. begin
  277. para.value:=ctypeconvnode.create_internal(para.value,s32inttype);
  278. typecheckpass(para.value);
  279. end
  280. else if para.value.nodetype=stringconstn then
  281. begin
  282. para.value:=ctypeconvnode.create_internal(para.value,cwidestringtype);
  283. typecheckpass(para.value);
  284. end
  285. { force automatable boolean type }
  286. else if is_boolean(para.value.resultdef) then
  287. begin
  288. para.value:=ctypeconvnode.create_internal(para.value,bool16type);
  289. typecheckpass(para.value);
  290. end
  291. { force automatable float type }
  292. else if is_extended(para.value.resultdef) then
  293. begin
  294. para.value:=ctypeconvnode.create_internal(para.value,s64floattype);
  295. typecheckpass(para.value);
  296. end;
  297. if assigned(para.parametername) then
  298. begin
  299. typecheckpass(para.value);
  300. inc(namedparacount);
  301. end;
  302. if para.value.nodetype<>nothingn then
  303. if not is_automatable(para.value.resultdef) then
  304. CGMessagePos1(para.value.fileinfo,type_e_not_automatable,para.value.resultdef.typename);
  305. { we've to know the parameter size to allocate the temp. space }
  306. increase_paramssize;
  307. para:=tcallparanode(para.nextpara);
  308. end;
  309. calldesc.calltype:=DISPATCH_METHOD;
  310. calldesc.argcount:=paracount;
  311. { allocate space }
  312. params:=ctempcreatenode.create(voidtype,paramssize,tt_persistent,true);
  313. addstatement(statements,params);
  314. calldescnode:=cdataconstnode.create;
  315. if dispintfinvoke then
  316. calldescnode.append(dispid,sizeof(dispid));
  317. { build up parameters and description }
  318. para:=tcallparanode(parametersnode);
  319. currargpos:=0;
  320. paramssize:=0;
  321. names := '';
  322. while assigned(para) do
  323. begin
  324. if assigned(para.parametername) then
  325. begin
  326. if para.parametername.nodetype=stringconstn then
  327. names:=names+tstringconstnode(para.parametername).value_str+#0
  328. else
  329. internalerror(200611041);
  330. end;
  331. dispatchbyref:=para.value.resultdef.typ in [variantdef];
  332. { assign the argument/parameter to the temporary location }
  333. if para.value.nodetype<>nothingn then
  334. if dispatchbyref then
  335. addstatement(statements,cassignmentnode.create(
  336. ctypeconvnode.create_internal(cderefnode.create(caddnode.create(addn,
  337. caddrnode.create(ctemprefnode.create(params)),
  338. cordconstnode.create(paramssize,ptruinttype,false)
  339. )),voidpointertype),
  340. ctypeconvnode.create_internal(caddrnode.create_internal(para.value),voidpointertype)))
  341. else
  342. begin
  343. case para.value.resultdef.size of
  344. 1..4:
  345. assignmenttype:=u32inttype;
  346. 8:
  347. assignmenttype:=u64inttype;
  348. else
  349. internalerror(2007042801);
  350. end;
  351. addstatement(statements,cassignmentnode.create(
  352. ctypeconvnode.create_internal(cderefnode.create(caddnode.create(addn,
  353. caddrnode.create(ctemprefnode.create(params)),
  354. cordconstnode.create(paramssize,ptruinttype,false)
  355. )),assignmenttype),
  356. ctypeconvnode.create_internal(para.value,assignmenttype)));
  357. end;
  358. if is_ansistring(para.value.resultdef) then
  359. calldesc.argtypes[currargpos]:=varStrArg
  360. else
  361. calldesc.argtypes[currargpos]:=para.value.resultdef.getvardef;
  362. if dispatchbyref then
  363. calldesc.argtypes[currargpos]:=calldesc.argtypes[currargpos] or $80;
  364. increase_paramssize;
  365. para.value:=nil;
  366. inc(currargpos);
  367. para:=tcallparanode(para.nextpara);
  368. end;
  369. // typecheckpass(statements);
  370. // printnode(output,statements);
  371. { old argument list skeleton isn't needed anymore }
  372. parametersnode.free;
  373. calldescnode.append(calldesc,3+calldesc.argcount);
  374. pvardatadef:=tpointerdef(search_system_type('PVARDATA').typedef);
  375. if useresult then
  376. resultvalue:=caddrnode.create(ctemprefnode.create(result_data))
  377. else
  378. resultvalue:=cpointerconstnode.create(0,voidpointertype);
  379. if variantdispatch then
  380. begin
  381. methodname:=methodname+#0;
  382. calldescnode.append(pointer(methodname)^,length(methodname));
  383. calldescnode.append(pointer(names)^,length(names));
  384. { actual call }
  385. vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);
  386. addstatement(statements,ccallnode.createintern('fpc_dispinvoke_variant',
  387. { parameters are passed always reverted, i.e. the last comes first }
  388. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  389. ccallparanode.create(caddrnode.create(calldescnode),
  390. ccallparanode.create(ctypeconvnode.create_internal(selfnode,vardatadef),
  391. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  392. );
  393. end
  394. else
  395. begin
  396. addstatement(statements,ccallnode.createintern('fpc_dispatch_by_id',
  397. { parameters are passed always reverted, i.e. the last comes first }
  398. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  399. ccallparanode.create(caddrnode.create(calldescnode),
  400. ccallparanode.create(ctypeconvnode.create_internal(selfnode,voidpointertype),
  401. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  402. );
  403. end;
  404. if useresult then
  405. begin
  406. { clean up }
  407. addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
  408. addstatement(statements,ctemprefnode.create(result_data));
  409. end;
  410. end;
  411. procedure maybe_load_para_in_temp(var p:tnode);
  412. function is_simple_node(hp:tnode):boolean;
  413. begin
  414. is_simple_node:=(hp.nodetype in [typen,loadvmtaddrn,loadn,arrayconstructorn]);
  415. end;
  416. var
  417. hp,
  418. loadp,
  419. refp : tnode;
  420. hdef : tdef;
  421. ptemp : ttempcreatenode;
  422. usederef : boolean;
  423. usevoidpointer : boolean;
  424. newinitstatement,
  425. newdonestatement : tstatementnode;
  426. begin
  427. if not assigned(aktcallnode) then
  428. internalerror(200410121);
  429. { Load all complex loads into a temp to prevent
  430. double calls to a function. We can't simply check for a hp.nodetype=calln
  431. }
  432. hp:=p;
  433. while assigned(hp) and
  434. (hp.nodetype=typeconvn) and
  435. (ttypeconvnode(hp).convtype=tc_equal) do
  436. hp:=tunarynode(hp).left;
  437. if assigned(hp) and
  438. not is_simple_node(hp) then
  439. begin
  440. if not assigned(aktcallnode.methodpointerinit) then
  441. begin
  442. aktcallnode.methodpointerinit:=internalstatements(newinitstatement);
  443. aktcallnode.methodpointerdone:=internalstatements(newdonestatement);
  444. end
  445. else
  446. begin
  447. newinitstatement:=laststatement(aktcallnode.methodpointerinit);
  448. newdonestatement:=laststatement(aktcallnode.methodpointerdone);
  449. end;
  450. { temp create }
  451. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  452. is_shortstring(p.resultdef) or
  453. is_object(p.resultdef);
  454. { avoid refcount increase }
  455. usevoidpointer:=is_interface(p.resultdef);
  456. if usederef then
  457. hdef:=tpointerdef.create(p.resultdef)
  458. else
  459. hdef:=p.resultdef;
  460. if usevoidpointer then
  461. begin
  462. ptemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  463. loadp := ctypeconvnode.create_internal(p,voidpointertype);
  464. refp:=ctypeconvnode.create_internal(ctemprefnode.create(ptemp),hdef);
  465. end
  466. else
  467. begin
  468. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  469. if usederef then
  470. begin
  471. loadp:=caddrnode.create_internal(p);
  472. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  473. end
  474. else
  475. begin
  476. loadp:=p;
  477. refp:=ctemprefnode.create(ptemp)
  478. end
  479. end;
  480. addstatement(newinitstatement,ptemp);
  481. addstatement(newinitstatement,cassignmentnode.create(
  482. ctemprefnode.create(ptemp),
  483. loadp));
  484. { new tree is only a temp reference }
  485. p:=refp;
  486. { temp release. We need to return a reference to the methodpointer
  487. otherwise the conversion from callnode to loadnode can't be done
  488. for the methodpointer unless the loadnode will also get a methodpointerinit and
  489. methodpointerdone node. For the moment we use register as temp and therefor
  490. don't create a temp-leak in the stackframe (PFV) }
  491. { the last statement should return the value as
  492. location and type, this is done be referencing the
  493. temp and converting it first from a persistent temp to
  494. normal temp }
  495. addstatement(newdonestatement,ctempdeletenode.create_normal_temp(ptemp));
  496. if usevoidpointer then
  497. addstatement(newdonestatement,ctypeconvnode.create_internal(
  498. ctemprefnode.create(ptemp),hdef))
  499. else
  500. addstatement(newdonestatement,ctemprefnode.create(ptemp));
  501. { call typecheckpass for new nodes }
  502. typecheckpass(p);
  503. typecheckpass(aktcallnode.methodpointerinit);
  504. typecheckpass(aktcallnode.methodpointerdone);
  505. end;
  506. end;
  507. function gen_high_tree(var p:tnode;paradef:tdef):tnode;
  508. {When passing an array to an open array, or a string to an open string,
  509. some code is needed that generates the high bound of the array. This
  510. function returns a tree containing the nodes for it.}
  511. var
  512. temp: tnode;
  513. len : integer;
  514. loadconst : boolean;
  515. hightree,l,r : tnode;
  516. begin
  517. len:=-1;
  518. loadconst:=true;
  519. hightree:=nil;
  520. case p.resultdef.typ of
  521. arraydef :
  522. begin
  523. if (paradef.typ<>arraydef) then
  524. internalerror(200405241);
  525. { passing a string to an array of char }
  526. if (p.nodetype=stringconstn) and
  527. is_char(tarraydef(paradef).elementdef) then
  528. begin
  529. len:=tstringconstnode(p).len;
  530. if len>0 then
  531. dec(len);
  532. end
  533. else
  534. { handle special case of passing an single array to an array of array }
  535. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  536. len:=0
  537. else
  538. begin
  539. { handle via a normal inline in_high_x node }
  540. loadconst:=false;
  541. { slice? }
  542. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  543. with Tcallparanode(Tinlinenode(p).left) do
  544. begin
  545. {Array slice using slice builtin function.}
  546. l:=Tcallparanode(right).left;
  547. hightree:=caddnode.create(subn,l,genintconstnode(1));
  548. Tcallparanode(right).left:=nil;
  549. {Remove the inline node.}
  550. temp:=p;
  551. p:=left;
  552. Tcallparanode(tinlinenode(temp).left).left:=nil;
  553. temp.free;
  554. typecheckpass(hightree);
  555. end
  556. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  557. begin
  558. {Array slice using .. operator.}
  559. with Trangenode(Tvecnode(p).right) do
  560. begin
  561. l:=left; {Get lower bound.}
  562. r:=right; {Get upper bound.}
  563. end;
  564. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  565. hightree:=caddnode.create(subn,r,l);
  566. {Replace the rangnode in the tree by its lower_bound, and
  567. dispose the rangenode.}
  568. temp:=Tvecnode(p).right;
  569. Tvecnode(p).right:=l.getcopy;
  570. {Typecheckpass can only be performed *after* the l.getcopy since it
  571. can modify the tree, and l is in the hightree.}
  572. typecheckpass(hightree);
  573. with Trangenode(temp) do
  574. begin
  575. left:=nil;
  576. right:=nil;
  577. end;
  578. temp.free;
  579. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  580. p.resultdef:=nil;
  581. typecheckpass(p);
  582. end
  583. else
  584. begin
  585. maybe_load_para_in_temp(p);
  586. hightree:=geninlinenode(in_high_x,false,p.getcopy);
  587. typecheckpass(hightree);
  588. { only substract low(array) if it's <> 0 }
  589. temp:=geninlinenode(in_low_x,false,p.getcopy);
  590. typecheckpass(temp);
  591. if (temp.nodetype <> ordconstn) or
  592. (tordconstnode(temp).value <> 0) then
  593. hightree := caddnode.create(subn,hightree,temp)
  594. else
  595. temp.free;
  596. end;
  597. end;
  598. end;
  599. stringdef :
  600. begin
  601. if is_open_string(paradef) then
  602. begin
  603. maybe_load_para_in_temp(p);
  604. { handle via a normal inline in_high_x node }
  605. loadconst := false;
  606. hightree := geninlinenode(in_high_x,false,p.getcopy);
  607. end
  608. else
  609. { handle special case of passing an single string to an array of string }
  610. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  611. len:=0
  612. else
  613. { passing a string to an array of char }
  614. if (p.nodetype=stringconstn) and
  615. is_char(tarraydef(paradef).elementdef) then
  616. begin
  617. len:=tstringconstnode(p).len;
  618. if len>0 then
  619. dec(len);
  620. end
  621. else
  622. begin
  623. maybe_load_para_in_temp(p);
  624. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  625. cordconstnode.create(1,sinttype,false));
  626. loadconst:=false;
  627. end;
  628. end;
  629. else
  630. len:=0;
  631. end;
  632. if loadconst then
  633. hightree:=cordconstnode.create(len,sinttype,true)
  634. else
  635. begin
  636. if not assigned(hightree) then
  637. internalerror(200304071);
  638. { Need to use explicit, because it can also be a enum }
  639. hightree:=ctypeconvnode.create_internal(hightree,sinttype);
  640. end;
  641. result:=hightree;
  642. end;
  643. {****************************************************************************
  644. TOBJECTINFOITEM
  645. ****************************************************************************}
  646. constructor tobjectinfoitem.create(def : tobjectdef);
  647. begin
  648. inherited create;
  649. objinfo := def;
  650. end;
  651. {****************************************************************************
  652. TCALLPARANODE
  653. ****************************************************************************}
  654. constructor tcallparanode.create(expr,next : tnode);
  655. begin
  656. inherited create(callparan,expr,next,nil);
  657. if not assigned(expr) then
  658. internalerror(200305091);
  659. expr.fileinfo:=fileinfo;
  660. callparaflags:=[];
  661. end;
  662. destructor tcallparanode.destroy;
  663. begin
  664. { When the node is used by callnode then
  665. we don't destroy left, the callnode takes care of it }
  666. if used_by_callnode then
  667. left:=nil;
  668. inherited destroy;
  669. end;
  670. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  671. begin
  672. inherited ppuload(t,ppufile);
  673. ppufile.getsmallset(callparaflags);
  674. end;
  675. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  676. begin
  677. inherited ppuwrite(ppufile);
  678. ppufile.putsmallset(callparaflags);
  679. end;
  680. function tcallparanode.dogetcopy : tnode;
  681. var
  682. n : tcallparanode;
  683. begin
  684. n:=tcallparanode(inherited dogetcopy);
  685. n.callparaflags:=callparaflags;
  686. n.parasym:=parasym;
  687. result:=n;
  688. end;
  689. procedure tcallparanode.insertintolist(l : tnodelist);
  690. begin
  691. end;
  692. procedure tcallparanode.get_paratype;
  693. var
  694. old_array_constructor : boolean;
  695. begin
  696. inc(parsing_para_level);
  697. if assigned(right) then
  698. tcallparanode(right).get_paratype;
  699. old_array_constructor:=allow_array_constructor;
  700. allow_array_constructor:=true;
  701. typecheckpass(left);
  702. allow_array_constructor:=old_array_constructor;
  703. if codegenerror then
  704. resultdef:=generrordef
  705. else
  706. resultdef:=left.resultdef;
  707. dec(parsing_para_level);
  708. end;
  709. procedure tcallparanode.insert_typeconv(do_count : boolean);
  710. var
  711. olddef : tdef;
  712. hp : tnode;
  713. block : tblocknode;
  714. statements : tstatementnode;
  715. temp : ttempcreatenode;
  716. {$ifdef extdebug}
  717. store_count_ref : boolean;
  718. {$endif def extdebug}
  719. begin
  720. inc(parsing_para_level);
  721. {$ifdef extdebug}
  722. if do_count then
  723. begin
  724. store_count_ref:=count_ref;
  725. count_ref:=true;
  726. end;
  727. {$endif def extdebug}
  728. { Be sure to have the resultdef }
  729. if not assigned(left.resultdef) then
  730. typecheckpass(left);
  731. if (left.nodetype<>nothingn) then
  732. begin
  733. { Convert tp procvars, this is needs to be done
  734. here to make the change permanent. in the overload
  735. choosing the changes are only made temporary }
  736. if (left.resultdef.typ=procvardef) and
  737. (parasym.vardef.typ<>procvardef) then
  738. begin
  739. if maybe_call_procvar(left,true) then
  740. resultdef:=left.resultdef;
  741. end;
  742. { Remove implicitly inserted typecast to pointer for
  743. @procvar in macpas }
  744. if (m_mac_procvar in current_settings.modeswitches) and
  745. (parasym.vardef.typ=procvardef) and
  746. (left.nodetype=typeconvn) and
  747. is_voidpointer(left.resultdef) and
  748. (ttypeconvnode(left).left.nodetype=typeconvn) and
  749. (ttypeconvnode(ttypeconvnode(left).left).convtype=tc_proc_2_procvar) then
  750. begin
  751. hp:=left;
  752. left:=ttypeconvnode(left).left;
  753. ttypeconvnode(hp).left:=nil;
  754. hp.free;
  755. end;
  756. { Handle varargs and hidden paras directly, no typeconvs or }
  757. { pass_typechecking needed }
  758. if (cpf_varargs_para in callparaflags) then
  759. begin
  760. { this should only happen vor C varargs }
  761. { the necessary conversions have already been performed in }
  762. { tarrayconstructornode.insert_typeconvs }
  763. set_varstate(left,vs_read,[vsf_must_be_valid]);
  764. insert_varargstypeconv(left,true);
  765. resultdef:=left.resultdef;
  766. { also update parasym type to get the correct parameter location
  767. for the new types }
  768. parasym.vardef:=left.resultdef;
  769. end
  770. else
  771. if (vo_is_hidden_para in parasym.varoptions) then
  772. begin
  773. set_varstate(left,vs_read,[vsf_must_be_valid]);
  774. resultdef:=left.resultdef;
  775. end
  776. else
  777. begin
  778. { Do we need arrayconstructor -> set conversion, then insert
  779. it here before the arrayconstructor node breaks the tree
  780. with its conversions of enum->ord }
  781. if (left.nodetype=arrayconstructorn) and
  782. (parasym.vardef.typ=setdef) then
  783. inserttypeconv(left,parasym.vardef);
  784. { set some settings needed for arrayconstructor }
  785. if is_array_constructor(left.resultdef) then
  786. begin
  787. if left.nodetype<>arrayconstructorn then
  788. internalerror(200504041);
  789. if is_array_of_const(parasym.vardef) then
  790. begin
  791. { force variant array }
  792. include(left.flags,nf_forcevaria);
  793. end
  794. else
  795. begin
  796. include(left.flags,nf_novariaallowed);
  797. { now that the resultting type is know we can insert the required
  798. typeconvs for the array constructor }
  799. if parasym.vardef.typ=arraydef then
  800. tarrayconstructornode(left).force_type(tarraydef(parasym.vardef).elementdef);
  801. end;
  802. end;
  803. { check if local proc/func is assigned to procvar }
  804. if left.resultdef.typ=procvardef then
  805. test_local_to_procvar(tprocvardef(left.resultdef),parasym.vardef);
  806. { test conversions }
  807. if not(is_shortstring(left.resultdef) and
  808. is_shortstring(parasym.vardef)) and
  809. (parasym.vardef.typ<>formaldef) then
  810. begin
  811. { Process open parameters }
  812. if paramanager.push_high_param(parasym.varspez,parasym.vardef,aktcallnode.procdefinition.proccalloption) then
  813. begin
  814. { insert type conv but hold the ranges of the array }
  815. olddef:=left.resultdef;
  816. inserttypeconv(left,parasym.vardef);
  817. left.resultdef:=olddef;
  818. end
  819. else
  820. begin
  821. check_ranges(left.fileinfo,left,parasym.vardef);
  822. inserttypeconv(left,parasym.vardef);
  823. end;
  824. if codegenerror then
  825. begin
  826. dec(parsing_para_level);
  827. exit;
  828. end;
  829. end;
  830. { truncate shortstring value parameters at the caller side if }
  831. { they are passed by value (if passed by reference, then the }
  832. { callee will truncate when copying in the string) }
  833. { This happens e.g. on x86_64 for small strings }
  834. if is_shortstring(left.resultdef) and
  835. is_shortstring(parasym.vardef) and
  836. (parasym.varspez=vs_value) and
  837. not paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  838. aktcallnode.procdefinition.proccalloption) and
  839. ((is_open_string(left.resultdef) and
  840. (tstringdef(parasym.vardef).len < 255)) or
  841. (not is_open_string(left.resultdef) and
  842. { when a stringconstn is typeconverted, then only its }
  843. { def is modified, not the contents (needed because in }
  844. { Delphi/TP, if you pass a longer string to a const }
  845. { parameter, then the callee has to see this longer }
  846. { string) }
  847. (((left.nodetype<>stringconstn) and
  848. (tstringdef(parasym.vardef).len<tstringdef(left.resultdef).len)) or
  849. ((left.nodetype=stringconstn) and
  850. (tstringdef(parasym.vardef).len<tstringconstnode(left).len))))) then
  851. begin
  852. block:=internalstatements(statements);
  853. { temp for the new string }
  854. temp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,
  855. tt_persistent,true);
  856. addstatement(statements,temp);
  857. { assign parameter to temp }
  858. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  859. left:=nil;
  860. { release temp after next use }
  861. addstatement(statements,ctempdeletenode.create_normal_temp(temp));
  862. addstatement(statements,ctemprefnode.create(temp));
  863. typecheckpass(block);
  864. left:=block;
  865. end;
  866. { check var strings }
  867. if (cs_strict_var_strings in current_settings.localswitches) and
  868. is_shortstring(left.resultdef) and
  869. is_shortstring(parasym.vardef) and
  870. (parasym.varspez in [vs_out,vs_var]) and
  871. not(is_open_string(parasym.vardef)) and
  872. not(equal_defs(left.resultdef,parasym.vardef)) then
  873. begin
  874. current_filepos:=left.fileinfo;
  875. CGMessage(type_e_strict_var_string_violation);
  876. end;
  877. { Handle formal parameters separate }
  878. if (parasym.vardef.typ=formaldef) then
  879. begin
  880. { load procvar if a procedure is passed }
  881. if ((m_tp_procvar in current_settings.modeswitches) or
  882. (m_mac_procvar in current_settings.modeswitches)) and
  883. (left.nodetype=calln) and
  884. (is_void(left.resultdef)) then
  885. load_procvar_from_calln(left);
  886. case parasym.varspez of
  887. vs_var,
  888. vs_out :
  889. begin
  890. if not valid_for_formal_var(left,true) then
  891. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  892. end;
  893. vs_const :
  894. begin
  895. if not valid_for_formal_const(left,true) then
  896. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  897. end;
  898. end;
  899. end
  900. else
  901. begin
  902. { check if the argument is allowed }
  903. if (parasym.varspez in [vs_out,vs_var]) then
  904. valid_for_var(left,true);
  905. end;
  906. if parasym.varspez in [vs_var,vs_out] then
  907. set_unique(left);
  908. { When the address needs to be pushed then the register is
  909. not regable. Exception is when the location is also a var
  910. parameter and we can pass the address transparently (but
  911. that is handled by make_not_regable if ra_addr_regable is
  912. passed, and make_not_regable always needs to called for
  913. the ra_addr_taken info for non-invisble parameters }
  914. if (
  915. not(
  916. (vo_is_hidden_para in parasym.varoptions) and
  917. (left.resultdef.typ in [pointerdef,classrefdef])
  918. ) and
  919. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  920. aktcallnode.procdefinition.proccalloption)
  921. ) then
  922. { pushing the address of a variable to take the place of a temp }
  923. { as the complex function result of a function does not make its }
  924. { address escape the current block, as the "address of the }
  925. { function result" is not something which can be stored }
  926. { persistently by the callee (it becomes invalid when the callee }
  927. { returns) }
  928. if not(vo_is_funcret in parasym.varoptions) then
  929. make_not_regable(left,[ra_addr_regable,ra_addr_taken])
  930. else
  931. make_not_regable(left,[ra_addr_regable]);
  932. if do_count then
  933. begin
  934. case parasym.varspez of
  935. vs_out :
  936. begin
  937. { first set written separately to avoid false }
  938. { uninitialized warnings (tbs/tb0542) }
  939. set_varstate(left,vs_written,[]);
  940. set_varstate(left,vs_readwritten,[]);
  941. end;
  942. vs_var :
  943. set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
  944. else
  945. set_varstate(left,vs_read,[vsf_must_be_valid]);
  946. end;
  947. end;
  948. { must only be done after typeconv PM }
  949. resultdef:=parasym.vardef;
  950. end;
  951. end;
  952. { process next node }
  953. if assigned(right) then
  954. tcallparanode(right).insert_typeconv(do_count);
  955. dec(parsing_para_level);
  956. {$ifdef extdebug}
  957. if do_count then
  958. count_ref:=store_count_ref;
  959. {$endif def extdebug}
  960. end;
  961. procedure tcallparanode.det_registers;
  962. begin
  963. if assigned(right) then
  964. begin
  965. tcallparanode(right).det_registers;
  966. registersint:=right.registersint;
  967. registersfpu:=right.registersfpu;
  968. {$ifdef SUPPORT_MMX}
  969. registersmmx:=right.registersmmx;
  970. {$endif}
  971. end;
  972. firstpass(left);
  973. if left.registersint>registersint then
  974. registersint:=left.registersint;
  975. if left.registersfpu>registersfpu then
  976. registersfpu:=left.registersfpu;
  977. {$ifdef SUPPORT_MMX}
  978. if left.registersmmx>registersmmx then
  979. registersmmx:=left.registersmmx;
  980. {$endif SUPPORT_MMX}
  981. end;
  982. function tcallparanode.contains_unsafe_typeconversion: boolean;
  983. var
  984. n: tnode;
  985. begin
  986. n:=left;
  987. while assigned(n) and
  988. (n.nodetype=typeconvn) do
  989. begin
  990. { look for type conversion nodes which convert a }
  991. { refcounted type into a non-refcounted type }
  992. if (not n.resultdef.needs_inittable or
  993. is_class(n.resultdef)) and
  994. (ttypeconvnode(n).left.resultdef.needs_inittable and
  995. not is_class(ttypeconvnode(n).left.resultdef)) then
  996. begin
  997. result:=true;
  998. exit;
  999. end;
  1000. n:=ttypeconvnode(n).left;
  1001. end;
  1002. result:=false;
  1003. end;
  1004. procedure tcallparanode.firstcallparan;
  1005. begin
  1006. if not assigned(left.resultdef) then
  1007. get_paratype;
  1008. det_registers;
  1009. end;
  1010. function tcallparanode.docompare(p: tnode): boolean;
  1011. begin
  1012. docompare :=
  1013. inherited docompare(p) and
  1014. (callparaflags = tcallparanode(p).callparaflags)
  1015. ;
  1016. end;
  1017. procedure tcallparanode.printnodetree(var t:text);
  1018. begin
  1019. printnodelist(t);
  1020. end;
  1021. {****************************************************************************
  1022. TCALLNODE
  1023. ****************************************************************************}
  1024. constructor tcallnode.create(l:tnode;v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);
  1025. begin
  1026. inherited create(calln,l,nil);
  1027. symtableprocentry:=v;
  1028. symtableproc:=st;
  1029. callnodeflags:=callflags+[cnf_return_value_used];
  1030. methodpointer:=mp;
  1031. methodpointerinit:=nil;
  1032. methodpointerdone:=nil;
  1033. procdefinition:=nil;
  1034. _funcretnode:=nil;
  1035. paralength:=-1;
  1036. varargsparas:=nil;
  1037. end;
  1038. constructor tcallnode.create_procvar(l,r:tnode);
  1039. begin
  1040. inherited create(calln,l,r);
  1041. symtableprocentry:=nil;
  1042. symtableproc:=nil;
  1043. methodpointer:=nil;
  1044. methodpointerinit:=nil;
  1045. methodpointerdone:=nil;
  1046. procdefinition:=nil;
  1047. callnodeflags:=[cnf_return_value_used];
  1048. _funcretnode:=nil;
  1049. paralength:=-1;
  1050. varargsparas:=nil;
  1051. end;
  1052. constructor tcallnode.createintern(const name: string; params: tnode);
  1053. var
  1054. srsym: tsym;
  1055. begin
  1056. srsym := tsym(systemunit.Find(name));
  1057. if not assigned(srsym) and
  1058. (cs_compilesystem in current_settings.moduleswitches) then
  1059. srsym := tsym(systemunit.Find(upper(name)));
  1060. if not assigned(srsym) or
  1061. (srsym.typ<>procsym) then
  1062. Message1(cg_f_unknown_compilerproc,name);
  1063. create(params,tprocsym(srsym),srsym.owner,nil,[]);
  1064. end;
  1065. constructor tcallnode.createinternres(const name: string; params: tnode; res:tdef);
  1066. var
  1067. pd : tprocdef;
  1068. begin
  1069. createintern(name,params);
  1070. typedef := res;
  1071. include(callnodeflags,cnf_typedefset);
  1072. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1073. { both the normal and specified resultdef either have to be returned via a }
  1074. { parameter or not, but no mixing (JM) }
  1075. if paramanager.ret_in_param(typedef,pd.proccalloption) xor
  1076. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  1077. internalerror(200108291);
  1078. end;
  1079. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  1080. var
  1081. pd : tprocdef;
  1082. begin
  1083. createintern(name,params);
  1084. _funcretnode:=returnnode;
  1085. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1086. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  1087. internalerror(200204247);
  1088. end;
  1089. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  1090. var
  1091. para: tcallparanode;
  1092. begin
  1093. if assigned(_funcretnode) then
  1094. _funcretnode.free;
  1095. _funcretnode := returnnode;
  1096. { if the resultdef pass hasn't occurred yet, that one will do }
  1097. { everything }
  1098. if assigned(resultdef) then
  1099. begin
  1100. { these are returned as values, but we can optimize their loading }
  1101. { as well }
  1102. if is_ansistring(resultdef) or
  1103. is_widestring(resultdef) then
  1104. exit;
  1105. para := tcallparanode(left);
  1106. while assigned(para) do
  1107. begin
  1108. if (vo_is_hidden_para in para.parasym.varoptions) and
  1109. (vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  1110. begin
  1111. para.left.free;
  1112. para.left := _funcretnode.getcopy;
  1113. exit;
  1114. end;
  1115. para := tcallparanode(para.right);
  1116. end;
  1117. { no hidden resultpara found, error! }
  1118. if not(po_inline in procdefinition.procoptions) then
  1119. internalerror(200306087);
  1120. end;
  1121. end;
  1122. destructor tcallnode.destroy;
  1123. begin
  1124. methodpointer.free;
  1125. methodpointerinit.free;
  1126. methodpointerdone.free;
  1127. _funcretnode.free;
  1128. if assigned(varargsparas) then
  1129. varargsparas.free;
  1130. inherited destroy;
  1131. end;
  1132. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1133. begin
  1134. methodpointerinit:=tblocknode(ppuloadnode(ppufile));
  1135. methodpointer:=ppuloadnode(ppufile);
  1136. methodpointerdone:=tblocknode(ppuloadnode(ppufile));
  1137. _funcretnode:=ppuloadnode(ppufile);
  1138. inherited ppuload(t,ppufile);
  1139. ppufile.getderef(symtableprocentryderef);
  1140. {$warning FIXME: No withsymtable support}
  1141. symtableproc:=nil;
  1142. ppufile.getderef(procdefinitionderef);
  1143. ppufile.getsmallset(callnodeflags);
  1144. end;
  1145. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  1146. begin
  1147. ppuwritenode(ppufile,methodpointerinit);
  1148. ppuwritenode(ppufile,methodpointer);
  1149. ppuwritenode(ppufile,methodpointerdone);
  1150. ppuwritenode(ppufile,_funcretnode);
  1151. inherited ppuwrite(ppufile);
  1152. ppufile.putderef(symtableprocentryderef);
  1153. ppufile.putderef(procdefinitionderef);
  1154. ppufile.putsmallset(callnodeflags);
  1155. end;
  1156. procedure tcallnode.derefnode;
  1157. begin
  1158. if assigned(methodpointerinit) then
  1159. methodpointerinit.derefnode;
  1160. if assigned(methodpointer) then
  1161. methodpointer.derefnode;
  1162. if assigned(methodpointerdone) then
  1163. methodpointerdone.derefnode;
  1164. if assigned(_funcretnode) then
  1165. _funcretnode.derefnode;
  1166. inherited derefnode;
  1167. end;
  1168. procedure tcallnode.buildderefimpl;
  1169. begin
  1170. inherited buildderefimpl;
  1171. symtableprocentryderef.build(symtableprocentry);
  1172. procdefinitionderef.build(procdefinition);
  1173. if assigned(methodpointer) then
  1174. methodpointer.buildderefimpl;
  1175. if assigned(methodpointerinit) then
  1176. methodpointerinit.buildderefimpl;
  1177. if assigned(methodpointerdone) then
  1178. methodpointerdone.buildderefimpl;
  1179. if assigned(_funcretnode) then
  1180. _funcretnode.buildderefimpl;
  1181. end;
  1182. procedure tcallnode.derefimpl;
  1183. var
  1184. pt : tcallparanode;
  1185. i : integer;
  1186. begin
  1187. inherited derefimpl;
  1188. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  1189. if assigned(symtableprocentry) then
  1190. symtableproc:=symtableprocentry.owner;
  1191. procdefinition:=tabstractprocdef(procdefinitionderef.resolve);
  1192. if assigned(methodpointer) then
  1193. methodpointer.derefimpl;
  1194. if assigned(methodpointerinit) then
  1195. methodpointerinit.derefimpl;
  1196. if assigned(methodpointerdone) then
  1197. methodpointerdone.derefimpl;
  1198. if assigned(_funcretnode) then
  1199. _funcretnode.derefimpl;
  1200. { Connect parasyms }
  1201. pt:=tcallparanode(left);
  1202. while assigned(pt) and
  1203. (cpf_varargs_para in pt.callparaflags) do
  1204. pt:=tcallparanode(pt.right);
  1205. for i:=procdefinition.paras.count-1 downto 0 do
  1206. begin
  1207. if not assigned(pt) then
  1208. internalerror(200311077);
  1209. pt.parasym:=tparavarsym(procdefinition.paras[i]);
  1210. pt:=tcallparanode(pt.right);
  1211. end;
  1212. if assigned(pt) then
  1213. internalerror(200311078);
  1214. end;
  1215. function tcallnode.dogetcopy : tnode;
  1216. var
  1217. n : tcallnode;
  1218. i : integer;
  1219. hp,hpn : tparavarsym;
  1220. oldleft : tnode;
  1221. begin
  1222. { Need to use a hack here to prevent the parameters from being copied.
  1223. The parameters must be copied between methodpointerinit/methodpointerdone because
  1224. the can reference methodpointer }
  1225. oldleft:=left;
  1226. left:=nil;
  1227. n:=tcallnode(inherited dogetcopy);
  1228. left:=oldleft;
  1229. n.symtableprocentry:=symtableprocentry;
  1230. n.symtableproc:=symtableproc;
  1231. n.procdefinition:=procdefinition;
  1232. n.typedef := typedef;
  1233. n.callnodeflags := callnodeflags;
  1234. if assigned(methodpointerinit) then
  1235. n.methodpointerinit:=tblocknode(methodpointerinit.dogetcopy)
  1236. else
  1237. n.methodpointerinit:=nil;
  1238. { methodpointerinit is copied, now references to the temp will also be copied
  1239. correctly. We can now copy the parameters and methodpointer }
  1240. if assigned(left) then
  1241. n.left:=left.dogetcopy
  1242. else
  1243. n.left:=nil;
  1244. if assigned(methodpointer) then
  1245. n.methodpointer:=methodpointer.dogetcopy
  1246. else
  1247. n.methodpointer:=nil;
  1248. if assigned(methodpointerdone) then
  1249. n.methodpointerdone:=tblocknode(methodpointerdone.dogetcopy)
  1250. else
  1251. n.methodpointerdone:=nil;
  1252. if assigned(_funcretnode) then
  1253. n._funcretnode:=_funcretnode.dogetcopy
  1254. else
  1255. n._funcretnode:=nil;
  1256. if assigned(varargsparas) then
  1257. begin
  1258. n.varargsparas:=tvarargsparalist.create(true);
  1259. for i:=0 to varargsparas.count-1 do
  1260. begin
  1261. hp:=tparavarsym(varargsparas[i]);
  1262. hpn:=tparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vardef,[]);
  1263. n.varargsparas.add(hpn);
  1264. end;
  1265. end
  1266. else
  1267. n.varargsparas:=nil;
  1268. result:=n;
  1269. end;
  1270. procedure tcallnode.insertintolist(l : tnodelist);
  1271. begin
  1272. end;
  1273. procedure tcallnode.convert_carg_array_of_const;
  1274. var
  1275. hp : tarrayconstructornode;
  1276. oldleft : tcallparanode;
  1277. begin
  1278. oldleft:=tcallparanode(left);
  1279. if oldleft.left.nodetype<>arrayconstructorn then
  1280. begin
  1281. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  1282. exit;
  1283. end;
  1284. include(callnodeflags,cnf_uses_varargs);
  1285. { Get arrayconstructor node and insert typeconvs }
  1286. hp:=tarrayconstructornode(oldleft.left);
  1287. { Add c args parameters }
  1288. { It could be an empty set }
  1289. if assigned(hp) and
  1290. assigned(hp.left) then
  1291. begin
  1292. while assigned(hp) do
  1293. begin
  1294. left:=ccallparanode.create(hp.left,left);
  1295. { set callparanode resultdef and flags }
  1296. left.resultdef:=hp.left.resultdef;
  1297. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  1298. hp.left:=nil;
  1299. hp:=tarrayconstructornode(hp.right);
  1300. end;
  1301. end;
  1302. { Remove value of old array of const parameter, but keep it
  1303. in the list because it is required for bind_parasym.
  1304. Generate a nothign to keep callparanoed.left valid }
  1305. oldleft.left.free;
  1306. oldleft.left:=cnothingnode.create;
  1307. end;
  1308. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  1309. var
  1310. pd : tprocdef;
  1311. i : longint;
  1312. j : integer;
  1313. hs : string;
  1314. begin
  1315. if (tsym(sym).typ<>procsym) then
  1316. exit;
  1317. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1318. begin
  1319. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1320. hs:=pd.procsym.name+pd.typename_paras(false);
  1321. j:=AbstractMethodsList.FindIndexOf(hs);
  1322. if j<>-1 then
  1323. AbstractMethodsList[j]:=pd
  1324. else
  1325. AbstractMethodsList.Add(hs,pd);
  1326. end;
  1327. end;
  1328. procedure tcallnode.verifyabstractcalls;
  1329. var
  1330. objectdf : tobjectdef;
  1331. parents : tlinkedlist;
  1332. objectinfo : tobjectinfoitem;
  1333. pd : tprocdef;
  1334. i : integer;
  1335. first : boolean;
  1336. begin
  1337. objectdf := nil;
  1338. { verify if trying to create an instance of a class which contains
  1339. non-implemented abstract methods }
  1340. { first verify this class type, no class than exit }
  1341. { also, this checking can only be done if the constructor is directly
  1342. called, indirect constructor calls cannot be checked.
  1343. }
  1344. if assigned(methodpointer) and
  1345. not (nf_is_self in methodpointer.flags) then
  1346. begin
  1347. if (methodpointer.resultdef.typ = objectdef) then
  1348. objectdf:=tobjectdef(methodpointer.resultdef)
  1349. else
  1350. if (methodpointer.resultdef.typ = classrefdef) and
  1351. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1352. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1353. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1354. end;
  1355. if not assigned(objectdf) then
  1356. exit;
  1357. parents := tlinkedlist.create;
  1358. AbstractMethodsList := TFPHashList.create;
  1359. { insert all parents in this class : the first item in the
  1360. list will be the base parent of the class .
  1361. }
  1362. while assigned(objectdf) do
  1363. begin
  1364. objectinfo:=tobjectinfoitem.create(objectdf);
  1365. parents.insert(objectinfo);
  1366. objectdf := objectdf.childof;
  1367. end;
  1368. { now all parents are in the correct order
  1369. insert all abstract methods in the list, and remove
  1370. those which are overriden by parent classes.
  1371. }
  1372. objectinfo:=tobjectinfoitem(parents.first);
  1373. while assigned(objectinfo) do
  1374. begin
  1375. objectdf := objectinfo.objinfo;
  1376. if assigned(objectdf.symtable) then
  1377. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1378. objectinfo:=tobjectinfoitem(objectinfo.next);
  1379. end;
  1380. if assigned(parents) then
  1381. parents.free;
  1382. { Finally give out a warning for each abstract method still in the list }
  1383. first:=true;
  1384. for i:=0 to AbstractMethodsList.Count-1 do
  1385. begin
  1386. pd:=tprocdef(AbstractMethodsList[i]);
  1387. if po_abstractmethod in pd.procoptions then
  1388. begin
  1389. if first then
  1390. begin
  1391. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  1392. first:=false;
  1393. end;
  1394. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1395. end;
  1396. end;
  1397. if assigned(AbstractMethodsList) then
  1398. AbstractMethodsList.Free;
  1399. end;
  1400. function tcallnode.gen_self_tree_methodpointer:tnode;
  1401. var
  1402. hsym : tfieldvarsym;
  1403. begin
  1404. { find self field in methodpointer record }
  1405. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('self'));
  1406. if not assigned(hsym) then
  1407. internalerror(200305251);
  1408. { Load tmehodpointer(right).self }
  1409. result:=csubscriptnode.create(
  1410. hsym,
  1411. ctypeconvnode.create_internal(right.getcopy,methodpointertype));
  1412. end;
  1413. function tcallnode.gen_self_tree:tnode;
  1414. var
  1415. selftree : tnode;
  1416. begin
  1417. selftree:=nil;
  1418. { inherited }
  1419. if (cnf_inherited in callnodeflags) then
  1420. selftree:=load_self_node
  1421. else
  1422. { constructors }
  1423. if (procdefinition.proctypeoption=potype_constructor) then
  1424. begin
  1425. { push 0 as self when allocation is needed }
  1426. if (methodpointer.resultdef.typ=classrefdef) or
  1427. (cnf_new_call in callnodeflags) then
  1428. selftree:=cpointerconstnode.create(0,voidpointertype)
  1429. else
  1430. begin
  1431. if methodpointer.nodetype=typen then
  1432. selftree:=load_self_node
  1433. else
  1434. selftree:=methodpointer.getcopy;
  1435. end;
  1436. end
  1437. else
  1438. { Calling a static/class method }
  1439. if (po_classmethod in procdefinition.procoptions) or
  1440. (po_staticmethod in procdefinition.procoptions) then
  1441. begin
  1442. if (procdefinition.typ<>procdef) then
  1443. internalerror(200305062);
  1444. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1445. begin
  1446. { we only need the vmt, loading self is not required and there is no
  1447. need to check for typen, because that will always get the
  1448. loadvmtaddrnode added }
  1449. selftree:=methodpointer.getcopy;
  1450. if (methodpointer.resultdef.typ<>classrefdef) or
  1451. (methodpointer.nodetype = typen) then
  1452. selftree:=cloadvmtaddrnode.create(selftree);
  1453. end
  1454. else
  1455. selftree:=cpointerconstnode.create(0,voidpointertype);
  1456. end
  1457. else
  1458. begin
  1459. if methodpointer.nodetype=typen then
  1460. selftree:=load_self_node
  1461. else
  1462. selftree:=methodpointer.getcopy;
  1463. end;
  1464. result:=selftree;
  1465. end;
  1466. function tcallnode.gen_vmt_tree:tnode;
  1467. var
  1468. vmttree : tnode;
  1469. begin
  1470. vmttree:=nil;
  1471. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1472. internalerror(200305051);
  1473. { Handle classes and legacy objects separate to make it
  1474. more maintainable }
  1475. if (methodpointer.resultdef.typ=classrefdef) then
  1476. begin
  1477. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  1478. internalerror(200501041);
  1479. { constructor call via classreference => allocate memory }
  1480. if (procdefinition.proctypeoption=potype_constructor) then
  1481. begin
  1482. vmttree:=methodpointer.getcopy;
  1483. { Only a typenode can be passed when it is called with <class of xx>.create }
  1484. if vmttree.nodetype=typen then
  1485. vmttree:=cloadvmtaddrnode.create(vmttree);
  1486. end
  1487. else
  1488. begin
  1489. { Call afterconstruction }
  1490. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1491. end;
  1492. end
  1493. else
  1494. { Class style objects }
  1495. if is_class(methodpointer.resultdef) then
  1496. begin
  1497. { inherited call, no create/destroy }
  1498. if (cnf_inherited in callnodeflags) then
  1499. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1500. else
  1501. { do not create/destroy when called from member function
  1502. without specifying self explicit }
  1503. if (cnf_member_call in callnodeflags) then
  1504. begin
  1505. { destructor: don't release instance, vmt=0
  1506. constructor:
  1507. if called from a constructor in the same class then
  1508. don't call afterconstruction, vmt=0
  1509. else
  1510. call afterconstrution, vmt=1 }
  1511. if (procdefinition.proctypeoption=potype_destructor) then
  1512. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1513. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1514. (procdefinition.proctypeoption=potype_constructor) then
  1515. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1516. else
  1517. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1518. end
  1519. else
  1520. { normal call to method like cl1.proc }
  1521. begin
  1522. { destructor:
  1523. if not called from exception block in constructor
  1524. call beforedestruction and release instance, vmt=1
  1525. else
  1526. don't call beforedestruction and release instance, vmt=-1
  1527. constructor:
  1528. if called from a constructor in the same class using self.create then
  1529. don't call afterconstruction, vmt=0
  1530. else
  1531. call afterconstrution, vmt=1 }
  1532. if (procdefinition.proctypeoption=potype_destructor) then
  1533. if not(cnf_create_failed in callnodeflags) then
  1534. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1535. else
  1536. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  1537. else
  1538. begin
  1539. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1540. (procdefinition.proctypeoption=potype_constructor) and
  1541. (nf_is_self in methodpointer.flags) then
  1542. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1543. else
  1544. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1545. end;
  1546. end;
  1547. end
  1548. else
  1549. { Old style object }
  1550. begin
  1551. { constructor with extended syntax called from new }
  1552. if (cnf_new_call in callnodeflags) then
  1553. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1554. else
  1555. { destructor with extended syntax called from dispose }
  1556. if (cnf_dispose_call in callnodeflags) then
  1557. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1558. else
  1559. { inherited call, no create/destroy }
  1560. if (cnf_inherited in callnodeflags) then
  1561. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1562. else
  1563. { do not create/destroy when called from member function
  1564. without specifying self explicit }
  1565. if (cnf_member_call in callnodeflags) then
  1566. begin
  1567. { destructor: don't release instance, vmt=0
  1568. constructor: don't initialize instance, vmt=0 }
  1569. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1570. end
  1571. else
  1572. { normal object call like obj.proc }
  1573. begin
  1574. { destructor: direct call, no dispose, vmt=0
  1575. constructor: initialize object, load vmt }
  1576. if (procdefinition.proctypeoption=potype_constructor) then
  1577. begin
  1578. { old styled inherited call? }
  1579. if (methodpointer.nodetype=typen) then
  1580. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1581. else
  1582. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1583. end
  1584. else
  1585. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1586. end;
  1587. end;
  1588. result:=vmttree;
  1589. end;
  1590. type
  1591. pcallparanode = ^tcallparanode;
  1592. procedure tcallnode.bind_parasym;
  1593. var
  1594. i : integer;
  1595. pt : tcallparanode;
  1596. oldppt : pcallparanode;
  1597. varargspara,
  1598. currpara : tparavarsym;
  1599. used_by_callnode : boolean;
  1600. hiddentree : tnode;
  1601. newstatement : tstatementnode;
  1602. temp : ttempcreatenode;
  1603. begin
  1604. pt:=tcallparanode(left);
  1605. oldppt:=pcallparanode(@left);
  1606. { flag all callparanodes that belong to the varargs }
  1607. i:=paralength;
  1608. while (i>procdefinition.maxparacount) do
  1609. begin
  1610. include(pt.callparaflags,cpf_varargs_para);
  1611. oldppt:=pcallparanode(@pt.right);
  1612. pt:=tcallparanode(pt.right);
  1613. dec(i);
  1614. end;
  1615. { skip varargs that are inserted by array of const }
  1616. while assigned(pt) and
  1617. (cpf_varargs_para in pt.callparaflags) do
  1618. pt:=tcallparanode(pt.right);
  1619. { process normal parameters and insert hidden parameters }
  1620. for i:=procdefinition.paras.count-1 downto 0 do
  1621. begin
  1622. currpara:=tparavarsym(procdefinition.paras[i]);
  1623. if vo_is_hidden_para in currpara.varoptions then
  1624. begin
  1625. { generate hidden tree }
  1626. used_by_callnode:=false;
  1627. hiddentree:=nil;
  1628. if (vo_is_funcret in currpara.varoptions) then
  1629. begin
  1630. { Generate funcretnode if not specified }
  1631. if assigned(funcretnode) then
  1632. begin
  1633. hiddentree:=funcretnode.getcopy;
  1634. end
  1635. else
  1636. begin
  1637. hiddentree:=internalstatements(newstatement);
  1638. { need to use resultdef instead of procdefinition.returndef,
  1639. because they can be different }
  1640. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1641. addstatement(newstatement,temp);
  1642. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1643. addstatement(newstatement,ctemprefnode.create(temp));
  1644. end;
  1645. end
  1646. else
  1647. if vo_is_high_para in currpara.varoptions then
  1648. begin
  1649. if not assigned(pt) or
  1650. (i=0) then
  1651. internalerror(200304082);
  1652. { we need the information of the previous parameter }
  1653. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1654. end
  1655. else
  1656. if vo_is_self in currpara.varoptions then
  1657. begin
  1658. if assigned(right) then
  1659. hiddentree:=gen_self_tree_methodpointer
  1660. else
  1661. hiddentree:=gen_self_tree;
  1662. end
  1663. else
  1664. if vo_is_vmt in currpara.varoptions then
  1665. begin
  1666. hiddentree:=gen_vmt_tree;
  1667. end
  1668. {$if defined(powerpc) or defined(m68k)}
  1669. else
  1670. if vo_is_syscall_lib in currpara.varoptions then
  1671. begin
  1672. { lib parameter has no special type but proccalloptions must be a syscall }
  1673. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1674. end
  1675. {$endif powerpc or m68k}
  1676. else
  1677. if vo_is_parentfp in currpara.varoptions then
  1678. begin
  1679. if not(assigned(procdefinition.owner.defowner)) then
  1680. internalerror(200309287);
  1681. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1682. end;
  1683. { add the hidden parameter }
  1684. if not assigned(hiddentree) then
  1685. internalerror(200304073);
  1686. { Already insert para and let the previous node point to
  1687. this new node }
  1688. pt:=ccallparanode.create(hiddentree,oldppt^);
  1689. pt.used_by_callnode:=used_by_callnode;
  1690. oldppt^:=pt;
  1691. end;
  1692. if not assigned(pt) then
  1693. internalerror(200310052);
  1694. pt.parasym:=currpara;
  1695. oldppt:=pcallparanode(@pt.right);
  1696. pt:=tcallparanode(pt.right);
  1697. end;
  1698. { Create parasyms for varargs, first count the number of varargs paras,
  1699. then insert the parameters with numbering in reverse order. The SortParas
  1700. will set the correct order at the end}
  1701. pt:=tcallparanode(left);
  1702. i:=0;
  1703. while assigned(pt) do
  1704. begin
  1705. if cpf_varargs_para in pt.callparaflags then
  1706. inc(i);
  1707. pt:=tcallparanode(pt.right);
  1708. end;
  1709. if (i>0) then
  1710. begin
  1711. varargsparas:=tvarargsparalist.create;
  1712. pt:=tcallparanode(left);
  1713. while assigned(pt) do
  1714. begin
  1715. if cpf_varargs_para in pt.callparaflags then
  1716. begin
  1717. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  1718. dec(i);
  1719. { varargspara is left-right, use insert
  1720. instead of concat }
  1721. varargsparas.add(varargspara);
  1722. pt.parasym:=varargspara;
  1723. end;
  1724. pt:=tcallparanode(pt.right);
  1725. end;
  1726. varargsparas.sortparas;
  1727. end;
  1728. end;
  1729. function tcallnode.pass_typecheck:tnode;
  1730. var
  1731. candidates : tcallcandidates;
  1732. oldcallnode : tcallnode;
  1733. hpt : tnode;
  1734. pt : tcallparanode;
  1735. lastpara : longint;
  1736. paraidx,
  1737. cand_cnt : integer;
  1738. i : longint;
  1739. is_const : boolean;
  1740. statements : tstatementnode;
  1741. converted_result_data : ttempcreatenode;
  1742. label
  1743. errorexit;
  1744. begin
  1745. result:=nil;
  1746. candidates:=nil;
  1747. oldcallnode:=aktcallnode;
  1748. aktcallnode:=self;
  1749. { determine length of parameter list }
  1750. pt:=tcallparanode(left);
  1751. paralength:=0;
  1752. while assigned(pt) do
  1753. begin
  1754. inc(paralength);
  1755. pt:=tcallparanode(pt.right);
  1756. end;
  1757. { determine the type of the parameters }
  1758. if assigned(left) then
  1759. begin
  1760. tcallparanode(left).get_paratype;
  1761. if codegenerror then
  1762. goto errorexit;
  1763. end;
  1764. if assigned(methodpointer) then
  1765. begin
  1766. typecheckpass(methodpointer);
  1767. maybe_load_para_in_temp(methodpointer);
  1768. end;
  1769. { procedure variable ? }
  1770. if assigned(right) then
  1771. begin
  1772. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1773. typecheckpass(right);
  1774. if codegenerror then
  1775. exit;
  1776. procdefinition:=tabstractprocdef(right.resultdef);
  1777. { Compare parameters from right to left }
  1778. paraidx:=procdefinition.Paras.count-1;
  1779. { Skip default parameters }
  1780. if not(po_varargs in procdefinition.procoptions) then
  1781. begin
  1782. { ignore hidden parameters }
  1783. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1784. dec(paraidx);
  1785. for i:=1 to procdefinition.maxparacount-paralength do
  1786. begin
  1787. if paraidx<0 then
  1788. internalerror(200402261);
  1789. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1790. begin
  1791. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1792. goto errorexit;
  1793. end;
  1794. dec(paraidx);
  1795. end;
  1796. end;
  1797. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1798. dec(paraidx);
  1799. pt:=tcallparanode(left);
  1800. lastpara:=paralength;
  1801. while (paraidx>=0) and assigned(pt) do
  1802. begin
  1803. { only goto next para if we're out of the varargs }
  1804. if not(po_varargs in procdefinition.procoptions) or
  1805. (lastpara<=procdefinition.maxparacount) then
  1806. begin
  1807. repeat
  1808. dec(paraidx);
  1809. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1810. end;
  1811. pt:=tcallparanode(pt.right);
  1812. dec(lastpara);
  1813. end;
  1814. if assigned(pt) or
  1815. ((paraidx>=0) and
  1816. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  1817. begin
  1818. if assigned(pt) then
  1819. current_filepos:=pt.fileinfo;
  1820. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1821. goto errorexit;
  1822. end;
  1823. end
  1824. else
  1825. { not a procedure variable }
  1826. begin
  1827. { do we know the procedure to call ? }
  1828. if not(assigned(procdefinition)) then
  1829. begin
  1830. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags),
  1831. { ignore possible private in delphi mode for anon. inherited (FK) }
  1832. (m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  1833. { no procedures found? then there is something wrong
  1834. with the parameter size or the procedures are
  1835. not accessible }
  1836. if candidates.count=0 then
  1837. begin
  1838. { when it's an auto inherited call and there
  1839. is no procedure found, but the procedures
  1840. were defined with overload directive and at
  1841. least two procedures are defined then we ignore
  1842. this inherited by inserting a nothingn. Only
  1843. do this ugly hack in Delphi mode as it looks more
  1844. like a bug. It's also not documented }
  1845. if (m_delphi in current_settings.modeswitches) and
  1846. (cnf_anon_inherited in callnodeflags) and
  1847. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  1848. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  1849. (symtableprocentry.ProcdefList.Count>=2) then
  1850. result:=cnothingnode.create
  1851. else
  1852. begin
  1853. { in tp mode we can try to convert to procvar if
  1854. there are no parameters specified }
  1855. if not(assigned(left)) and
  1856. not(cnf_inherited in callnodeflags) and
  1857. ((m_tp_procvar in current_settings.modeswitches) or
  1858. (m_mac_procvar in current_settings.modeswitches)) and
  1859. (not assigned(methodpointer) or
  1860. (methodpointer.nodetype <> typen)) then
  1861. begin
  1862. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1863. if assigned(methodpointer) then
  1864. tloadnode(hpt).set_mp(get_load_methodpointer);
  1865. typecheckpass(hpt);
  1866. result:=hpt;
  1867. end
  1868. else
  1869. begin
  1870. if assigned(left) then
  1871. current_filepos:=left.fileinfo;
  1872. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  1873. symtableprocentry.write_parameter_lists(nil);
  1874. end;
  1875. end;
  1876. goto errorexit;
  1877. end;
  1878. { Retrieve information about the candidates }
  1879. candidates.get_information;
  1880. {$ifdef EXTDEBUG}
  1881. { Display info when multiple candidates are found }
  1882. if candidates.count>1 then
  1883. candidates.dump_info(V_Debug);
  1884. {$endif EXTDEBUG}
  1885. { Choose the best candidate and count the number of
  1886. candidates left }
  1887. cand_cnt:=candidates.choose_best(procdefinition,
  1888. assigned(left) and
  1889. not assigned(tcallparanode(left).right) and
  1890. (tcallparanode(left).left.resultdef.typ=variantdef));
  1891. { All parameters are checked, check if there are any
  1892. procedures left }
  1893. if cand_cnt>0 then
  1894. begin
  1895. { Multiple candidates left? }
  1896. if cand_cnt>1 then
  1897. begin
  1898. CGMessage(type_e_cant_choose_overload_function);
  1899. {$ifdef EXTDEBUG}
  1900. candidates.dump_info(V_Hint);
  1901. {$else EXTDEBUG}
  1902. candidates.list(false);
  1903. {$endif EXTDEBUG}
  1904. { we'll just use the first candidate to make the
  1905. call }
  1906. end;
  1907. { assign procdefinition }
  1908. if symtableproc=nil then
  1909. symtableproc:=procdefinition.owner;
  1910. end
  1911. else
  1912. begin
  1913. { No candidates left, this must be a type error,
  1914. because wrong size is already checked. procdefinition
  1915. is filled with the first (random) definition that is
  1916. found. We use this definition to display a nice error
  1917. message that the wrong type is passed }
  1918. candidates.find_wrong_para;
  1919. candidates.list(true);
  1920. {$ifdef EXTDEBUG}
  1921. candidates.dump_info(V_Hint);
  1922. {$endif EXTDEBUG}
  1923. { We can not proceed, release all procs and exit }
  1924. candidates.free;
  1925. goto errorexit;
  1926. end;
  1927. candidates.free;
  1928. end; { end of procedure to call determination }
  1929. end;
  1930. { check for hints (deprecated etc) }
  1931. if (procdefinition.typ = procdef) then
  1932. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  1933. { add needed default parameters }
  1934. if assigned(procdefinition) and
  1935. (paralength<procdefinition.maxparacount) then
  1936. begin
  1937. paraidx:=0;
  1938. i:=0;
  1939. while (i<paralength) do
  1940. begin
  1941. if paraidx>=procdefinition.Paras.count then
  1942. internalerror(200306181);
  1943. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  1944. inc(i);
  1945. inc(paraidx);
  1946. end;
  1947. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1948. inc(paraidx);
  1949. while (paraidx<procdefinition.paras.count) do
  1950. begin
  1951. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1952. internalerror(200212142);
  1953. left:=ccallparanode.create(genconstsymtree(
  1954. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  1955. { Ignore vs_hidden parameters }
  1956. repeat
  1957. inc(paraidx);
  1958. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1959. end;
  1960. end;
  1961. { recursive call? }
  1962. if assigned(current_procinfo) and
  1963. (procdefinition=current_procinfo.procdef) then
  1964. include(current_procinfo.flags,pi_is_recursive);
  1965. { handle predefined procedures }
  1966. is_const:=(po_internconst in procdefinition.procoptions) and
  1967. ((block_type in [bt_const,bt_type]) or
  1968. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1969. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1970. begin
  1971. if assigned(left) then
  1972. begin
  1973. { ptr and settextbuf needs two args }
  1974. if assigned(tcallparanode(left).right) then
  1975. begin
  1976. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1977. left:=nil;
  1978. end
  1979. else
  1980. begin
  1981. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1982. tcallparanode(left).left:=nil;
  1983. end;
  1984. end
  1985. else
  1986. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1987. result:=hpt;
  1988. goto errorexit;
  1989. end;
  1990. { ensure that the result type is set }
  1991. if not(cnf_typedefset in callnodeflags) then
  1992. begin
  1993. { constructors return their current class type, not the type where the
  1994. constructor is declared, this can be different because of inheritance }
  1995. if (procdefinition.proctypeoption=potype_constructor) and
  1996. assigned(methodpointer) and
  1997. assigned(methodpointer.resultdef) and
  1998. (methodpointer.resultdef.typ=classrefdef) then
  1999. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  2000. else
  2001. { Member call to a (inherited) constructor from the class, the return
  2002. value is always self, so we change it to voidtype to generate an
  2003. error and to prevent users from generating non-working code
  2004. when they expect to clone the current instance, see bug 3662 (PFV) }
  2005. if (procdefinition.proctypeoption=potype_constructor) and
  2006. is_class(tprocdef(procdefinition)._class) and
  2007. assigned(methodpointer) and
  2008. (nf_is_self in methodpointer.flags) then
  2009. resultdef:=voidtype
  2010. else
  2011. resultdef:=procdefinition.returndef;
  2012. end
  2013. else
  2014. resultdef:=typedef;
  2015. {if resultdef.needs_inittable then
  2016. include(current_procinfo.flags,pi_needs_implicit_finally);}
  2017. if assigned(methodpointer) then
  2018. begin
  2019. { when methodpointer is a callnode we must load it first into a
  2020. temp to prevent the processing callnode twice }
  2021. if (methodpointer.nodetype=calln) then
  2022. internalerror(200405121);
  2023. { direct call to inherited abstract method, then we
  2024. can already give a error in the compiler instead
  2025. of a runtime error }
  2026. if (cnf_inherited in callnodeflags) and
  2027. (po_abstractmethod in procdefinition.procoptions) then
  2028. begin
  2029. if (m_delphi in current_settings.modeswitches) and
  2030. (cnf_anon_inherited in callnodeflags) then
  2031. begin
  2032. CGMessage(cg_h_inherited_ignored);
  2033. result:=cnothingnode.create;
  2034. exit;
  2035. end
  2036. else
  2037. CGMessage(cg_e_cant_call_abstract_method);
  2038. end;
  2039. { if an inherited con- or destructor should be }
  2040. { called in a con- or destructor then a warning }
  2041. { will be made }
  2042. { con- and destructors need a pointer to the vmt }
  2043. if (cnf_inherited in callnodeflags) and
  2044. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2045. is_object(methodpointer.resultdef) and
  2046. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  2047. CGMessage(cg_w_member_cd_call_from_method);
  2048. if methodpointer.nodetype<>typen then
  2049. begin
  2050. { Remove all postfix operators }
  2051. hpt:=methodpointer;
  2052. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  2053. hpt:=tunarynode(hpt).left;
  2054. if (procdefinition.proctypeoption=potype_constructor) and
  2055. assigned(symtableproc) and
  2056. (symtableproc.symtabletype=withsymtable) and
  2057. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  2058. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2059. { R.Init then R will be initialized by the constructor,
  2060. Also allow it for simple loads }
  2061. if (procdefinition.proctypeoption=potype_constructor) or
  2062. ((hpt.nodetype=loadn) and
  2063. (methodpointer.resultdef.typ=objectdef) and
  2064. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  2065. ) then
  2066. { a constructor will and a method may write something to }
  2067. { the fields }
  2068. set_varstate(methodpointer,vs_readwritten,[])
  2069. else if ((hpt.nodetype=loadn) and
  2070. (methodpointer.resultdef.typ=classrefdef)) then
  2071. set_varstate(methodpointer,vs_read,[])
  2072. else
  2073. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  2074. { The object is already used if it is called once }
  2075. if (hpt.nodetype=loadn) and
  2076. (tloadnode(hpt).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  2077. set_varstate(hpt,vs_read,[]);
  2078. // tabstractvarsym(tloadnode(hpt).symtableentry).varstate:=vs_readwritten;
  2079. end;
  2080. { if we are calling the constructor check for abstract
  2081. methods. Ignore inherited and member calls, because the
  2082. class is then already created }
  2083. if (procdefinition.proctypeoption=potype_constructor) and
  2084. not(cnf_inherited in callnodeflags) and
  2085. not(cnf_member_call in callnodeflags) then
  2086. verifyabstractcalls;
  2087. end
  2088. else
  2089. begin
  2090. { When this is method the methodpointer must be available }
  2091. if (right=nil) and
  2092. (procdefinition.owner.symtabletype=ObjectSymtable) then
  2093. internalerror(200305061);
  2094. end;
  2095. { Set flag that the procedure uses varargs, also if they are not passed it is still
  2096. needed for x86_64 to pass the number of SSE registers used }
  2097. if po_varargs in procdefinition.procoptions then
  2098. include(callnodeflags,cnf_uses_varargs);
  2099. { Change loading of array of const to varargs }
  2100. if assigned(left) and
  2101. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  2102. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  2103. convert_carg_array_of_const;
  2104. { bind parasyms to the callparanodes and insert hidden parameters }
  2105. bind_parasym;
  2106. { insert type conversions for parameters }
  2107. if assigned(left) then
  2108. tcallparanode(left).insert_typeconv(true);
  2109. { dispinterface methode invoke? }
  2110. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  2111. begin
  2112. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  2113. if cnf_return_value_used in callnodeflags then
  2114. begin
  2115. result:=internalstatements(statements);
  2116. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  2117. addstatement(statements,converted_result_data);
  2118. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  2119. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid,true),
  2120. procdefinition.returndef)));
  2121. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  2122. addstatement(statements,ctemprefnode.create(converted_result_data));
  2123. end
  2124. else
  2125. result:=translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid,false);
  2126. { don't free reused nodes }
  2127. methodpointer:=nil;
  2128. parameters:=nil;
  2129. end;
  2130. errorexit:
  2131. aktcallnode:=oldcallnode;
  2132. end;
  2133. procedure tcallnode.order_parameters;
  2134. var
  2135. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2136. currloc : tcgloc;
  2137. begin
  2138. hpfirst:=nil;
  2139. hpcurr:=tcallparanode(left);
  2140. while assigned(hpcurr) do
  2141. begin
  2142. { pull out }
  2143. hpnext:=tcallparanode(hpcurr.right);
  2144. { pull in at the correct place.
  2145. Used order:
  2146. 1. LOC_REFERENCE with smallest offset (i386 only)
  2147. 2. LOC_REFERENCE with most registers and least complexity (non-i386 only)
  2148. 3. LOC_REFERENCE with least registers and most complexity (non-i386 only)
  2149. 4. LOC_REGISTER with most registers and most complexity
  2150. 5. LOC_REGISTER with least registers and least complexity
  2151. For the moment we only look at the first parameter field. Combining it
  2152. with multiple parameter fields will make things a lot complexer (PFV)
  2153. The reason for the difference regarding complexity ordering
  2154. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  2155. we first want to treat the LOC_REFERENCE destinations whose
  2156. calculation does not require a call, because their location
  2157. may contain registers which might otherwise have to be saved
  2158. if a call has to be evaluated first. The calculated value is
  2159. stored on the stack and will thus no longer occupy any
  2160. register.
  2161. Similarly, for the register parameters we first want to
  2162. evaluate the calls, because otherwise the already loaded
  2163. register parameters will have to be saved so the intermediate
  2164. call can be evaluated (JM) }
  2165. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2166. internalerror(200412152);
  2167. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2168. hpprev:=nil;
  2169. hp:=hpfirst;
  2170. while assigned(hp) do
  2171. begin
  2172. case currloc of
  2173. LOC_REFERENCE :
  2174. begin
  2175. case hp.parasym.paraloc[callerside].location^.loc of
  2176. LOC_REFERENCE :
  2177. begin
  2178. { Offset is calculated like:
  2179. sub esp,12
  2180. mov [esp+8],para3
  2181. mov [esp+4],para2
  2182. mov [esp],para1
  2183. call function
  2184. That means the for pushes the para with the
  2185. highest offset (see para3) needs to be pushed first
  2186. }
  2187. {$ifdef i386}
  2188. { the i386 code generator expects all reference }
  2189. { parameter to be in this order so it can use }
  2190. { pushes }
  2191. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset) then
  2192. {$else i386}
  2193. if (hpcurr.registersint>hp.registersint) or
  2194. (node_complexity(hpcurr)<node_complexity(hp)) then
  2195. {$endif i386}
  2196. break;
  2197. end;
  2198. LOC_MMREGISTER,
  2199. LOC_REGISTER,
  2200. LOC_FPUREGISTER :
  2201. break;
  2202. end;
  2203. end;
  2204. LOC_MMREGISTER,
  2205. LOC_FPUREGISTER,
  2206. LOC_REGISTER :
  2207. begin
  2208. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  2209. (node_complexity(hpcurr)>node_complexity(hp)) then
  2210. break;
  2211. end;
  2212. end;
  2213. hpprev:=hp;
  2214. hp:=tcallparanode(hp.right);
  2215. end;
  2216. hpcurr.right:=hp;
  2217. if assigned(hpprev) then
  2218. hpprev.right:=hpcurr
  2219. else
  2220. hpfirst:=hpcurr;
  2221. { next }
  2222. hpcurr:=hpnext;
  2223. end;
  2224. left:=hpfirst;
  2225. end;
  2226. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2227. var
  2228. paras: tcallparanode;
  2229. temp: tnode;
  2230. indexnr : integer;
  2231. begin
  2232. result := fen_false;
  2233. n.fileinfo := pfileposinfo(arg)^;
  2234. if (n.nodetype = loadn) then
  2235. begin
  2236. case tloadnode(n).symtableentry.typ of
  2237. paravarsym :
  2238. begin
  2239. paras := tcallparanode(left);
  2240. while assigned(paras) and
  2241. (paras.parasym <> tloadnode(n).symtableentry) do
  2242. paras := tcallparanode(paras.right);
  2243. if assigned(paras) then
  2244. begin
  2245. n.free;
  2246. n := paras.left.getcopy;
  2247. typecheckpass(n);
  2248. result := fen_true;
  2249. end;
  2250. end;
  2251. localvarsym :
  2252. begin
  2253. { local? }
  2254. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2255. exit;
  2256. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2257. if (indexnr >= inlinelocals.count) or
  2258. not assigned(inlinelocals[indexnr]) then
  2259. internalerror(20040720);
  2260. temp := tnode(inlinelocals[indexnr]).getcopy;
  2261. n.free;
  2262. n := temp;
  2263. typecheckpass(n);
  2264. result := fen_true;
  2265. end;
  2266. end;
  2267. end;
  2268. end;
  2269. type
  2270. ptempnodes = ^ttempnodes;
  2271. ttempnodes = record
  2272. createstatement, deletestatement: tstatementnode;
  2273. end;
  2274. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2275. var
  2276. tempinfo: ptempnodes absolute arg;
  2277. tempnode: ttempcreatenode;
  2278. indexnr : integer;
  2279. begin
  2280. if (TSym(p).typ <> localvarsym) then
  2281. exit;
  2282. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2283. if (indexnr >= inlinelocals.count) then
  2284. inlinelocals.count:=indexnr+10;
  2285. if (vo_is_funcret in tabstractvarsym(p).varoptions) and
  2286. assigned(funcretnode) then
  2287. begin
  2288. if node_complexity(funcretnode) > 1 then
  2289. begin
  2290. { can this happen? }
  2291. { we may have to replace the funcretnode with the address of funcretnode }
  2292. { loaded in a temp in this case, because the expression may e.g. contain }
  2293. { a global variable that gets changed inside the function }
  2294. internalerror(2004072101);
  2295. end;
  2296. inlinelocals[indexnr] := funcretnode.getcopy
  2297. end
  2298. else
  2299. begin
  2300. if (vo_is_funcret in tlocalvarsym(p).varoptions) then
  2301. begin
  2302. tempnode := ctempcreatenode.create_inlined_result(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2303. addstatement(tempinfo^.createstatement,tempnode);
  2304. funcretnode := ctemprefnode.create(tempnode);
  2305. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2306. end
  2307. else
  2308. begin
  2309. tempnode := ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2310. addstatement(tempinfo^.createstatement,tempnode);
  2311. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  2312. end;
  2313. { inherit addr_taken flag }
  2314. if (tabstractvarsym(p).addr_taken) then
  2315. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2316. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2317. end;
  2318. end;
  2319. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2320. begin
  2321. result := fen_false;
  2322. { this is just to play it safe, there are more safe situations }
  2323. if (n.nodetype = derefn) or
  2324. ((n.nodetype = loadn) and
  2325. { globals and fields of (possibly global) objects could always be changed in the callee }
  2326. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2327. { statics can only be modified by functions in the same unit }
  2328. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2329. (tloadnode(n).symtable = TSymtable(arg))))) or
  2330. ((n.nodetype = subscriptn) and
  2331. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2332. result := fen_norecurse_true;
  2333. end;
  2334. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  2335. var
  2336. para: tcallparanode;
  2337. tempnode: ttempcreatenode;
  2338. tempnodes: ttempnodes;
  2339. n: tnode;
  2340. paracomplexity: longint;
  2341. begin
  2342. { parameters }
  2343. para := tcallparanode(left);
  2344. while assigned(para) do
  2345. begin
  2346. if (para.parasym.typ = paravarsym) and
  2347. { para.left will already be the same as funcretnode in the following case, so don't change }
  2348. (not(vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2349. (not assigned(funcretnode))) then
  2350. begin
  2351. { must take copy of para.left, because if it contains a }
  2352. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2353. { then this parameter must be changed to point to the copy of }
  2354. { that temp (JM) }
  2355. n := para.left.getcopy;
  2356. para.left.free;
  2357. para.left := n;
  2358. firstpass(para.left);
  2359. { create temps for value parameters, function result and also for }
  2360. { const parameters which are passed by value instead of by reference }
  2361. { we need to take care that we use the type of the defined parameter and not of the
  2362. passed parameter, because these can be different in case of a formaldef (PFV) }
  2363. paracomplexity := node_complexity(para.left);
  2364. { check if we have to create a temp, assign the parameter's }
  2365. { contents to that temp and then substitute the paramter }
  2366. { with the temp everywhere in the function }
  2367. if
  2368. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2369. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2370. { we can't assign to formaldef temps }
  2371. ((para.parasym.vardef.typ<>formaldef) and
  2372. (
  2373. { if paracomplexity > 1, we normally take the address of }
  2374. { the parameter expression, store it in a temp and }
  2375. { substitute the dereferenced temp in the inlined function }
  2376. { We can't do this if we can't take the address of the }
  2377. { parameter expression, so in that case assign to a temp }
  2378. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2379. ((paracomplexity > 1) and
  2380. (not valid_for_addr(para.left,false) or
  2381. (para.left.nodetype = calln) or
  2382. is_constnode(para.left))) or
  2383. { the problem is that we can't take the address of a function result :( }
  2384. (vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2385. { we do not need to create a temp for value parameters }
  2386. { which are not modified in the inlined function }
  2387. { const parameters can get vs_readwritten if their }
  2388. { address is taken }
  2389. ((((para.parasym.varspez = vs_value) and
  2390. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2391. { in case of const, this is only necessary if the }
  2392. { variable would be passed by value normally, or if }
  2393. { there is such a variable somewhere in an expression }
  2394. ((para.parasym.varspez = vs_const) and
  2395. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2396. (paracomplexity > 1)))) and
  2397. { however, if we pass a global variable, an object field or}
  2398. { an expression containing a pointer dereference as }
  2399. { parameter, this value could be modified in other ways as }
  2400. { well and in such cases create a temp to be on the safe }
  2401. { side }
  2402. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2403. { value parameters of which we know they are modified by }
  2404. { definition have to be copied to a temp }
  2405. ((para.parasym.varspez = vs_value) and
  2406. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2407. { the compiler expects that it can take the address of parameters passed by reference in
  2408. the case of const so we can't replace the node simply by a constant node
  2409. When playing with this code, ensure that
  2410. function f(const a,b : longint) : longint;inline;
  2411. begin
  2412. result:=a*b;
  2413. end;
  2414. [...]
  2415. ...:=f(10,20));
  2416. [...]
  2417. is still folded. (FK)
  2418. }
  2419. ((para.parasym.varspez = vs_const) and
  2420. { const para's can get vs_readwritten if their address }
  2421. { is taken }
  2422. ((para.parasym.varstate = vs_readwritten) or
  2423. { call-by-reference const's may need to be passed by }
  2424. { reference to function called in the inlined code }
  2425. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2426. (not valid_for_addr(para.left,false) or
  2427. is_constnode(para.left)))))
  2428. )
  2429. ) then
  2430. begin
  2431. tempnode:=nil;
  2432. {$ifdef reuse_existing_para_temp}
  2433. { Try to reuse existing result tempnode from a parameter }
  2434. if para.left.nodetype=blockn then
  2435. begin
  2436. n:=tstatementnode(tblocknode(para.left).left);
  2437. while assigned(n) and assigned(tstatementnode(n).right) do
  2438. begin
  2439. if tstatementnode(n).left.nodetype=tempdeleten then
  2440. break;
  2441. n:=tstatementnode(tstatementnode(n).right);
  2442. end;
  2443. { We expect to find the following statements
  2444. tempdeletenode
  2445. tempref
  2446. nil }
  2447. if assigned(n) and
  2448. assigned(tstatementnode(n).right) and
  2449. (tstatementnode(tstatementnode(n).right).right=nil) and
  2450. (tstatementnode(tstatementnode(n).right).left.nodetype=temprefn) then
  2451. begin
  2452. tempnode:=ttempdeletenode(tstatementnode(n).left).tempinfo^.owner;
  2453. para.left:=tstatementnode(tstatementnode(n).right).left;
  2454. addstatement(deletestatement,tstatementnode(n).left);
  2455. { Replace tempdelete,tempref with dummy statement }
  2456. tstatementnode(n).left:=cnothingnode.create;
  2457. tstatementnode(tstatementnode(n).right).left:=cnothingnode.create;
  2458. end;
  2459. end;
  2460. {$endif reuse_existing_para_temp}
  2461. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2462. addstatement(createstatement,tempnode);
  2463. { inherit addr_taken flag }
  2464. if (tabstractvarsym(para.parasym).addr_taken) then
  2465. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2466. { assign the value of the parameter to the temp, except in case of the function result }
  2467. { (in that case, para.left is a block containing the creation of a new temp, while we }
  2468. { only need a temprefnode, so delete the old stuff) }
  2469. if not(vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  2470. begin
  2471. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2472. para.left));
  2473. para.left := ctemprefnode.create(tempnode);
  2474. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2475. end
  2476. else
  2477. begin
  2478. if not(assigned(funcretnode)) then
  2479. funcretnode := ctemprefnode.create(tempnode);
  2480. para.left.free;
  2481. para.left := ctemprefnode.create(tempnode);
  2482. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2483. end;
  2484. end
  2485. { otherwise if the parameter is "complex", take the address }
  2486. { of the parameter expression, store it in a temp and replace }
  2487. { occurrences of the parameter with dereferencings of this }
  2488. { temp }
  2489. else if (paracomplexity > 1) then
  2490. begin
  2491. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2492. addstatement(createstatement,tempnode);
  2493. { inherit addr_taken flag }
  2494. if (tabstractvarsym(para.parasym).addr_taken) then
  2495. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2496. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2497. caddrnode.create_internal(para.left)));
  2498. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  2499. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2500. end;
  2501. end;
  2502. para := tcallparanode(para.right);
  2503. end;
  2504. { local variables }
  2505. if not assigned(tprocdef(procdefinition).localst) or
  2506. (tprocdef(procdefinition).localst.SymList.count = 0) then
  2507. exit;
  2508. tempnodes.createstatement := createstatement;
  2509. tempnodes.deletestatement := deletestatement;
  2510. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  2511. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,@tempnodes);
  2512. createstatement := tempnodes.createstatement;
  2513. deletestatement := tempnodes.deletestatement;
  2514. end;
  2515. function tcallnode.getfuncretassignment(inlineblock: tblocknode): tnode;
  2516. var
  2517. hp: tstatementnode;
  2518. resassign: tnode;
  2519. begin
  2520. result:=nil;
  2521. if not assigned(funcretnode) or
  2522. not(cnf_return_value_used in callnodeflags) then
  2523. exit;
  2524. { tempcreatenode for the function result }
  2525. hp:=tstatementnode(inlineblock.left);
  2526. if not(assigned(hp)) or
  2527. (hp.left.nodetype <> tempcreaten) then
  2528. exit;
  2529. { assignment to the result }
  2530. hp:=tstatementnode(hp.right);
  2531. if not(assigned(hp)) or
  2532. (hp.left.nodetype<>assignn) or
  2533. { left must be function result }
  2534. (not(tassignmentnode(hp.left).left.isequal(funcretnode)) and
  2535. { can have extra type conversion due to absolute mapping }
  2536. { of <fucntionname> on function result var }
  2537. not((tassignmentnode(hp.left).left.nodetype = typeconvn) and
  2538. (ttypeconvnode(tassignmentnode(hp.left).left).convtype = tc_equal) and
  2539. (ttypeconvnode(tassignmentnode(hp.left).left).left.isequal(funcretnode)))) or
  2540. { right must be a constant (mainly to avoid trying to reuse }
  2541. { local temps which may already be freed afterwards once these }
  2542. { checks are made looser) }
  2543. not is_constnode(tassignmentnode(hp.left).right) then
  2544. exit
  2545. else
  2546. resassign:=hp.left;
  2547. { tempdelete to normal of the function result }
  2548. hp:=tstatementnode(hp.right);
  2549. if not(assigned(hp)) or
  2550. (hp.left.nodetype <> tempdeleten) then
  2551. exit;
  2552. { the function result once more }
  2553. hp:=tstatementnode(hp.right);
  2554. if not(assigned(hp)) or
  2555. not(hp.left.isequal(funcretnode)) then
  2556. exit;
  2557. { should be the end }
  2558. if assigned(hp.right) then
  2559. exit;
  2560. { we made it! }
  2561. result:=tassignmentnode(resassign).right.getcopy;
  2562. firstpass(result);
  2563. end;
  2564. function tcallnode.pass1_inline:tnode;
  2565. var
  2566. createstatement,deletestatement: tstatementnode;
  2567. createblock,deleteblock: tblocknode;
  2568. body : tnode;
  2569. begin
  2570. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2571. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2572. internalerror(200412021);
  2573. { inherit flags }
  2574. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2575. { create blocks for loading/deleting of local data }
  2576. createblock:=internalstatements(createstatement);
  2577. deleteblock:=internalstatements(deletestatement);
  2578. { add methodpointer init code to init statement }
  2579. { (fini must be done later, as it will delete the hookoncopy info) }
  2580. if assigned(methodpointerinit) then
  2581. addstatement(createstatement,methodpointerinit.getcopy);
  2582. inlinelocals:=TFPObjectList.create(true);
  2583. { get copy of the procedure body }
  2584. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2585. { replace complex parameters with temps }
  2586. createinlineparas(createstatement,deletestatement);
  2587. { replace the parameter loads with the parameter values }
  2588. foreachnode(body,@replaceparaload,@fileinfo);
  2589. { copy methodpointer fini code }
  2590. if assigned(methodpointerdone) then
  2591. addstatement(deletestatement,methodpointerdone.getcopy);
  2592. { free the temps for the locals }
  2593. inlinelocals.free;
  2594. inlinelocals:=nil;
  2595. addstatement(createstatement,body);
  2596. addstatement(createstatement,deleteblock);
  2597. { set function result location if necessary }
  2598. if assigned(funcretnode) and
  2599. (cnf_return_value_used in callnodeflags) then
  2600. addstatement(createstatement,funcretnode.getcopy);
  2601. { consider it must not be inlined if called
  2602. again inside the args or itself }
  2603. exclude(procdefinition.procoptions,po_inline);
  2604. dosimplify(createblock);
  2605. firstpass(createblock);
  2606. include(procdefinition.procoptions,po_inline);
  2607. { if all that's left of the inlined function is an constant }
  2608. { assignment to the result, replace the whole block with what's }
  2609. { assigned to the result. There will also be a tempcreatenode for }
  2610. { the function result itself though, so ignore it. The statement/ }
  2611. { blocknode simplification code will have removed all nothingn- }
  2612. { statements empty nested blocks, so we don't have to care about }
  2613. { those }
  2614. result := getfuncretassignment(createblock);
  2615. if assigned(result) then
  2616. createblock.free
  2617. else
  2618. { return inlined block }
  2619. result := createblock;
  2620. {$ifdef DEBUGINLINE}
  2621. writeln;
  2622. writeln('**************************',tprocdef(procdefinition).mangledname);
  2623. printnode(output,result);
  2624. {$endif DEBUGINLINE}
  2625. end;
  2626. procedure tcallnode.check_stack_parameters;
  2627. var
  2628. hp : tcallparanode;
  2629. begin
  2630. hp:=tcallparanode(left);
  2631. while assigned(hp) do
  2632. begin
  2633. if assigned(hp.parasym) and
  2634. assigned(hp.parasym.paraloc[callerside].location) and
  2635. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2636. include(current_procinfo.flags,pi_has_stackparameter);
  2637. hp:=tcallparanode(hp.right);
  2638. end;
  2639. end;
  2640. function tcallnode.pass_1 : tnode;
  2641. var
  2642. st : TSymtable;
  2643. n: tcallparanode;
  2644. do_inline: boolean;
  2645. begin
  2646. result:=nil;
  2647. { Can we inline the procedure? }
  2648. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2649. begin
  2650. { Check if we can inline the procedure when it references proc/var that
  2651. are not in the globally available }
  2652. st:=procdefinition.owner;
  2653. if (st.symtabletype=ObjectSymtable) then
  2654. st:=st.defowner.owner;
  2655. do_inline:=true;
  2656. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2657. (st.symtabletype=globalsymtable) and
  2658. (not st.iscurrentunit) then
  2659. begin
  2660. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2661. do_inline:=false;
  2662. end;
  2663. n:=tcallparanode(parameters);
  2664. while assigned(n) do
  2665. begin
  2666. if n.contains_unsafe_typeconversion then
  2667. begin
  2668. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", invocation parameter contains unsafe type conversion');
  2669. do_inline:=false;
  2670. break;
  2671. end;
  2672. n:=tcallparanode(n.nextpara);
  2673. end;
  2674. if do_inline then
  2675. begin
  2676. result:=pass1_inline;
  2677. exit;
  2678. end;
  2679. end;
  2680. { calculate the parameter info for the procdef }
  2681. if not procdefinition.has_paraloc_info then
  2682. begin
  2683. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2684. procdefinition.has_paraloc_info:=true;
  2685. end;
  2686. { calculate the parameter size needed for this call include varargs if they are available }
  2687. if assigned(varargsparas) then
  2688. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2689. else
  2690. pushedparasize:=procdefinition.requiredargarea;
  2691. { record maximum parameter size used in this proc }
  2692. current_procinfo.allocate_push_parasize(pushedparasize);
  2693. { work trough all parameters to get the register requirements }
  2694. if assigned(left) then
  2695. begin
  2696. tcallparanode(left).det_registers;
  2697. if (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  2698. begin
  2699. { check for stacked parameters }
  2700. check_stack_parameters;
  2701. end;
  2702. end;
  2703. { order parameters }
  2704. order_parameters;
  2705. if assigned(methodpointerinit) then
  2706. firstpass(methodpointerinit);
  2707. if assigned(methodpointerdone) then
  2708. firstpass(methodpointerdone);
  2709. { function result node }
  2710. if assigned(_funcretnode) then
  2711. firstpass(_funcretnode);
  2712. { procedure variable ? }
  2713. if assigned(right) then
  2714. firstpass(right);
  2715. if not (block_type in [bt_const,bt_type]) then
  2716. include(current_procinfo.flags,pi_do_call);
  2717. { implicit finally needed ? }
  2718. if resultdef.needs_inittable and
  2719. not paramanager.ret_in_param(resultdef,procdefinition.proccalloption) and
  2720. not assigned(funcretnode) then
  2721. include(current_procinfo.flags,pi_needs_implicit_finally);
  2722. { get a register for the return value }
  2723. if (not is_void(resultdef)) then
  2724. begin
  2725. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2726. begin
  2727. expectloc:=LOC_REFERENCE;
  2728. end
  2729. else
  2730. { ansi/widestrings must be registered, so we can dispose them }
  2731. if is_ansistring(resultdef) or
  2732. is_widestring(resultdef) then
  2733. begin
  2734. expectloc:=LOC_REFERENCE;
  2735. registersint:=1;
  2736. end
  2737. else
  2738. { we have only to handle the result if it is used }
  2739. if (cnf_return_value_used in callnodeflags) then
  2740. begin
  2741. case resultdef.typ of
  2742. enumdef,
  2743. orddef :
  2744. begin
  2745. if (procdefinition.proctypeoption=potype_constructor) then
  2746. begin
  2747. expectloc:=LOC_REGISTER;
  2748. registersint:=1;
  2749. end
  2750. else
  2751. begin
  2752. expectloc:=LOC_REGISTER;
  2753. if is_64bit(resultdef) then
  2754. registersint:=2
  2755. else
  2756. registersint:=1;
  2757. end;
  2758. end;
  2759. floatdef :
  2760. begin
  2761. expectloc:=LOC_FPUREGISTER;
  2762. {$ifdef cpufpemu}
  2763. if (cs_fp_emulation in current_settings.moduleswitches) then
  2764. registersint:=1
  2765. else
  2766. {$endif cpufpemu}
  2767. {$ifdef m68k}
  2768. if (tfloatdef(resultdef).floattype=s32real) then
  2769. registersint:=1
  2770. else
  2771. {$endif m68k}
  2772. registersfpu:=1;
  2773. end;
  2774. else
  2775. begin
  2776. expectloc:=procdefinition.funcretloc[callerside].loc;
  2777. if (expectloc = LOC_REGISTER) then
  2778. {$ifndef cpu64bit}
  2779. if (resultdef.size > sizeof(aint)) then
  2780. registersint:=2
  2781. else
  2782. {$endif cpu64bit}
  2783. registersint:=1
  2784. else
  2785. registersint:=0;
  2786. end;
  2787. end;
  2788. end
  2789. else
  2790. expectloc:=LOC_VOID;
  2791. end
  2792. else
  2793. expectloc:=LOC_VOID;
  2794. {$ifdef m68k}
  2795. { we need one more address register for virtual calls on m68k }
  2796. if (po_virtualmethod in procdefinition.procoptions) then
  2797. inc(registersint);
  2798. {$endif m68k}
  2799. { a fpu can be used in any procedure !! }
  2800. {$ifdef i386}
  2801. registersfpu:=procdefinition.fpu_used;
  2802. {$endif i386}
  2803. { if this is a call to a method calc the registers }
  2804. if (methodpointer<>nil) then
  2805. begin
  2806. if methodpointer.nodetype<>typen then
  2807. begin
  2808. firstpass(methodpointer);
  2809. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2810. registersint:=max(methodpointer.registersint,registersint);
  2811. {$ifdef SUPPORT_MMX }
  2812. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2813. {$endif SUPPORT_MMX}
  2814. end;
  2815. end;
  2816. { determine the registers of the procedure variable }
  2817. { is this OK for inlined procs also ?? (PM) }
  2818. if assigned(right) then
  2819. begin
  2820. registersfpu:=max(right.registersfpu,registersfpu);
  2821. registersint:=max(right.registersint,registersint);
  2822. {$ifdef SUPPORT_MMX}
  2823. registersmmx:=max(right.registersmmx,registersmmx);
  2824. {$endif SUPPORT_MMX}
  2825. end;
  2826. { determine the registers of the procedure }
  2827. if assigned(left) then
  2828. begin
  2829. registersfpu:=max(left.registersfpu,registersfpu);
  2830. registersint:=max(left.registersint,registersint);
  2831. {$ifdef SUPPORT_MMX}
  2832. registersmmx:=max(left.registersmmx,registersmmx);
  2833. {$endif SUPPORT_MMX}
  2834. end;
  2835. end;
  2836. {$ifdef state_tracking}
  2837. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2838. var hp:Tcallparanode;
  2839. value:Tnode;
  2840. begin
  2841. track_state_pass:=false;
  2842. hp:=Tcallparanode(left);
  2843. while assigned(hp) do
  2844. begin
  2845. if left.track_state_pass(exec_known) then
  2846. begin
  2847. left.resultdef:=nil;
  2848. do_typecheckpass(left);
  2849. end;
  2850. value:=aktstate.find_fact(hp.left);
  2851. if value<>nil then
  2852. begin
  2853. track_state_pass:=true;
  2854. hp.left.destroy;
  2855. hp.left:=value.getcopy;
  2856. do_typecheckpass(hp.left);
  2857. end;
  2858. hp:=Tcallparanode(hp.right);
  2859. end;
  2860. end;
  2861. {$endif}
  2862. function tcallnode.para_count:longint;
  2863. var
  2864. ppn : tcallparanode;
  2865. begin
  2866. result:=0;
  2867. ppn:=tcallparanode(left);
  2868. while assigned(ppn) do
  2869. begin
  2870. if not(assigned(ppn.parasym) and
  2871. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  2872. inc(result);
  2873. ppn:=tcallparanode(ppn.right);
  2874. end;
  2875. end;
  2876. function tcallnode.get_load_methodpointer:tnode;
  2877. var
  2878. newstatement : tstatementnode;
  2879. begin
  2880. if assigned(methodpointerinit) then
  2881. begin
  2882. result:=internalstatements(newstatement);
  2883. addstatement(newstatement,methodpointerinit);
  2884. addstatement(newstatement,methodpointer);
  2885. addstatement(newstatement,methodpointerdone);
  2886. methodpointerinit:=nil;
  2887. methodpointer:=nil;
  2888. methodpointerdone:=nil;
  2889. end
  2890. else
  2891. begin
  2892. result:=methodpointer;
  2893. methodpointer:=nil;
  2894. end;
  2895. end;
  2896. function tcallnode.docompare(p: tnode): boolean;
  2897. begin
  2898. docompare :=
  2899. inherited docompare(p) and
  2900. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2901. (procdefinition = tcallnode(p).procdefinition) and
  2902. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2903. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  2904. (equal_defs(typedef,tcallnode(p).typedef))) or
  2905. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  2906. end;
  2907. procedure tcallnode.printnodedata(var t:text);
  2908. begin
  2909. if assigned(procdefinition) and
  2910. (procdefinition.typ=procdef) then
  2911. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2912. else
  2913. begin
  2914. if assigned(symtableprocentry) then
  2915. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2916. else
  2917. writeln(t,printnodeindention,'proc = <nil>');
  2918. end;
  2919. if assigned(methodpointer) then
  2920. begin
  2921. writeln(t,printnodeindention,'methodpointer =');
  2922. printnode(t,methodpointer);
  2923. end;
  2924. if assigned(methodpointerinit) then
  2925. begin
  2926. writeln(t,printnodeindention,'methodpointerinit =');
  2927. printnode(t,methodpointerinit);
  2928. end;
  2929. if assigned(methodpointerdone) then
  2930. begin
  2931. writeln(t,printnodeindention,'methodpointerdone =');
  2932. printnode(t,methodpointerdone);
  2933. end;
  2934. if assigned(right) then
  2935. begin
  2936. writeln(t,printnodeindention,'right =');
  2937. printnode(t,right);
  2938. end;
  2939. if assigned(left) then
  2940. begin
  2941. writeln(t,printnodeindention,'left =');
  2942. printnode(t,left);
  2943. end;
  2944. end;
  2945. begin
  2946. ccallnode:=tcallnode;
  2947. ccallparanode:=tcallparanode;
  2948. end.