2
0

cg386cal.pas 71 KB

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