n386cal.pas 72 KB

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