cg386cal.pas 66 KB

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