cg386cal.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for in call nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386cal;
  19. interface
  20. { $define AnsiStrRef}
  21. uses
  22. symtable,tree;
  23. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  24. push_from_left_to_right,inlined,dword_align : boolean;para_offset : longint);
  25. procedure secondcalln(var p : ptree);
  26. procedure secondprocinline(var p : ptree);
  27. implementation
  28. uses
  29. globtype,systems,
  30. cobjects,verbose,globals,
  31. symconst,aasm,types,
  32. {$ifdef GDB}
  33. gdb,
  34. {$endif GDB}
  35. hcodegen,temp_gen,pass_2,
  36. cpubase,cpuasm,
  37. cgai386,tgeni386,cg386ld;
  38. {*****************************************************************************
  39. SecondCallParaN
  40. *****************************************************************************}
  41. procedure secondcallparan(var p : ptree;defcoll : pparaitem;
  42. push_from_left_to_right,inlined,dword_align : boolean;para_offset : longint);
  43. procedure maybe_push_high;
  44. begin
  45. { open array ? }
  46. { defcoll^.data can be nil for read/write }
  47. if assigned(defcoll^.paratype.def) and
  48. push_high_param(defcoll^.paratype.def) then
  49. begin
  50. if assigned(p^.hightree) then
  51. begin
  52. secondpass(p^.hightree);
  53. { this is a longint anyway ! }
  54. push_value_para(p^.hightree,inlined,para_offset,4);
  55. end
  56. else
  57. internalerror(432645);
  58. end;
  59. end;
  60. var
  61. otlabel,oflabel : pasmlabel;
  62. align : longint;
  63. { temporary variables: }
  64. tempdeftype : tdeftype;
  65. r : preference;
  66. begin
  67. { push from left to right if specified }
  68. if push_from_left_to_right and assigned(p^.right) then
  69. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  70. inlined,dword_align,para_offset);
  71. otlabel:=truelabel;
  72. oflabel:=falselabel;
  73. getlabel(truelabel);
  74. getlabel(falselabel);
  75. secondpass(p^.left);
  76. { filter array constructor with c styled args }
  77. if is_array_constructor(p^.left^.resulttype) and p^.left^.cargs then
  78. begin
  79. { nothing, everything is already pushed }
  80. end
  81. { in codegen.handleread.. defcoll^.data is set to nil }
  82. else if assigned(defcoll^.paratype.def) and
  83. (defcoll^.paratype.def^.deftype=formaldef) then
  84. begin
  85. { allow @var }
  86. inc(pushedparasize,4);
  87. if p^.left^.treetype=addrn then
  88. begin
  89. { always a register }
  90. if inlined then
  91. begin
  92. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  93. emit_reg_ref(A_MOV,S_L,
  94. p^.left^.location.register,r);
  95. end
  96. else
  97. emit_reg(A_PUSH,S_L,p^.left^.location.register);
  98. ungetregister32(p^.left^.location.register);
  99. end
  100. else
  101. begin
  102. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  103. CGMessage(type_e_mismatch)
  104. else
  105. begin
  106. if inlined then
  107. begin
  108. {$ifndef noAllocEdi}
  109. getexplicitregister32(R_EDI);
  110. {$endif noAllocEdi}
  111. emit_ref_reg(A_LEA,S_L,
  112. newreference(p^.left^.location.reference),R_EDI);
  113. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  114. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  115. {$ifndef noAllocEdi}
  116. ungetregister32(R_EDI);
  117. {$endif noAllocEdi}
  118. end
  119. else
  120. emitpushreferenceaddr(p^.left^.location.reference);
  121. del_reference(p^.left^.location.reference);
  122. end;
  123. end;
  124. end
  125. { handle call by reference parameter }
  126. else if (defcoll^.paratyp=vs_var) then
  127. begin
  128. if (p^.left^.location.loc<>LOC_REFERENCE) then
  129. CGMessage(cg_e_var_must_be_reference);
  130. maybe_push_high;
  131. inc(pushedparasize,4);
  132. if inlined then
  133. begin
  134. {$ifndef noAllocEdi}
  135. getexplicitregister32(R_EDI);
  136. {$endif noAllocEdi}
  137. emit_ref_reg(A_LEA,S_L,
  138. newreference(p^.left^.location.reference),R_EDI);
  139. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  140. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  141. {$ifndef noAllocEdi}
  142. ungetregister32(R_EDI);
  143. {$endif noAllocEdi}
  144. end
  145. else
  146. emitpushreferenceaddr(p^.left^.location.reference);
  147. del_reference(p^.left^.location.reference);
  148. end
  149. else
  150. begin
  151. tempdeftype:=p^.resulttype^.deftype;
  152. if tempdeftype=filedef then
  153. CGMessage(cg_e_file_must_call_by_reference);
  154. { open array must always push the address, this is needed to
  155. also push addr of small arrays (PFV) }
  156. if (assigned(defcoll^.paratype.def) and
  157. is_open_array(defcoll^.paratype.def)) or
  158. push_addr_param(p^.resulttype) then
  159. begin
  160. maybe_push_high;
  161. inc(pushedparasize,4);
  162. if inlined then
  163. begin
  164. {$ifndef noAllocEdi}
  165. getexplicitregister32(R_EDI);
  166. {$endif noAllocEdi}
  167. emit_ref_reg(A_LEA,S_L,
  168. newreference(p^.left^.location.reference),R_EDI);
  169. r:=new_reference(procinfo^.framepointer,para_offset-pushedparasize);
  170. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  171. {$ifndef noAllocEdi}
  172. ungetregister32(R_EDI);
  173. {$endif noAllocEdi}
  174. end
  175. else
  176. emitpushreferenceaddr(p^.left^.location.reference);
  177. del_reference(p^.left^.location.reference);
  178. end
  179. else
  180. begin
  181. align:=target_os.stackalignment;
  182. if dword_align then
  183. align:=4;
  184. push_value_para(p^.left,inlined,para_offset,align);
  185. end;
  186. end;
  187. truelabel:=otlabel;
  188. falselabel:=oflabel;
  189. { push from right to left }
  190. if not push_from_left_to_right and assigned(p^.right) then
  191. secondcallparan(p^.right,pparaitem(defcoll^.next),push_from_left_to_right,
  192. inlined,dword_align,para_offset);
  193. end;
  194. {*****************************************************************************
  195. SecondCallN
  196. *****************************************************************************}
  197. procedure secondcalln(var p : ptree);
  198. var
  199. unusedregisters : tregisterset;
  200. usablecount : byte;
  201. pushed : tpushed;
  202. hr,funcretref : treference;
  203. hregister,hregister2 : tregister;
  204. oldpushedparasize : longint;
  205. { true if ESI must be loaded again after the subroutine }
  206. loadesi : boolean;
  207. { true if a virtual method must be called directly }
  208. no_virtual_call : boolean;
  209. { true if we produce a con- or destrutor in a call }
  210. is_con_or_destructor : boolean;
  211. { true if a constructor is called again }
  212. extended_new : boolean;
  213. { adress returned from an I/O-error }
  214. iolabel : pasmlabel;
  215. { lexlevel count }
  216. i : longint;
  217. { help reference pointer }
  218. r : preference;
  219. hp,
  220. pp,params : ptree;
  221. inlined : boolean;
  222. inlinecode : ptree;
  223. para_offset : longint;
  224. { instruction for alignement correction }
  225. { corr : paicpu;}
  226. { we must pop this size also after !! }
  227. { must_pop : boolean; }
  228. pop_size : longint;
  229. pop_allowed : boolean;
  230. label
  231. dont_call;
  232. begin
  233. reset_reference(p^.location.reference);
  234. extended_new:=false;
  235. iolabel:=nil;
  236. inlinecode:=nil;
  237. inlined:=false;
  238. loadesi:=true;
  239. no_virtual_call:=false;
  240. unusedregisters:=unused;
  241. usablecount:=usablereg32;
  242. if not assigned(p^.procdefinition) then
  243. exit;
  244. if (pocall_inline in p^.procdefinition^.proccalloptions) then
  245. begin
  246. inlined:=true;
  247. inlinecode:=p^.right;
  248. { set it to the same lexical level as the local symtable, becuase
  249. the para's are stored there }
  250. pprocdef(p^.procdefinition)^.parast^.symtablelevel:=aktprocsym^.definition^.localst^.symtablelevel;
  251. if assigned(p^.left) then
  252. inlinecode^.para_offset:=gettempofsizepersistant(inlinecode^.para_size);
  253. pprocdef(p^.procdefinition)^.parast^.address_fixup:=inlinecode^.para_offset;
  254. {$ifdef extdebug}
  255. Comment(V_debug,
  256. 'inlined parasymtable is at offset '
  257. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup));
  258. exprasmlist^.concat(new(pai_asm_comment,init(
  259. strpnew('inlined parasymtable is at offset '
  260. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup)))));
  261. {$endif extdebug}
  262. p^.right:=nil;
  263. { disable further inlining of the same proc
  264. in the args }
  265. {$ifdef INCLUDEOK}
  266. exclude(p^.procdefinition^.proccalloptions,pocall_inline);
  267. {$else}
  268. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions-[pocall_inline];
  269. {$endif}
  270. end;
  271. { only if no proc var }
  272. if not(assigned(p^.right)) then
  273. is_con_or_destructor:=(p^.procdefinition^.proctypeoption in [potype_constructor,potype_destructor]);
  274. { proc variables destroy all registers }
  275. if (p^.right=nil) and
  276. { virtual methods too }
  277. not(po_virtualmethod in p^.procdefinition^.procoptions) then
  278. begin
  279. if (cs_check_io in aktlocalswitches) and
  280. (po_iocheck in p^.procdefinition^.procoptions) and
  281. not(po_iocheck in aktprocsym^.definition^.procoptions) then
  282. begin
  283. getlabel(iolabel);
  284. emitlab(iolabel);
  285. end
  286. else
  287. iolabel:=nil;
  288. { save all used registers }
  289. pushusedregisters(pushed,pprocdef(p^.procdefinition)^.usedregisters);
  290. { give used registers through }
  291. usedinproc:=usedinproc or pprocdef(p^.procdefinition)^.usedregisters;
  292. end
  293. else
  294. begin
  295. pushusedregisters(pushed,$ff);
  296. usedinproc:=$ff;
  297. { no IO check for methods and procedure variables }
  298. iolabel:=nil;
  299. end;
  300. { generate the code for the parameter and push them }
  301. oldpushedparasize:=pushedparasize;
  302. pushedparasize:=0;
  303. pop_size:=0;
  304. { no inc esp for inlined procedure
  305. and for objects constructors PM }
  306. if inlined or
  307. ((p^.right=nil) and
  308. (p^.procdefinition^.proctypeoption=potype_constructor) and
  309. { quick'n'dirty check if it is a class or an object }
  310. (p^.resulttype^.deftype=orddef)) then
  311. pop_allowed:=false
  312. else
  313. pop_allowed:=true;
  314. if pop_allowed then
  315. begin
  316. { Old pushedsize aligned on 4 ? }
  317. i:=oldpushedparasize and 3;
  318. if i>0 then
  319. inc(pop_size,4-i);
  320. { This parasize aligned on 4 ? }
  321. i:=p^.procdefinition^.para_size and 3;
  322. if i>0 then
  323. inc(pop_size,4-i);
  324. { insert the opcode and update pushedparasize }
  325. { never push 4 or more !! }
  326. pop_size:=pop_size mod 4;
  327. if pop_size>0 then
  328. begin
  329. inc(pushedparasize,pop_size);
  330. emit_const_reg(A_SUB,S_L,pop_size,R_ESP);
  331. {$ifdef GDB}
  332. if (cs_debuginfo in aktmoduleswitches) and
  333. (exprasmlist^.first=exprasmlist^.last) then
  334. exprasmlist^.concat(new(pai_force_line,init));
  335. {$endif GDB}
  336. end;
  337. end;
  338. if (p^.resulttype<>pdef(voiddef)) and
  339. ret_in_param(p^.resulttype) then
  340. begin
  341. funcretref.symbol:=nil;
  342. {$ifdef test_dest_loc}
  343. if dest_loc_known and (dest_loc_tree=p) and
  344. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  345. begin
  346. funcretref:=dest_loc.reference;
  347. if assigned(dest_loc.reference.symbol) then
  348. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  349. in_dest_loc:=true;
  350. end
  351. else
  352. {$endif test_dest_loc}
  353. if inlined then
  354. begin
  355. reset_reference(funcretref);
  356. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.rettype.def^.size);
  357. funcretref.base:=procinfo^.framepointer;
  358. end
  359. else
  360. gettempofsizereference(p^.procdefinition^.rettype.def^.size,funcretref);
  361. end;
  362. if assigned(p^.left) then
  363. begin
  364. { be found elsewhere }
  365. if inlined then
  366. para_offset:=pprocdef(p^.procdefinition)^.parast^.address_fixup+
  367. pprocdef(p^.procdefinition)^.parast^.datasize
  368. else
  369. para_offset:=0;
  370. if assigned(p^.right) then
  371. secondcallparan(p^.left,pparaitem(pabstractprocdef(p^.right^.resulttype)^.para^.first),
  372. (pocall_leftright in p^.procdefinition^.proccalloptions),
  373. inlined,
  374. (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  375. (pocall_stdcall in p^.procdefinition^.proccalloptions),
  376. para_offset)
  377. else
  378. secondcallparan(p^.left,pparaitem(p^.procdefinition^.para^.first),
  379. (pocall_leftright in p^.procdefinition^.proccalloptions),
  380. inlined,
  381. (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  382. (pocall_stdcall in p^.procdefinition^.proccalloptions),
  383. para_offset);
  384. end;
  385. params:=p^.left;
  386. p^.left:=nil;
  387. if inlined then
  388. inlinecode^.retoffset:=gettempofsizepersistant(4);
  389. if ret_in_param(p^.resulttype) then
  390. begin
  391. { This must not be counted for C code
  392. complex return address is removed from stack
  393. by function itself ! }
  394. {$ifdef OLD_C_STACK}
  395. inc(pushedparasize,4); { lets try without it PM }
  396. {$endif not OLD_C_STACK}
  397. if inlined then
  398. begin
  399. {$ifndef noAllocEdi}
  400. getexplicitregister32(R_EDI);
  401. {$endif noAllocEdi}
  402. emit_ref_reg(A_LEA,S_L,
  403. newreference(funcretref),R_EDI);
  404. r:=new_reference(procinfo^.framepointer,inlinecode^.retoffset);
  405. emit_reg_ref(A_MOV,S_L,R_EDI,r);
  406. {$ifndef noAllocEdi}
  407. ungetregister32(R_EDI);
  408. {$endif noAllocEdi}
  409. end
  410. else
  411. emitpushreferenceaddr(funcretref);
  412. end;
  413. { procedure variable ? }
  414. if (p^.right=nil) then
  415. begin
  416. { overloaded operator have no symtable }
  417. { push self }
  418. if assigned(p^.symtable) and
  419. (p^.symtable^.symtabletype=withsymtable) then
  420. begin
  421. { dirty trick to avoid the secondcall below }
  422. p^.methodpointer:=genzeronode(callparan);
  423. p^.methodpointer^.location.loc:=LOC_REGISTER;
  424. p^.methodpointer^.location.register:=R_ESI;
  425. { ARGHHH this is wrong !!!
  426. if we can init from base class for a child
  427. class that the wrong VMT will be
  428. transfered to constructor !! }
  429. p^.methodpointer^.resulttype:=
  430. ptree(pwithsymtable(p^.symtable)^.withnode)^.left^.resulttype;
  431. { change dispose type !! }
  432. p^.disposetyp:=dt_mbleft_and_method;
  433. { make a reference }
  434. new(r);
  435. reset_reference(r^);
  436. { if assigned(ptree(pwithsymtable(p^.symtable)^.withnode)^.pref) then
  437. begin
  438. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.pref^;
  439. end
  440. else
  441. begin
  442. r^.offset:=p^.symtable^.datasize;
  443. r^.base:=procinfo^.framepointer;
  444. end; }
  445. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^;
  446. if (not pwithsymtable(p^.symtable)^.direct_with) or
  447. pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  448. emit_ref_reg(A_MOV,S_L,r,R_ESI)
  449. else
  450. emit_ref_reg(A_LEA,S_L,r,R_ESI);
  451. end;
  452. { push self }
  453. if assigned(p^.symtable) and
  454. ((p^.symtable^.symtabletype=objectsymtable) or
  455. (p^.symtable^.symtabletype=withsymtable)) then
  456. begin
  457. if assigned(p^.methodpointer) then
  458. begin
  459. {
  460. if p^.methodpointer^.resulttype=classrefdef then
  461. begin
  462. two possibilities:
  463. 1. constructor
  464. 2. class method
  465. end
  466. else }
  467. begin
  468. case p^.methodpointer^.treetype of
  469. typen:
  470. begin
  471. { direct call to inherited method }
  472. if (po_abstractmethod in p^.procdefinition^.procoptions) then
  473. begin
  474. CGMessage(cg_e_cant_call_abstract_method);
  475. goto dont_call;
  476. end;
  477. { generate no virtual call }
  478. no_virtual_call:=true;
  479. if (sp_static in p^.symtableprocentry^.symoptions) then
  480. begin
  481. { well lets put the VMT address directly into ESI }
  482. { it is kind of dirty but that is the simplest }
  483. { way to accept virtual static functions (PM) }
  484. loadesi:=true;
  485. { if no VMT just use $0 bug0214 PM }
  486. if not(oo_has_vmt in pobjectdef(p^.methodpointer^.resulttype)^.objectoptions) then
  487. emit_const_reg(A_MOV,S_L,0,R_ESI)
  488. else
  489. begin
  490. emit_sym_ofs_reg(A_MOV,S_L,
  491. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname),
  492. 0,R_ESI);
  493. end;
  494. { emit_reg(A_PUSH,S_L,R_ESI);
  495. this is done below !! }
  496. end
  497. else
  498. { this is a member call, so ESI isn't modfied }
  499. loadesi:=false;
  500. { a class destructor needs a flag }
  501. if pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  502. assigned(aktprocsym) and
  503. (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  504. begin
  505. push_int(0);
  506. emit_reg(A_PUSH,S_L,R_ESI);
  507. end;
  508. if not(is_con_or_destructor and
  509. pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  510. assigned(aktprocsym) and
  511. (aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor])
  512. ) then
  513. emit_reg(A_PUSH,S_L,R_ESI);
  514. { if an inherited con- or destructor should be }
  515. { called in a con- or destructor then a warning }
  516. { will be made }
  517. { con- and destructors need a pointer to the vmt }
  518. if is_con_or_destructor and
  519. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) and
  520. assigned(aktprocsym) then
  521. begin
  522. if not(aktprocsym^.definition^.proctypeoption in
  523. [potype_constructor,potype_destructor]) then
  524. CGMessage(cg_w_member_cd_call_from_method);
  525. end;
  526. { class destructors get there flag below }
  527. if is_con_or_destructor and
  528. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  529. assigned(aktprocsym) and
  530. (aktprocsym^.definition^.proctypeoption=potype_destructor)) then
  531. push_int(0);
  532. end;
  533. hnewn:
  534. begin
  535. { extended syntax of new }
  536. { ESI must be zero }
  537. emit_reg_reg(A_XOR,S_L,R_ESI,R_ESI);
  538. emit_reg(A_PUSH,S_L,R_ESI);
  539. { insert the vmt }
  540. emit_sym(A_PUSH,S_L,
  541. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  542. extended_new:=true;
  543. end;
  544. hdisposen:
  545. begin
  546. secondpass(p^.methodpointer);
  547. { destructor with extended syntax called from dispose }
  548. { hdisposen always deliver LOC_REFERENCE }
  549. emit_ref_reg(A_LEA,S_L,
  550. newreference(p^.methodpointer^.location.reference),R_ESI);
  551. del_reference(p^.methodpointer^.location.reference);
  552. emit_reg(A_PUSH,S_L,R_ESI);
  553. emit_sym(A_PUSH,S_L,
  554. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  555. end;
  556. else
  557. begin
  558. { call to an instance member }
  559. if (p^.symtable^.symtabletype<>withsymtable) then
  560. begin
  561. secondpass(p^.methodpointer);
  562. case p^.methodpointer^.location.loc of
  563. LOC_CREGISTER,
  564. LOC_REGISTER:
  565. begin
  566. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  567. ungetregister32(p^.methodpointer^.location.register);
  568. end;
  569. else
  570. begin
  571. if (p^.methodpointer^.resulttype^.deftype=classrefdef) or
  572. ((p^.methodpointer^.resulttype^.deftype=objectdef) and
  573. pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  574. emit_ref_reg(A_MOV,S_L,
  575. newreference(p^.methodpointer^.location.reference),R_ESI)
  576. else
  577. emit_ref_reg(A_LEA,S_L,
  578. newreference(p^.methodpointer^.location.reference),R_ESI);
  579. del_reference(p^.methodpointer^.location.reference);
  580. end;
  581. end;
  582. end;
  583. { when calling a class method, we have to load ESI with the VMT !
  584. But, not for a class method via self }
  585. if not(po_containsself in p^.procdefinition^.procoptions) then
  586. begin
  587. if (po_classmethod in p^.procdefinition^.procoptions) and
  588. not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
  589. begin
  590. { class method needs current VMT }
  591. new(r);
  592. reset_reference(r^);
  593. r^.base:=R_ESI;
  594. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  595. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  596. end;
  597. { direct call to destructor: don't remove data! }
  598. if (p^.procdefinition^.proctypeoption=potype_destructor) and
  599. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  600. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  601. emit_const(A_PUSH,S_L,1);
  602. { direct call to class constructor, don't allocate memory }
  603. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  604. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  605. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  606. emit_const(A_PUSH,S_L,0)
  607. else
  608. emit_reg(A_PUSH,S_L,R_ESI);
  609. end;
  610. if is_con_or_destructor then
  611. begin
  612. { classes don't get a VMT pointer pushed }
  613. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  614. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  615. begin
  616. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  617. begin
  618. { it's no bad idea, to insert the VMT }
  619. emit_sym(A_PUSH,S_L,newasmsymbol(
  620. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname));
  621. end
  622. { destructors haven't to dispose the instance, if this is }
  623. { a direct call }
  624. else
  625. push_int(0);
  626. end;
  627. end;
  628. end;
  629. end;
  630. end;
  631. end
  632. else
  633. begin
  634. if (po_classmethod in p^.procdefinition^.procoptions) and
  635. not(
  636. assigned(aktprocsym) and
  637. (po_classmethod in aktprocsym^.definition^.procoptions)
  638. ) then
  639. begin
  640. { class method needs current VMT }
  641. new(r);
  642. reset_reference(r^);
  643. r^.base:=R_ESI;
  644. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  645. emit_ref_reg(A_MOV,S_L,r,R_ESI);
  646. end
  647. else
  648. begin
  649. { member call, ESI isn't modified }
  650. loadesi:=false;
  651. end;
  652. emit_reg(A_PUSH,S_L,R_ESI);
  653. { but a con- or destructor here would probably almost }
  654. { always be placed wrong }
  655. if is_con_or_destructor then
  656. begin
  657. CGMessage(cg_w_member_cd_call_from_method);
  658. push_int(0);
  659. end;
  660. end;
  661. end;
  662. { push base pointer ?}
  663. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  664. ((pprocdef(p^.procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  665. begin
  666. { if we call a nested function in a method, we must }
  667. { push also SELF! }
  668. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  669. { access }
  670. {
  671. begin
  672. loadesi:=false;
  673. emit_reg(A_PUSH,S_L,R_ESI);
  674. end;
  675. }
  676. if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  677. begin
  678. new(r);
  679. reset_reference(r^);
  680. r^.offset:=procinfo^.framepointer_offset;
  681. r^.base:=procinfo^.framepointer;
  682. emit_ref(A_PUSH,S_L,r)
  683. end
  684. { this is only true if the difference is one !!
  685. but it cannot be more !! }
  686. else if (lexlevel=pprocdef(p^.procdefinition)^.parast^.symtablelevel-1) then
  687. begin
  688. emit_reg(A_PUSH,S_L,procinfo^.framepointer)
  689. end
  690. else if (lexlevel>pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  691. begin
  692. hregister:=getregister32;
  693. new(r);
  694. reset_reference(r^);
  695. r^.offset:=procinfo^.framepointer_offset;
  696. r^.base:=procinfo^.framepointer;
  697. emit_ref_reg(A_MOV,S_L,r,hregister);
  698. for i:=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  699. begin
  700. new(r);
  701. reset_reference(r^);
  702. {we should get the correct frame_pointer_offset at each level
  703. how can we do this !!! }
  704. r^.offset:=procinfo^.framepointer_offset;
  705. r^.base:=hregister;
  706. emit_ref_reg(A_MOV,S_L,r,hregister);
  707. end;
  708. emit_reg(A_PUSH,S_L,hregister);
  709. ungetregister32(hregister);
  710. end
  711. else
  712. internalerror(25000);
  713. end;
  714. if (po_virtualmethod in p^.procdefinition^.procoptions) and
  715. not(no_virtual_call) then
  716. begin
  717. { static functions contain the vmt_address in ESI }
  718. { also class methods }
  719. { Here it is quite tricky because it also depends }
  720. { on the methodpointer PM }
  721. if assigned(aktprocsym) then
  722. begin
  723. if (((sp_static in aktprocsym^.symoptions) or
  724. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  725. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  726. or
  727. (po_staticmethod in p^.procdefinition^.procoptions) or
  728. (p^.procdefinition^.proctypeoption=potype_constructor) or
  729. { ESI is loaded earlier }
  730. (po_classmethod in p^.procdefinition^.procoptions) then
  731. begin
  732. new(r);
  733. reset_reference(r^);
  734. r^.base:=R_ESI;
  735. end
  736. else
  737. begin
  738. new(r);
  739. reset_reference(r^);
  740. r^.base:=R_ESI;
  741. { this is one point where we need vmt_offset (PM) }
  742. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  743. {$ifndef noAllocEdi}
  744. getexplicitregister32(R_EDI);
  745. {$endif noAllocEdi}
  746. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  747. new(r);
  748. reset_reference(r^);
  749. r^.base:=R_EDI;
  750. end;
  751. end
  752. else
  753. { aktprocsym should be assigned, also in main program }
  754. internalerror(12345);
  755. {
  756. begin
  757. new(r);
  758. reset_reference(r^);
  759. r^.base:=R_ESI;
  760. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  761. new(r);
  762. reset_reference(r^);
  763. r^.base:=R_EDI;
  764. end;
  765. }
  766. if pprocdef(p^.procdefinition)^.extnumber=-1 then
  767. internalerror(44584);
  768. r^.offset:=pprocdef(p^.procdefinition)^._class^.vmtmethodoffset(pprocdef(p^.procdefinition)^.extnumber);
  769. {$ifndef TESTOBJEXT}
  770. if (cs_check_range in aktlocalswitches) then
  771. begin
  772. emit_reg(A_PUSH,S_L,r^.base);
  773. emitcall('FPC_CHECK_OBJECT');
  774. end;
  775. {$else TESTOBJEXT}
  776. if (cs_check_range in aktlocalswitches) then
  777. begin
  778. emit_sym(A_PUSH,S_L,
  779. newasmsymbol(pprocdef(p^.procdefinition)^._class^.vmt_mangledname));
  780. emit_reg(A_PUSH,S_L,r^.base);
  781. emitcall('FPC_CHECK_OBJECT_EXT');
  782. end;
  783. {$endif TESTOBJEXT}
  784. emit_ref(A_CALL,S_NO,r);
  785. {$ifndef noAllocEdi}
  786. ungetregister32(R_EDI);
  787. {$endif noAllocEdi}
  788. end
  789. else if not inlined then
  790. emitcall(pprocdef(p^.procdefinition)^.mangledname)
  791. else { inlined proc }
  792. { inlined code is in inlinecode }
  793. begin
  794. { set poinline again }
  795. {$ifdef INCLUDEOK}
  796. include(p^.procdefinition^.proccalloptions,pocall_inline);
  797. {$else}
  798. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions+[pocall_inline];
  799. {$endif}
  800. { process the inlinecode }
  801. secondpass(inlinecode);
  802. { free the args }
  803. ungetpersistanttemp(pprocdef(p^.procdefinition)^.parast^.address_fixup);
  804. end;
  805. end
  806. else
  807. { now procedure variable case }
  808. begin
  809. secondpass(p^.right);
  810. { procedure of object? }
  811. if (po_methodpointer in p^.procdefinition^.procoptions) then
  812. begin
  813. { method pointer can't be in a register }
  814. hregister:=R_NO;
  815. { do some hacking if we call a method pointer }
  816. { which is a class member }
  817. { else ESI is overwritten ! }
  818. if (p^.right^.location.reference.base=R_ESI) or
  819. (p^.right^.location.reference.index=R_ESI) then
  820. begin
  821. del_reference(p^.right^.location.reference);
  822. {$ifndef noAllocEdi}
  823. getexplicitregister32(R_EDI);
  824. {$endif noAllocEdi}
  825. emit_ref_reg(A_MOV,S_L,
  826. newreference(p^.right^.location.reference),R_EDI);
  827. hregister:=R_EDI;
  828. end;
  829. { load self, but not if it's already explicitly pushed }
  830. if not(po_containsself in p^.procdefinition^.procoptions) then
  831. begin
  832. { load ESI }
  833. inc(p^.right^.location.reference.offset,4);
  834. emit_ref_reg(A_MOV,S_L,
  835. newreference(p^.right^.location.reference),R_ESI);
  836. dec(p^.right^.location.reference.offset,4);
  837. { push self pointer }
  838. emit_reg(A_PUSH,S_L,R_ESI);
  839. end;
  840. if hregister=R_NO then
  841. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))
  842. else
  843. begin
  844. {$ifndef noAllocEdi}
  845. ungetregister32(hregister);
  846. {$else noAllocEdi}
  847. { the same code, the previous line is just to }
  848. { indicate EDI actually is deallocated if allocated }
  849. { above (JM) }
  850. ungetregister32(hregister);
  851. {$endif noAllocEdi}
  852. emit_reg(A_CALL,S_NO,hregister);
  853. end;
  854. del_reference(p^.right^.location.reference);
  855. end
  856. else
  857. begin
  858. case p^.right^.location.loc of
  859. LOC_REGISTER,LOC_CREGISTER:
  860. begin
  861. emit_reg(A_CALL,S_NO,p^.right^.location.register);
  862. ungetregister32(p^.right^.location.register);
  863. end
  864. else
  865. emit_ref(A_CALL,S_NO,newreference(p^.right^.location.reference));
  866. del_reference(p^.right^.location.reference);
  867. end;
  868. end;
  869. end;
  870. { this was only for normal functions
  871. displaced here so we also get
  872. it to work for procvars PM }
  873. if (not inlined) and (pocall_clearstack in p^.procdefinition^.proccalloptions) then
  874. begin
  875. { consider the alignment with the rest (PM) }
  876. inc(pushedparasize,pop_size);
  877. pop_size:=0;
  878. { better than an add on all processors }
  879. if pushedparasize=4 then
  880. begin
  881. {$ifndef noAllocEdi}
  882. getexplicitregister32(R_EDI);
  883. {$endif noAllocEdi}
  884. emit_reg(A_POP,S_L,R_EDI);
  885. {$ifndef noAllocEdi}
  886. ungetregister32(R_EDI);
  887. {$endif noAllocEdi}
  888. end
  889. { the pentium has two pipes and pop reg is pairable }
  890. { but the registers must be different! }
  891. else if (pushedparasize=8) and
  892. not(cs_littlesize in aktglobalswitches) and
  893. (aktoptprocessor=ClassP5) and
  894. (procinfo^._class=nil) then
  895. begin
  896. {$ifndef noAllocEdi}
  897. getexplicitregister32(R_EDI);
  898. {$endif noAllocEdi}
  899. emit_reg(A_POP,S_L,R_EDI);
  900. {$ifndef noAllocEdi}
  901. ungetregister32(R_EDI);
  902. {$endif noAllocEdi}
  903. {$ifndef noAllocEdi}
  904. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  905. {$endif noAllocEdi}
  906. emit_reg(A_POP,S_L,R_ESI);
  907. {$ifndef noAllocEdi}
  908. exprasmlist^.concat(new(pairegalloc,alloc(R_ESI)));
  909. {$endif noAllocEdi}
  910. end
  911. else if pushedparasize<>0 then
  912. emit_const_reg(A_ADD,S_L,pushedparasize,R_ESP);
  913. end;
  914. dont_call:
  915. pushedparasize:=oldpushedparasize;
  916. unused:=unusedregisters;
  917. usablereg32:=usablecount;
  918. {$ifdef TEMPREGDEBUG}
  919. testregisters32;
  920. {$endif TEMPREGDEBUG}
  921. { a constructor could be a function with boolean result }
  922. { if calling constructor called fail we
  923. must jump directly to quickexitlabel PM
  924. but only if it is a call of an inherited constructor }
  925. if (p^.right=nil) and
  926. (p^.procdefinition^.proctypeoption=potype_constructor) and
  927. assigned(p^.methodpointer) and
  928. (p^.methodpointer^.treetype=typen) and
  929. (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  930. begin
  931. emitjmp(C_Z,faillabel);
  932. end;
  933. { handle function results }
  934. { structured results are easy to handle.... }
  935. { needed also when result_no_used !! }
  936. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  937. begin
  938. p^.location.loc:=LOC_MEM;
  939. p^.location.reference.symbol:=nil;
  940. p^.location.reference:=funcretref;
  941. end;
  942. { we have only to handle the result if it is used, but }
  943. { ansi/widestrings must be registered, so we can dispose them }
  944. if (p^.resulttype<>pdef(voiddef)) and (p^.return_value_used or
  945. is_ansistring(p^.resulttype) or is_widestring(p^.resulttype)) then
  946. begin
  947. { a contructor could be a function with boolean result }
  948. if (p^.right=nil) and
  949. (p^.procdefinition^.proctypeoption=potype_constructor) and
  950. { quick'n'dirty check if it is a class or an object }
  951. (p^.resulttype^.deftype=orddef) then
  952. begin
  953. { this fails if popsize > 0 PM }
  954. p^.location.loc:=LOC_FLAGS;
  955. p^.location.resflags:=F_NE;
  956. if extended_new then
  957. begin
  958. {$ifdef test_dest_loc}
  959. if dest_loc_known and (dest_loc_tree=p) then
  960. mov_reg_to_dest(p,S_L,R_EAX)
  961. else
  962. {$endif test_dest_loc}
  963. begin
  964. hregister:=getexplicitregister32(R_EAX);
  965. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  966. p^.location.register:=hregister;
  967. end;
  968. end;
  969. end
  970. { structed results are easy to handle.... }
  971. else if ret_in_param(p^.resulttype) then
  972. begin
  973. {p^.location.loc:=LOC_MEM;
  974. stringdispose(p^.location.reference.symbol);
  975. p^.location.reference:=funcretref;
  976. already done above (PM) }
  977. end
  978. else
  979. begin
  980. if (p^.resulttype^.deftype in [orddef,enumdef]) then
  981. begin
  982. p^.location.loc:=LOC_REGISTER;
  983. case p^.resulttype^.size of
  984. 4 :
  985. begin
  986. {$ifdef test_dest_loc}
  987. if dest_loc_known and (dest_loc_tree=p) then
  988. mov_reg_to_dest(p,S_L,R_EAX)
  989. else
  990. {$endif test_dest_loc}
  991. begin
  992. hregister:=getexplicitregister32(R_EAX);
  993. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  994. p^.location.register:=hregister;
  995. end;
  996. end;
  997. 1 :
  998. begin
  999. {$ifdef test_dest_loc}
  1000. if dest_loc_known and (dest_loc_tree=p) then
  1001. mov_reg_to_dest(p,S_B,R_AL)
  1002. else
  1003. {$endif test_dest_loc}
  1004. begin
  1005. hregister:=getexplicitregister32(R_EAX);
  1006. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1007. p^.location.register:=reg32toreg8(hregister);
  1008. end;
  1009. end;
  1010. 2 :
  1011. begin
  1012. {$ifdef test_dest_loc}
  1013. if dest_loc_known and (dest_loc_tree=p) then
  1014. mov_reg_to_dest(p,S_W,R_AX)
  1015. else
  1016. {$endif test_dest_loc}
  1017. begin
  1018. hregister:=getexplicitregister32(R_EAX);
  1019. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1020. p^.location.register:=reg32toreg16(hregister);
  1021. end;
  1022. end;
  1023. 8 :
  1024. begin
  1025. {$ifdef test_dest_loc}
  1026. {$error Don't know what to do here}
  1027. {$endif test_dest_loc}
  1028. hregister:=getexplicitregister32(R_EAX);
  1029. hregister2:=getexplicitregister32(R_EDX);
  1030. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1031. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  1032. p^.location.registerlow:=hregister;
  1033. p^.location.registerhigh:=hregister2;
  1034. end;
  1035. else internalerror(7);
  1036. end
  1037. end
  1038. else if (p^.resulttype^.deftype=floatdef) then
  1039. case pfloatdef(p^.resulttype)^.typ of
  1040. f32bit:
  1041. begin
  1042. p^.location.loc:=LOC_REGISTER;
  1043. {$ifdef test_dest_loc}
  1044. if dest_loc_known and (dest_loc_tree=p) then
  1045. mov_reg_to_dest(p,S_L,R_EAX)
  1046. else
  1047. {$endif test_dest_loc}
  1048. begin
  1049. hregister:=getexplicitregister32(R_EAX);
  1050. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1051. p^.location.register:=hregister;
  1052. end;
  1053. end;
  1054. else
  1055. begin
  1056. p^.location.loc:=LOC_FPU;
  1057. inc(fpuvaroffset);
  1058. end;
  1059. end
  1060. else if is_ansistring(p^.resulttype) or
  1061. is_widestring(p^.resulttype) then
  1062. begin
  1063. hregister:=getexplicitregister32(R_EAX);
  1064. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1065. gettempansistringreference(hr);
  1066. decrstringref(p^.resulttype,hr);
  1067. emit_reg_ref(A_MOV,S_L,hregister,
  1068. newreference(hr));
  1069. ungetregister32(hregister);
  1070. p^.location.loc:=LOC_MEM;
  1071. p^.location.reference:=hr;
  1072. end
  1073. else
  1074. begin
  1075. p^.location.loc:=LOC_REGISTER;
  1076. {$ifdef test_dest_loc}
  1077. if dest_loc_known and (dest_loc_tree=p) then
  1078. mov_reg_to_dest(p,S_L,R_EAX)
  1079. else
  1080. {$endif test_dest_loc}
  1081. begin
  1082. hregister:=getexplicitregister32(R_EAX);
  1083. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1084. p^.location.register:=hregister;
  1085. end;
  1086. end;
  1087. end;
  1088. end;
  1089. { perhaps i/o check ? }
  1090. if iolabel<>nil then
  1091. begin
  1092. emit_sym(A_PUSH,S_L,iolabel);
  1093. emitcall('FPC_IOCHECK');
  1094. end;
  1095. if pop_size>0 then
  1096. emit_const_reg(A_ADD,S_L,pop_size,R_ESP);
  1097. { restore registers }
  1098. popusedregisters(pushed);
  1099. { at last, restore instance pointer (SELF) }
  1100. if loadesi then
  1101. maybe_loadesi;
  1102. pp:=params;
  1103. while assigned(pp) do
  1104. begin
  1105. if assigned(pp^.left) then
  1106. begin
  1107. if (pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1108. ungetiftemp(pp^.left^.location.reference);
  1109. { process also all nodes of an array of const }
  1110. if pp^.left^.treetype=arrayconstructn then
  1111. begin
  1112. if assigned(pp^.left^.left) then
  1113. begin
  1114. hp:=pp^.left;
  1115. while assigned(hp) do
  1116. begin
  1117. if (hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1118. ungetiftemp(hp^.left^.location.reference);
  1119. hp:=hp^.right;
  1120. end;
  1121. end;
  1122. end;
  1123. end;
  1124. pp:=pp^.right;
  1125. end;
  1126. if inlined then
  1127. ungetpersistanttemp(inlinecode^.retoffset);
  1128. disposetree(params);
  1129. { from now on the result can be freed normally }
  1130. if inlined and ret_in_param(p^.resulttype) then
  1131. persistanttemptonormal(funcretref.offset);
  1132. { if return value is not used }
  1133. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1134. begin
  1135. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1136. begin
  1137. { data which must be finalized ? }
  1138. if (p^.resulttype^.needs_inittable) and
  1139. ( (p^.resulttype^.deftype<>objectdef) or
  1140. not(pobjectdef(p^.resulttype)^.is_class)) then
  1141. finalize(p^.resulttype,p^.location.reference,ret_in_param(p^.resulttype));
  1142. { release unused temp }
  1143. ungetiftemp(p^.location.reference)
  1144. end
  1145. else if p^.location.loc=LOC_FPU then
  1146. begin
  1147. { release FPU stack }
  1148. emit_reg(A_FSTP,S_NO,R_ST0);
  1149. {
  1150. dec(fpuvaroffset);
  1151. do NOT decrement as the increment before
  1152. is not called for unused results PM }
  1153. end;
  1154. end;
  1155. end;
  1156. {*****************************************************************************
  1157. SecondProcInlineN
  1158. *****************************************************************************}
  1159. procedure secondprocinline(var p : ptree);
  1160. var st : psymtable;
  1161. oldprocsym : pprocsym;
  1162. para_size : longint;
  1163. oldprocinfo : pprocinfo;
  1164. oldinlining_procedure,
  1165. nostackframe,make_global : boolean;
  1166. proc_names : tstringcontainer;
  1167. inlineentrycode,inlineexitcode : paasmoutput;
  1168. oldexitlabel,oldexit2label,oldquickexitlabel:Pasmlabel;
  1169. begin
  1170. oldinlining_procedure:=inlining_procedure;
  1171. oldexitlabel:=aktexitlabel;
  1172. oldexit2label:=aktexit2label;
  1173. oldquickexitlabel:=quickexitlabel;
  1174. getlabel(aktexitlabel);
  1175. getlabel(aktexit2label);
  1176. oldprocsym:=aktprocsym;
  1177. { save old procinfo }
  1178. getmem(oldprocinfo,sizeof(tprocinfo));
  1179. move(procinfo^,oldprocinfo^,sizeof(tprocinfo));
  1180. { we're inlining a procedure }
  1181. inlining_procedure:=true;
  1182. { set the return value }
  1183. aktprocsym:=p^.inlineprocsym;
  1184. procinfo^.returntype:=aktprocsym^.definition^.rettype;
  1185. procinfo^.return_offset:=p^.retoffset;
  1186. procinfo^.para_offset:=p^.para_offset;
  1187. { arg space has been filled by the parent secondcall }
  1188. st:=aktprocsym^.definition^.localst;
  1189. { set it to the same lexical level }
  1190. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1191. if st^.datasize>0 then
  1192. begin
  1193. st^.address_fixup:=gettempofsizepersistant(st^.datasize);
  1194. {$ifdef extdebug}
  1195. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1196. exprasmlist^.concat(new(pai_asm_comment,init(strpnew(
  1197. 'local symtable is at offset '+tostr(st^.address_fixup)))));
  1198. {$endif extdebug}
  1199. end;
  1200. {$ifdef extdebug}
  1201. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('Start of inlined proc'))));
  1202. {$endif extdebug}
  1203. { takes care of local data initialization }
  1204. inlineentrycode:=new(paasmoutput,init);
  1205. inlineexitcode:=new(paasmoutput,init);
  1206. proc_names.init;
  1207. para_size:=p^.para_size;
  1208. make_global:=false; { to avoid warning }
  1209. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  1210. exprasmlist^.concatlist(inlineentrycode);
  1211. secondpass(p^.inlinetree);
  1212. genexitcode(inlineexitcode,0,false,true);
  1213. exprasmlist^.concatlist(inlineexitcode);
  1214. {$ifdef extdebug}
  1215. exprasmlist^.concat(new(pai_asm_comment,init(strpnew('End of inlined proc'))));
  1216. {$endif extdebug}
  1217. {we can free the local data now, reset also the fixup address }
  1218. if st^.datasize>0 then
  1219. begin
  1220. ungetpersistanttemp(st^.address_fixup);
  1221. st^.address_fixup:=0;
  1222. end;
  1223. { restore procinfo }
  1224. move(oldprocinfo^,procinfo^,sizeof(tprocinfo));
  1225. freemem(oldprocinfo,sizeof(tprocinfo));
  1226. { restore }
  1227. aktprocsym:=oldprocsym;
  1228. aktexitlabel:=oldexitlabel;
  1229. aktexit2label:=oldexit2label;
  1230. quickexitlabel:=oldquickexitlabel;
  1231. inlining_procedure:=oldinlining_procedure;
  1232. end;
  1233. end.
  1234. {
  1235. $Log$
  1236. Revision 1.117 2000-01-16 22:17:11 peter
  1237. * renamed call_offset to para_offset
  1238. Revision 1.116 2000/01/09 12:35:00 jonas
  1239. * changed edi allocation to use getexplicitregister32/ungetregister
  1240. (adapted tgeni386 a bit for this) and enabled it by default
  1241. * fixed very big and stupid bug of mine in cg386mat that broke the
  1242. include() code (and make cycle :( ) if you compiled without
  1243. -dnewoptimizations
  1244. Revision 1.115 2000/01/09 01:44:19 jonas
  1245. + (de)allocation info for EDI to fix reported bug on mailinglist.
  1246. Also some (de)allocation info for ESI added. Between -dallocEDI
  1247. because at this time of the night bugs could easily slip in ;)
  1248. Revision 1.114 2000/01/07 01:14:20 peter
  1249. * updated copyright to 2000
  1250. Revision 1.113 1999/12/22 01:01:46 peter
  1251. - removed freelabel()
  1252. * added undefined label detection in internal assembler, this prevents
  1253. a lot of ld crashes and wrong .o files
  1254. * .o files aren't written anymore if errors have occured
  1255. * inlining of assembler labels is now correct
  1256. Revision 1.112 1999/12/13 21:49:54 pierre
  1257. * bug in extdebugg code for inlined procedures
  1258. Revision 1.111 1999/11/30 10:40:42 peter
  1259. + ttype, tsymlist
  1260. Revision 1.110 1999/11/06 14:34:17 peter
  1261. * truncated log to 20 revs
  1262. Revision 1.109 1999/11/04 00:23:58 pierre
  1263. * fix for fpuvaroffset for unused return value
  1264. Revision 1.108 1999/10/26 12:30:40 peter
  1265. * const parameter is now checked
  1266. * better and generic check if a node can be used for assigning
  1267. * export fixes
  1268. * procvar equal works now (it never had worked at least from 0.99.8)
  1269. * defcoll changed to linkedlist with pparaitem so it can easily be
  1270. walked both directions
  1271. Revision 1.107 1999/10/08 15:40:47 pierre
  1272. * use and remember that C functions with complex data results use ret $4
  1273. Revision 1.106 1999/09/27 23:44:46 peter
  1274. * procinfo is now a pointer
  1275. * support for result setting in sub procedure
  1276. Revision 1.105 1999/09/26 13:26:02 florian
  1277. * exception patch of Romio nevertheless the excpetion handling
  1278. needs some corections regarding register saving
  1279. * gettempansistring is again a procedure
  1280. Revision 1.104 1999/09/16 11:34:46 pierre
  1281. * typo correction
  1282. Revision 1.103 1999/09/07 07:54:23 peter
  1283. * small array push to open array fixed, open array always needs addr
  1284. pushing
  1285. Revision 1.102 1999/08/25 11:59:39 jonas
  1286. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1287. Revision 1.101 1999/08/23 23:38:18 pierre
  1288. + TEMPREGDEBUG code added
  1289. Revision 1.100 1999/08/19 13:08:45 pierre
  1290. * emit_??? used
  1291. Revision 1.99 1999/08/09 22:19:47 peter
  1292. * classes vmt changed to only positive addresses
  1293. * sharedlib creation is working
  1294. Revision 1.98 1999/08/09 10:37:55 peter
  1295. * fixed pushing of self with methodpointer
  1296. Revision 1.97 1999/08/04 13:45:18 florian
  1297. + floating point register variables !!
  1298. * pairegalloc is now generated for register variables
  1299. Revision 1.96 1999/08/04 00:22:41 florian
  1300. * renamed i386asm and i386base to cpuasm and cpubase
  1301. Revision 1.95 1999/08/03 22:02:34 peter
  1302. * moved bitmask constants to sets
  1303. * some other type/const renamings
  1304. Revision 1.94 1999/07/06 21:48:09 florian
  1305. * a lot bug fixes:
  1306. - po_external isn't any longer necessary for procedure compatibility
  1307. - m_tp_procvar is in -Sd now available
  1308. - error messages of procedure variables improved
  1309. - return values with init./finalization fixed
  1310. - data types with init./finalization aren't any longer allowed in variant
  1311. record
  1312. Revision 1.93 1999/06/22 13:31:24 peter
  1313. * merged
  1314. Revision 1.92 1999/06/16 09:32:45 peter
  1315. * merged
  1316. Revision 1.91 1999/06/14 17:47:47 peter
  1317. * merged
  1318. Revision 1.90.2.3 1999/06/22 13:30:08 peter
  1319. * fixed return with packenum
  1320. }