cg386cal.pas 67 KB

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