cg386cal.pas 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for in call nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386cal;
  19. interface
  20. { $define AnsiStrRef}
  21. uses
  22. symtable,tree;
  23. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  24. push_from_left_to_right,inlined,dword_align : boolean;para_offset : longint);
  25. procedure secondcalln(var p : ptree);
  26. procedure secondprocinline(var p : ptree);
  27. implementation
  28. uses
  29. globtype,systems,
  30. cobjects,verbose,globals,
  31. symconst,aasm,types,
  32. {$ifdef GDB}
  33. gdb,
  34. {$endif GDB}
  35. hcodegen,temp_gen,pass_2,
  36. cpubase,cpuasm,
  37. cgai386,tgeni386,cg386ld;
  38. {*****************************************************************************
  39. SecondCallParaN
  40. *****************************************************************************}
  41. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  42. push_from_left_to_right,inlined,dword_align : boolean;para_offset : longint);
  43. procedure maybe_push_high;
  44. begin
  45. { open array ? }
  46. { defcoll^.data can be nil for read/write }
  47. if assigned(defcoll^.data) and
  48. push_high_param(defcoll^.data) then
  49. begin
  50. if assigned(p^.hightree) then
  51. begin
  52. secondpass(p^.hightree);
  53. { this is a longint anyway ! }
  54. push_value_para(p^.hightree,inlined,para_offset,4);
  55. end
  56. else
  57. internalerror(432645);
  58. end;
  59. end;
  60. var
  61. otlabel,oflabel : pasmlabel;
  62. align : longint;
  63. { temporary variables: }
  64. tempdeftype : tdeftype;
  65. r : preference;
  66. begin
  67. { push from left to right if specified }
  68. if push_from_left_to_right and assigned(p^.right) then
  69. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,
  70. inlined,dword_align,para_offset);
  71. otlabel:=truelabel;
  72. oflabel:=falselabel;
  73. getlabel(truelabel);
  74. getlabel(falselabel);
  75. secondpass(p^.left);
  76. { filter array constructor with c styled args }
  77. if is_array_constructor(p^.left^.resulttype) and p^.left^.cargs then
  78. begin
  79. { nothing, everything is already pushed }
  80. end
  81. { in codegen.handleread.. defcoll^.data is set to nil }
  82. else if assigned(defcoll^.data) and
  83. (defcoll^.data^.deftype=formaldef) then
  84. begin
  85. { allow @var }
  86. inc(pushedparasize,4);
  87. if p^.left^.treetype=addrn then
  88. begin
  89. { always a register }
  90. if inlined then
  91. begin
  92. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  93. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  94. p^.left^.location.register,r)));
  95. end
  96. else
  97. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
  98. ungetregister32(p^.left^.location.register);
  99. end
  100. else
  101. begin
  102. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  103. CGMessage(type_e_mismatch)
  104. else
  105. begin
  106. if inlined then
  107. begin
  108. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  109. newreference(p^.left^.location.reference),R_EDI)));
  110. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  111. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  112. end
  113. else
  114. emitpushreferenceaddr(p^.left^.location.reference);
  115. del_reference(p^.left^.location.reference);
  116. end;
  117. end;
  118. end
  119. { handle call by reference parameter }
  120. else if (defcoll^.paratyp=vs_var) then
  121. begin
  122. if (p^.left^.location.loc<>LOC_REFERENCE) then
  123. CGMessage(cg_e_var_must_be_reference);
  124. maybe_push_high;
  125. inc(pushedparasize,4);
  126. if inlined then
  127. begin
  128. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  129. newreference(p^.left^.location.reference),R_EDI)));
  130. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  131. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  132. end
  133. else
  134. emitpushreferenceaddr(p^.left^.location.reference);
  135. del_reference(p^.left^.location.reference);
  136. end
  137. else
  138. begin
  139. tempdeftype:=p^.resulttype^.deftype;
  140. if tempdeftype=filedef then
  141. CGMessage(cg_e_file_must_call_by_reference);
  142. if push_addr_param(p^.resulttype) then
  143. begin
  144. maybe_push_high;
  145. inc(pushedparasize,4);
  146. if inlined then
  147. begin
  148. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  149. newreference(p^.left^.location.reference),R_EDI)));
  150. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  151. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  152. R_EDI,r)));
  153. end
  154. else
  155. emitpushreferenceaddr(p^.left^.location.reference);
  156. del_reference(p^.left^.location.reference);
  157. end
  158. else
  159. begin
  160. align:=target_os.stackalignment;
  161. if dword_align then
  162. align:=4;
  163. push_value_para(p^.left,inlined,para_offset,align);
  164. end;
  165. end;
  166. freelabel(truelabel);
  167. freelabel(falselabel);
  168. truelabel:=otlabel;
  169. falselabel:=oflabel;
  170. { push from right to left }
  171. if not push_from_left_to_right and assigned(p^.right) then
  172. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,
  173. inlined,dword_align,para_offset);
  174. end;
  175. {*****************************************************************************
  176. SecondCallN
  177. *****************************************************************************}
  178. procedure secondcalln(var p : ptree);
  179. var
  180. unusedregisters : tregisterset;
  181. pushed : tpushed;
  182. hr,funcretref : treference;
  183. hregister,hregister2 : tregister;
  184. oldpushedparasize : longint;
  185. { true if ESI must be loaded again after the subroutine }
  186. loadesi : boolean;
  187. { true if a virtual method must be called directly }
  188. no_virtual_call : boolean;
  189. { true if we produce a con- or destrutor in a call }
  190. is_con_or_destructor : boolean;
  191. { true if a constructor is called again }
  192. extended_new : boolean;
  193. { adress returned from an I/O-error }
  194. iolabel : pasmlabel;
  195. { lexlevel count }
  196. i : longint;
  197. { help reference pointer }
  198. r : preference;
  199. hp,
  200. pp,params : ptree;
  201. inlined : boolean;
  202. inlinecode : ptree;
  203. para_offset : longint;
  204. { instruction for alignement correction }
  205. { corr : pai386;}
  206. { we must pop this size also after !! }
  207. { must_pop : boolean; }
  208. pop_size : longint;
  209. label
  210. dont_call;
  211. begin
  212. reset_reference(p^.location.reference);
  213. extended_new:=false;
  214. iolabel:=nil;
  215. inlinecode:=nil;
  216. inlined:=false;
  217. loadesi:=true;
  218. no_virtual_call:=false;
  219. unusedregisters:=unused;
  220. if not assigned(p^.procdefinition) then
  221. exit;
  222. if (pocall_inline in p^.procdefinition^.proccalloptions) then
  223. begin
  224. inlined:=true;
  225. inlinecode:=p^.right;
  226. { set it to the same lexical level as the local symtable, becuase
  227. the para's are stored there }
  228. pprocdef(p^.procdefinition)^.parast^.symtablelevel:=aktprocsym^.definition^.localst^.symtablelevel;
  229. if assigned(p^.left) then
  230. inlinecode^.para_offset:=gettempofsizepersistant(inlinecode^.para_size);
  231. pprocdef(p^.procdefinition)^.parast^.address_fixup:=inlinecode^.para_offset;
  232. {$ifdef extdebug}
  233. Comment(V_debug,
  234. 'inlined parasymtable is at offset '
  235. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup));
  236. exprasmlist^.concat(new(pai_asm_comment,init(
  237. strpnew('inlined parasymtable is at offset '
  238. +tostr(pprocdef(p^.procdefinition)^.parast^.address_fixup)))));
  239. {$endif extdebug}
  240. p^.right:=nil;
  241. { disable further inlining of the same proc
  242. in the args }
  243. {$ifdef INCLUDEOK}
  244. exclude(p^.procdefinition^.proccalloptions,pocall_inline);
  245. {$else}
  246. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions-[pocall_inline];
  247. {$endif}
  248. end;
  249. { only if no proc var }
  250. if not(assigned(p^.right)) then
  251. is_con_or_destructor:=(p^.procdefinition^.proctypeoption in [potype_constructor,potype_destructor]);
  252. { proc variables destroy all registers }
  253. if (p^.right=nil) and
  254. { virtual methods too }
  255. not(po_virtualmethod in p^.procdefinition^.procoptions) then
  256. begin
  257. if (cs_check_io in aktlocalswitches) and
  258. (po_iocheck in p^.procdefinition^.procoptions) and
  259. not(po_iocheck in aktprocsym^.definition^.procoptions) then
  260. begin
  261. getlabel(iolabel);
  262. emitlab(iolabel);
  263. end
  264. else
  265. iolabel:=nil;
  266. { save all used registers }
  267. pushusedregisters(pushed,pprocdef(p^.procdefinition)^.usedregisters);
  268. { give used registers through }
  269. usedinproc:=usedinproc or pprocdef(p^.procdefinition)^.usedregisters;
  270. end
  271. else
  272. begin
  273. pushusedregisters(pushed,$ff);
  274. usedinproc:=$ff;
  275. { no IO check for methods and procedure variables }
  276. iolabel:=nil;
  277. end;
  278. { generate the code for the parameter and push them }
  279. oldpushedparasize:=pushedparasize;
  280. pushedparasize:=0;
  281. pop_size:=0;
  282. if (not inlined) then
  283. begin
  284. { Old pushedsize aligned on 4 ? }
  285. i:=oldpushedparasize and 3;
  286. if i>0 then
  287. inc(pop_size,4-i);
  288. { This parasize aligned on 4 ? }
  289. i:=p^.procdefinition^.para_size and 3;
  290. if i>0 then
  291. inc(pop_size,4-i);
  292. { insert the opcode and update pushedparasize }
  293. if pop_size>0 then
  294. begin
  295. inc(pushedparasize,pop_size);
  296. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,pop_size,R_ESP)));
  297. {$ifdef GDB}
  298. if (cs_debuginfo in aktmoduleswitches) and
  299. (exprasmlist^.first=exprasmlist^.last) then
  300. exprasmlist^.concat(new(pai_force_line,init));
  301. {$endif GDB}
  302. end;
  303. end;
  304. if (p^.resulttype<>pdef(voiddef)) and
  305. ret_in_param(p^.resulttype) then
  306. begin
  307. funcretref.symbol:=nil;
  308. {$ifdef test_dest_loc}
  309. if dest_loc_known and (dest_loc_tree=p) and
  310. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  311. begin
  312. funcretref:=dest_loc.reference;
  313. if assigned(dest_loc.reference.symbol) then
  314. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  315. in_dest_loc:=true;
  316. end
  317. else
  318. {$endif test_dest_loc}
  319. if inlined then
  320. begin
  321. reset_reference(funcretref);
  322. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.retdef^.size);
  323. funcretref.base:=procinfo.framepointer;
  324. end
  325. else
  326. gettempofsizereference(p^.procdefinition^.retdef^.size,funcretref);
  327. end;
  328. if assigned(p^.left) then
  329. begin
  330. { be found elsewhere }
  331. if inlined then
  332. para_offset:=pprocdef(p^.procdefinition)^.parast^.address_fixup+
  333. pprocdef(p^.procdefinition)^.parast^.datasize
  334. else
  335. para_offset:=0;
  336. if assigned(p^.right) then
  337. secondcallparan(p^.left,pabstractprocdef(p^.right^.resulttype)^.para1,
  338. (pocall_leftright in p^.procdefinition^.proccalloptions),
  339. inlined,
  340. (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  341. (pocall_stdcall in p^.procdefinition^.proccalloptions),
  342. para_offset)
  343. else
  344. secondcallparan(p^.left,p^.procdefinition^.para1,
  345. (pocall_leftright in p^.procdefinition^.proccalloptions),
  346. inlined,
  347. (pocall_cdecl in p^.procdefinition^.proccalloptions) or
  348. (pocall_stdcall in p^.procdefinition^.proccalloptions),
  349. para_offset);
  350. end;
  351. params:=p^.left;
  352. p^.left:=nil;
  353. if inlined then
  354. inlinecode^.retoffset:=gettempofsizepersistant(4);
  355. if ret_in_param(p^.resulttype) then
  356. begin
  357. inc(pushedparasize,4);
  358. if inlined then
  359. begin
  360. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  361. newreference(funcretref),R_EDI)));
  362. r:=new_reference(procinfo.framepointer,inlinecode^.retoffset);
  363. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  364. R_EDI,r)));
  365. end
  366. else
  367. emitpushreferenceaddr(funcretref);
  368. end;
  369. { procedure variable ? }
  370. if (p^.right=nil) then
  371. begin
  372. { overloaded operator have no symtable }
  373. { push self }
  374. if assigned(p^.symtable) and
  375. (p^.symtable^.symtabletype=withsymtable) then
  376. begin
  377. { dirty trick to avoid the secondcall below }
  378. p^.methodpointer:=genzeronode(callparan);
  379. p^.methodpointer^.location.loc:=LOC_REGISTER;
  380. p^.methodpointer^.location.register:=R_ESI;
  381. { ARGHHH this is wrong !!!
  382. if we can init from base class for a child
  383. class that the wrong VMT will be
  384. transfered to constructor !! }
  385. p^.methodpointer^.resulttype:=
  386. ptree(pwithsymtable(p^.symtable)^.withnode)^.left^.resulttype;
  387. { change dispose type !! }
  388. p^.disposetyp:=dt_mbleft_and_method;
  389. { make a reference }
  390. new(r);
  391. reset_reference(r^);
  392. { if assigned(ptree(pwithsymtable(p^.symtable)^.withnode)^.pref) then
  393. begin
  394. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.pref^;
  395. end
  396. else
  397. begin
  398. r^.offset:=p^.symtable^.datasize;
  399. r^.base:=procinfo.framepointer;
  400. end; }
  401. r^:=ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^;
  402. if (not pwithsymtable(p^.symtable)^.direct_with) or
  403. pobjectdef(p^.methodpointer^.resulttype)^.is_class then
  404. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)))
  405. else
  406. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,r,R_ESI)));
  407. end;
  408. { push self }
  409. if assigned(p^.symtable) and
  410. ((p^.symtable^.symtabletype=objectsymtable) or
  411. (p^.symtable^.symtabletype=withsymtable)) then
  412. begin
  413. if assigned(p^.methodpointer) then
  414. begin
  415. {
  416. if p^.methodpointer^.resulttype=classrefdef then
  417. begin
  418. two possibilities:
  419. 1. constructor
  420. 2. class method
  421. end
  422. else }
  423. begin
  424. case p^.methodpointer^.treetype of
  425. typen:
  426. begin
  427. { direct call to inherited method }
  428. if (po_abstractmethod in p^.procdefinition^.procoptions) then
  429. begin
  430. CGMessage(cg_e_cant_call_abstract_method);
  431. goto dont_call;
  432. end;
  433. { generate no virtual call }
  434. no_virtual_call:=true;
  435. if (sp_static in p^.symtableprocentry^.symoptions) then
  436. begin
  437. { well lets put the VMT address directly into ESI }
  438. { it is kind of dirty but that is the simplest }
  439. { way to accept virtual static functions (PM) }
  440. loadesi:=true;
  441. { if no VMT just use $0 bug0214 PM }
  442. if not(oo_has_vmt in pobjectdef(p^.methodpointer^.resulttype)^.objectoptions) then
  443. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,0,R_ESI)))
  444. else
  445. begin
  446. exprasmlist^.concat(new(pai386,op_sym_ofs_reg(A_MOV,S_L,
  447. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname),
  448. 0,R_ESI)));
  449. end;
  450. { exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  451. this is done below !! }
  452. end
  453. else
  454. { this is a member call, so ESI isn't modfied }
  455. loadesi:=false;
  456. { a class destructor needs a flag }
  457. if pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  458. assigned(aktprocsym) and
  459. (aktprocsym^.definition^.proctypeoption=potype_destructor) then
  460. begin
  461. push_int(0);
  462. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  463. end;
  464. if not(is_con_or_destructor and
  465. pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  466. assigned(aktprocsym) and
  467. (aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor])
  468. ) then
  469. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  470. { if an inherited con- or destructor should be }
  471. { called in a con- or destructor then a warning }
  472. { will be made }
  473. { con- and destructors need a pointer to the vmt }
  474. if is_con_or_destructor and
  475. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) and
  476. assigned(aktprocsym) then
  477. begin
  478. if not(aktprocsym^.definition^.proctypeoption in
  479. [potype_constructor,potype_destructor]) then
  480. CGMessage(cg_w_member_cd_call_from_method);
  481. end;
  482. { class destructors get there flag below }
  483. if is_con_or_destructor and
  484. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class and
  485. assigned(aktprocsym) and
  486. (aktprocsym^.definition^.proctypeoption=potype_destructor)) then
  487. push_int(0);
  488. end;
  489. hnewn:
  490. begin
  491. { extended syntax of new }
  492. { ESI must be zero }
  493. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_ESI,R_ESI)));
  494. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  495. { insert the vmt }
  496. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,
  497. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname))));
  498. extended_new:=true;
  499. end;
  500. hdisposen:
  501. begin
  502. secondpass(p^.methodpointer);
  503. { destructor with extended syntax called from dispose }
  504. { hdisposen always deliver LOC_REFERENCE }
  505. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  506. newreference(p^.methodpointer^.location.reference),R_ESI)));
  507. del_reference(p^.methodpointer^.location.reference);
  508. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  509. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,
  510. newasmsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname))));
  511. end;
  512. else
  513. begin
  514. { call to an instance member }
  515. if (p^.symtable^.symtabletype<>withsymtable) then
  516. begin
  517. secondpass(p^.methodpointer);
  518. case p^.methodpointer^.location.loc of
  519. LOC_CREGISTER,
  520. LOC_REGISTER:
  521. begin
  522. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  523. ungetregister32(p^.methodpointer^.location.register);
  524. end;
  525. else
  526. begin
  527. if (p^.methodpointer^.resulttype^.deftype=classrefdef) or
  528. ((p^.methodpointer^.resulttype^.deftype=objectdef) and
  529. pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  530. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  531. newreference(p^.methodpointer^.location.reference),R_ESI)))
  532. else
  533. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  534. newreference(p^.methodpointer^.location.reference),R_ESI)));
  535. del_reference(p^.methodpointer^.location.reference);
  536. end;
  537. end;
  538. end;
  539. { when calling a class method, we have to load ESI with the VMT !
  540. But, not for a class method via self }
  541. if not(po_containsself in p^.procdefinition^.procoptions) then
  542. begin
  543. if (po_classmethod in p^.procdefinition^.procoptions) and
  544. not(p^.methodpointer^.resulttype^.deftype=classrefdef) then
  545. begin
  546. { class method needs current VMT }
  547. new(r);
  548. reset_reference(r^);
  549. r^.base:=R_ESI;
  550. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  551. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  552. end;
  553. { direct call to destructor: don't remove data! }
  554. if (p^.procdefinition^.proctypeoption=potype_destructor) and
  555. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  556. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  557. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,1)));
  558. { direct call to class constructor, don't allocate memory }
  559. if (p^.procdefinition^.proctypeoption=potype_constructor) and
  560. (p^.methodpointer^.resulttype^.deftype=objectdef) and
  561. (pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  562. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,0)))
  563. else
  564. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  565. end;
  566. if is_con_or_destructor then
  567. begin
  568. { classes don't get a VMT pointer pushed }
  569. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  570. not(pobjectdef(p^.methodpointer^.resulttype)^.is_class) then
  571. begin
  572. if (p^.procdefinition^.proctypeoption=potype_constructor) then
  573. begin
  574. { it's no bad idea, to insert the VMT }
  575. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,newasmsymbol(
  576. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname))));
  577. end
  578. { destructors haven't to dispose the instance, if this is }
  579. { a direct call }
  580. else
  581. push_int(0);
  582. end;
  583. end;
  584. end;
  585. end;
  586. end;
  587. end
  588. else
  589. begin
  590. if (po_classmethod in p^.procdefinition^.procoptions) and
  591. not(
  592. assigned(aktprocsym) and
  593. (po_classmethod in aktprocsym^.definition^.procoptions)
  594. ) then
  595. begin
  596. { class method needs current VMT }
  597. new(r);
  598. reset_reference(r^);
  599. r^.base:=R_ESI;
  600. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  601. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  602. end
  603. else
  604. begin
  605. { member call, ESI isn't modified }
  606. loadesi:=false;
  607. end;
  608. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  609. { but a con- or destructor here would probably almost }
  610. { always be placed wrong }
  611. if is_con_or_destructor then
  612. begin
  613. CGMessage(cg_w_member_cd_call_from_method);
  614. push_int(0);
  615. end;
  616. end;
  617. end;
  618. { push base pointer ?}
  619. if (lexlevel>=normal_function_level) and assigned(pprocdef(p^.procdefinition)^.parast) and
  620. ((pprocdef(p^.procdefinition)^.parast^.symtablelevel)>normal_function_level) then
  621. begin
  622. { if we call a nested function in a method, we must }
  623. { push also SELF! }
  624. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  625. { access }
  626. {
  627. begin
  628. loadesi:=false;
  629. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  630. end;
  631. }
  632. if lexlevel=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  633. begin
  634. new(r);
  635. reset_reference(r^);
  636. r^.offset:=procinfo.framepointer_offset;
  637. r^.base:=procinfo.framepointer;
  638. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)))
  639. end
  640. { this is only true if the difference is one !!
  641. but it cannot be more !! }
  642. else if (lexlevel=pprocdef(p^.procdefinition)^.parast^.symtablelevel-1) then
  643. begin
  644. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,procinfo.framepointer)))
  645. end
  646. else if (lexlevel>pprocdef(p^.procdefinition)^.parast^.symtablelevel) then
  647. begin
  648. hregister:=getregister32;
  649. new(r);
  650. reset_reference(r^);
  651. r^.offset:=procinfo.framepointer_offset;
  652. r^.base:=procinfo.framepointer;
  653. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  654. for i:=(pprocdef(p^.procdefinition)^.parast^.symtablelevel) to lexlevel-1 do
  655. begin
  656. new(r);
  657. reset_reference(r^);
  658. {we should get the correct frame_pointer_offset at each level
  659. how can we do this !!! }
  660. r^.offset:=procinfo.framepointer_offset;
  661. r^.base:=hregister;
  662. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  663. end;
  664. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hregister)));
  665. ungetregister32(hregister);
  666. end
  667. else
  668. internalerror(25000);
  669. end;
  670. if (po_virtualmethod in p^.procdefinition^.procoptions) and
  671. not(no_virtual_call) then
  672. begin
  673. { static functions contain the vmt_address in ESI }
  674. { also class methods }
  675. { Here it is quite tricky because it also depends }
  676. { on the methodpointer PM }
  677. if assigned(aktprocsym) then
  678. begin
  679. if (((sp_static in aktprocsym^.symoptions) or
  680. (po_classmethod in aktprocsym^.definition^.procoptions)) and
  681. ((p^.methodpointer=nil) or (p^.methodpointer^.treetype=typen)))
  682. or
  683. (po_staticmethod in p^.procdefinition^.procoptions) or
  684. (p^.procdefinition^.proctypeoption=potype_constructor) or
  685. { ESI is loaded earlier }
  686. (po_classmethod in p^.procdefinition^.procoptions) then
  687. begin
  688. new(r);
  689. reset_reference(r^);
  690. r^.base:=R_ESI;
  691. end
  692. else
  693. begin
  694. new(r);
  695. reset_reference(r^);
  696. r^.base:=R_ESI;
  697. { this is one point where we need vmt_offset (PM) }
  698. r^.offset:= pprocdef(p^.procdefinition)^._class^.vmt_offset;
  699. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  700. new(r);
  701. reset_reference(r^);
  702. r^.base:=R_EDI;
  703. end;
  704. end
  705. else
  706. { aktprocsym should be assigned, also in main program }
  707. internalerror(12345);
  708. {
  709. begin
  710. new(r);
  711. reset_reference(r^);
  712. r^.base:=R_ESI;
  713. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  714. new(r);
  715. reset_reference(r^);
  716. r^.base:=R_EDI;
  717. end;
  718. }
  719. if pprocdef(p^.procdefinition)^.extnumber=-1 then
  720. internalerror(44584);
  721. r^.offset:=pprocdef(p^.procdefinition)^.extnumber*4+12;
  722. {$ifndef TESTOBJEXT}
  723. if (cs_check_range in aktlocalswitches) then
  724. begin
  725. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r^.base)));
  726. emitcall('FPC_CHECK_OBJECT');
  727. end;
  728. {$else TESTOBJEXT}
  729. if (cs_check_range in aktlocalswitches) then
  730. begin
  731. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,
  732. newasmsymbol(pprocdef(p^.procdefinition)^._class^.vmt_mangledname))));
  733. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r^.base)));
  734. emitcall('FPC_CHECK_OBJECT_EXT');
  735. end;
  736. {$endif TESTOBJEXT}
  737. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,r)));
  738. end
  739. else if not inlined then
  740. emitcall(pprocdef(p^.procdefinition)^.mangledname)
  741. else { inlined proc }
  742. { inlined code is in inlinecode }
  743. begin
  744. { set poinline again }
  745. {$ifdef INCLUDEOK}
  746. include(p^.procdefinition^.proccalloptions,pocall_inline);
  747. {$else}
  748. p^.procdefinition^.proccalloptions:=p^.procdefinition^.proccalloptions+[pocall_inline];
  749. {$endif}
  750. { process the inlinecode }
  751. secondpass(inlinecode);
  752. { free the args }
  753. ungetpersistanttemp(pprocdef(p^.procdefinition)^.parast^.address_fixup);
  754. end;
  755. end
  756. else
  757. { now procedure variable case }
  758. begin
  759. secondpass(p^.right);
  760. { method pointer ? }
  761. if (po_methodpointer in p^.procdefinition^.procoptions) then
  762. begin
  763. { method pointer can't be in a register }
  764. hregister:=R_NO;
  765. { do some hacking if we call a method pointer }
  766. { which is a class member }
  767. { else ESI is overwritten ! }
  768. if (p^.right^.location.reference.base=R_ESI) or
  769. (p^.right^.location.reference.index=R_ESI) then
  770. begin
  771. del_reference(p^.right^.location.reference);
  772. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  773. newreference(p^.right^.location.reference),R_EDI)));
  774. hregister:=R_EDI;
  775. end;
  776. if (po_containsself in p^.procdefinition^.procoptions) then
  777. begin
  778. { load ESI }
  779. inc(p^.right^.location.reference.offset,4);
  780. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  781. newreference(p^.right^.location.reference),R_ESI)));
  782. dec(p^.right^.location.reference.offset,4);
  783. { push self pointer }
  784. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  785. end;
  786. if hregister=R_NO then
  787. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))))
  788. else
  789. exprasmlist^.concat(new(pai386,op_reg(A_CALL,S_NO,hregister)));
  790. del_reference(p^.right^.location.reference);
  791. end
  792. else
  793. begin
  794. case p^.right^.location.loc of
  795. LOC_REGISTER,LOC_CREGISTER:
  796. begin
  797. exprasmlist^.concat(new(pai386,op_reg(A_CALL,S_NO,p^.right^.location.register)));
  798. ungetregister32(p^.right^.location.register);
  799. end
  800. else
  801. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))));
  802. del_reference(p^.right^.location.reference);
  803. end;
  804. end;
  805. end;
  806. { this was only for normal functions
  807. displaced here so we also get
  808. it to work for procvars PM }
  809. if (not inlined) and (pocall_clearstack in p^.procdefinition^.proccalloptions) then
  810. begin
  811. { consider the alignment with the rest (PM) }
  812. inc(pushedparasize,pop_size);
  813. pop_size:=0;
  814. { better than an add on all processors }
  815. if pushedparasize=4 then
  816. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)))
  817. { the pentium has two pipes and pop reg is pairable }
  818. { but the registers must be different! }
  819. else if (pushedparasize=8) and
  820. not(cs_littlesize in aktglobalswitches) and
  821. (aktoptprocessor=ClassP5) and
  822. (procinfo._class=nil) then
  823. begin
  824. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  825. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  826. end
  827. else if pushedparasize<>0 then
  828. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pushedparasize,R_ESP)));
  829. end;
  830. dont_call:
  831. pushedparasize:=oldpushedparasize;
  832. unused:=unusedregisters;
  833. { handle function results }
  834. { structured results are easy to handle.... }
  835. { needed also when result_no_used !! }
  836. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  837. begin
  838. p^.location.loc:=LOC_MEM;
  839. p^.location.reference.symbol:=nil;
  840. p^.location.reference:=funcretref;
  841. end;
  842. { we have only to handle the result if it is used, but }
  843. { ansi/widestrings must be registered, so we can dispose them }
  844. if (p^.resulttype<>pdef(voiddef)) and (p^.return_value_used or
  845. is_ansistring(p^.resulttype) or is_widestring(p^.resulttype)) then
  846. begin
  847. { a contructor could be a function with boolean result }
  848. if (p^.right=nil) and
  849. (p^.procdefinition^.proctypeoption=potype_constructor) and
  850. { quick'n'dirty check if it is a class or an object }
  851. (p^.resulttype^.deftype=orddef) then
  852. begin
  853. p^.location.loc:=LOC_FLAGS;
  854. p^.location.resflags:=F_NE;
  855. if extended_new then
  856. begin
  857. {$ifdef test_dest_loc}
  858. if dest_loc_known and (dest_loc_tree=p) then
  859. mov_reg_to_dest(p,S_L,R_EAX)
  860. else
  861. {$endif test_dest_loc}
  862. begin
  863. hregister:=getexplicitregister32(R_EAX);
  864. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  865. p^.location.register:=hregister;
  866. end;
  867. end;
  868. end
  869. { structed results are easy to handle.... }
  870. else if ret_in_param(p^.resulttype) then
  871. begin
  872. {p^.location.loc:=LOC_MEM;
  873. stringdispose(p^.location.reference.symbol);
  874. p^.location.reference:=funcretref;
  875. already done above (PM) }
  876. end
  877. else
  878. begin
  879. if (p^.resulttype^.deftype in [orddef,enumdef]) then
  880. begin
  881. p^.location.loc:=LOC_REGISTER;
  882. case p^.resulttype^.size of
  883. 4 :
  884. begin
  885. {$ifdef test_dest_loc}
  886. if dest_loc_known and (dest_loc_tree=p) then
  887. mov_reg_to_dest(p,S_L,R_EAX)
  888. else
  889. {$endif test_dest_loc}
  890. begin
  891. hregister:=getexplicitregister32(R_EAX);
  892. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  893. p^.location.register:=hregister;
  894. end;
  895. end;
  896. 1 :
  897. begin
  898. {$ifdef test_dest_loc}
  899. if dest_loc_known and (dest_loc_tree=p) then
  900. mov_reg_to_dest(p,S_B,R_AL)
  901. else
  902. {$endif test_dest_loc}
  903. begin
  904. hregister:=getexplicitregister32(R_EAX);
  905. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  906. p^.location.register:=reg32toreg8(hregister);
  907. end;
  908. end;
  909. 2 :
  910. begin
  911. {$ifdef test_dest_loc}
  912. if dest_loc_known and (dest_loc_tree=p) then
  913. mov_reg_to_dest(p,S_W,R_AX)
  914. else
  915. {$endif test_dest_loc}
  916. begin
  917. hregister:=getexplicitregister32(R_EAX);
  918. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  919. p^.location.register:=reg32toreg16(hregister);
  920. end;
  921. end;
  922. 8 :
  923. begin
  924. {$ifdef test_dest_loc}
  925. {$error Don't know what to do here}
  926. {$endif test_dest_loc}
  927. hregister:=getexplicitregister32(R_EAX);
  928. hregister2:=getexplicitregister32(R_EDX);
  929. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  930. emit_reg_reg(A_MOV,S_L,R_EDX,hregister2);
  931. p^.location.registerlow:=hregister;
  932. p^.location.registerhigh:=hregister2;
  933. end;
  934. else internalerror(7);
  935. end
  936. end
  937. else if (p^.resulttype^.deftype=floatdef) then
  938. case pfloatdef(p^.resulttype)^.typ of
  939. f32bit:
  940. begin
  941. p^.location.loc:=LOC_REGISTER;
  942. {$ifdef test_dest_loc}
  943. if dest_loc_known and (dest_loc_tree=p) then
  944. mov_reg_to_dest(p,S_L,R_EAX)
  945. else
  946. {$endif test_dest_loc}
  947. begin
  948. hregister:=getexplicitregister32(R_EAX);
  949. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  950. p^.location.register:=hregister;
  951. end;
  952. end;
  953. else
  954. begin
  955. p^.location.loc:=LOC_FPU;
  956. inc(fpuvaroffset);
  957. end;
  958. end
  959. else if is_ansistring(p^.resulttype) or
  960. is_widestring(p^.resulttype) then
  961. begin
  962. hregister:=getexplicitregister32(R_EAX);
  963. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  964. if gettempansistringreference(hr) then
  965. decrstringref(p^.resulttype,hr);
  966. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,hregister,
  967. newreference(hr))));
  968. ungetregister32(hregister);
  969. p^.location.loc:=LOC_MEM;
  970. p^.location.reference:=hr;
  971. end
  972. else
  973. begin
  974. p^.location.loc:=LOC_REGISTER;
  975. {$ifdef test_dest_loc}
  976. if dest_loc_known and (dest_loc_tree=p) then
  977. mov_reg_to_dest(p,S_L,R_EAX)
  978. else
  979. {$endif test_dest_loc}
  980. begin
  981. hregister:=getexplicitregister32(R_EAX);
  982. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  983. p^.location.register:=hregister;
  984. end;
  985. end;
  986. end;
  987. end;
  988. { perhaps i/o check ? }
  989. if iolabel<>nil then
  990. begin
  991. exprasmlist^.concat(new(pai386,op_sym(A_PUSH,S_L,iolabel)));
  992. emitcall('FPC_IOCHECK');
  993. end;
  994. if pop_size>0 then
  995. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pop_size,R_ESP)));
  996. { restore registers }
  997. popusedregisters(pushed);
  998. { at last, restore instance pointer (SELF) }
  999. if loadesi then
  1000. maybe_loadesi;
  1001. pp:=params;
  1002. while assigned(pp) do
  1003. begin
  1004. if assigned(pp^.left) then
  1005. begin
  1006. if (pp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1007. ungetiftemp(pp^.left^.location.reference);
  1008. { process also all nodes of an array of const }
  1009. if pp^.left^.treetype=arrayconstructn then
  1010. begin
  1011. if assigned(pp^.left^.left) then
  1012. begin
  1013. hp:=pp^.left;
  1014. while assigned(hp) do
  1015. begin
  1016. if (hp^.left^.location.loc in [LOC_REFERENCE,LOC_MEM]) then
  1017. ungetiftemp(hp^.left^.location.reference);
  1018. hp:=hp^.right;
  1019. end;
  1020. end;
  1021. end;
  1022. end;
  1023. pp:=pp^.right;
  1024. end;
  1025. if inlined then
  1026. ungetpersistanttemp(inlinecode^.retoffset);
  1027. disposetree(params);
  1028. { from now on the result can be freed normally }
  1029. if inlined and ret_in_param(p^.resulttype) then
  1030. persistanttemptonormal(funcretref.offset);
  1031. { if return value is not used }
  1032. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1033. begin
  1034. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1035. begin
  1036. { data which must be finalized ? }
  1037. if (p^.resulttype^.needs_inittable) and
  1038. ( (p^.resulttype^.deftype<>objectdef) or
  1039. not(pobjectdef(p^.resulttype)^.is_class)) then
  1040. finalize(p^.resulttype,p^.location.reference,ret_in_param(p^.resulttype));
  1041. { release unused temp }
  1042. ungetiftemp(p^.location.reference)
  1043. end
  1044. else if p^.location.loc=LOC_FPU then
  1045. begin
  1046. { release FPU stack }
  1047. exprasmlist^.concat(new(pai386,op_reg(A_FSTP,S_NO,R_ST0)));
  1048. dec(fpuvaroffset);
  1049. end;
  1050. end;
  1051. end;
  1052. {*****************************************************************************
  1053. SecondProcInlineN
  1054. *****************************************************************************}
  1055. procedure secondprocinline(var p : ptree);
  1056. var st : psymtable;
  1057. oldprocsym : pprocsym;
  1058. para_size : longint;
  1059. oldprocinfo : tprocinfo;
  1060. { just dummies for genentrycode }
  1061. nostackframe,make_global : boolean;
  1062. proc_names : tstringcontainer;
  1063. inlineentrycode,inlineexitcode : paasmoutput;
  1064. oldexitlabel,oldexit2label,oldquickexitlabel:Pasmlabel;
  1065. begin
  1066. oldexitlabel:=aktexitlabel;
  1067. oldexit2label:=aktexit2label;
  1068. oldquickexitlabel:=quickexitlabel;
  1069. getlabel(aktexitlabel);
  1070. getlabel(aktexit2label);
  1071. oldprocsym:=aktprocsym;
  1072. oldprocinfo:=procinfo;
  1073. { set the return value }
  1074. aktprocsym:=p^.inlineprocsym;
  1075. procinfo.retdef:=aktprocsym^.definition^.retdef;
  1076. procinfo.retoffset:=p^.retoffset;
  1077. { arg space has been filled by the parent secondcall }
  1078. st:=aktprocsym^.definition^.localst;
  1079. { set it to the same lexical level }
  1080. st^.symtablelevel:=oldprocsym^.definition^.localst^.symtablelevel;
  1081. if st^.datasize>0 then
  1082. begin
  1083. st^.address_fixup:=gettempofsizepersistant(st^.datasize);
  1084. {$ifdef extdebug}
  1085. Comment(V_debug,'local symtable is at offset '+tostr(st^.address_fixup));
  1086. exprasmlist^.concat(new(pai_asm_comment,init(strpnew(
  1087. 'local symtable is at offset '+tostr(st^.address_fixup)))));
  1088. {$endif extdebug}
  1089. end;
  1090. {$ifdef extdebug}
  1091. exprasmlist^.concat(new(pai_asm_comment,init('Start of inlined proc')));
  1092. {$endif extdebug}
  1093. { takes care of local data initialization }
  1094. inlineentrycode:=new(paasmoutput,init);
  1095. inlineexitcode:=new(paasmoutput,init);
  1096. proc_names.init;
  1097. para_size:=p^.para_size;
  1098. make_global:=false; { to avoid warning }
  1099. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  1100. exprasmlist^.concatlist(inlineentrycode);
  1101. secondpass(p^.inlinetree);
  1102. genexitcode(inlineexitcode,0,false,true);
  1103. exprasmlist^.concatlist(inlineexitcode);
  1104. {$ifdef extdebug}
  1105. exprasmlist^.concat(new(pai_asm_comment,init('End of inlined proc')));
  1106. {$endif extdebug}
  1107. {we can free the local data now, reset also the fixup address }
  1108. if st^.datasize>0 then
  1109. begin
  1110. ungetpersistanttemp(st^.address_fixup);
  1111. st^.address_fixup:=0;
  1112. end;
  1113. aktprocsym:=oldprocsym;
  1114. freelabel(aktexitlabel);
  1115. freelabel(aktexit2label);
  1116. aktexitlabel:=oldexitlabel;
  1117. aktexit2label:=oldexit2label;
  1118. quickexitlabel:=oldquickexitlabel;
  1119. procinfo:=oldprocinfo;
  1120. end;
  1121. end.
  1122. {
  1123. $Log$
  1124. Revision 1.97 1999-08-04 13:45:18 florian
  1125. + floating point register variables !!
  1126. * pairegalloc is now generated for register variables
  1127. Revision 1.96 1999/08/04 00:22:41 florian
  1128. * renamed i386asm and i386base to cpuasm and cpubase
  1129. Revision 1.95 1999/08/03 22:02:34 peter
  1130. * moved bitmask constants to sets
  1131. * some other type/const renamings
  1132. Revision 1.94 1999/07/06 21:48:09 florian
  1133. * a lot bug fixes:
  1134. - po_external isn't any longer necessary for procedure compatibility
  1135. - m_tp_procvar is in -Sd now available
  1136. - error messages of procedure variables improved
  1137. - return values with init./finalization fixed
  1138. - data types with init./finalization aren't any longer allowed in variant
  1139. record
  1140. Revision 1.93 1999/06/22 13:31:24 peter
  1141. * merged
  1142. Revision 1.92 1999/06/16 09:32:45 peter
  1143. * merged
  1144. Revision 1.91 1999/06/14 17:47:47 peter
  1145. * merged
  1146. Revision 1.90.2.3 1999/06/22 13:30:08 peter
  1147. * fixed return with packenum
  1148. Revision 1.90.2.2 1999/06/16 09:30:44 peter
  1149. * fixed loading of ansistring when eax was already used
  1150. Revision 1.90.2.1 1999/06/14 17:24:42 peter
  1151. * fixed saving of registers with decr_ansistr
  1152. Revision 1.90 1999/06/02 10:11:40 florian
  1153. * make cycle fixed i.e. compilation with 0.99.10
  1154. * some fixes for qword
  1155. * start of register calling conventions
  1156. Revision 1.89 1999/05/28 15:59:46 pierre
  1157. * forgotten emitcall change in conditionnal
  1158. Revision 1.88 1999/05/28 11:00:49 peter
  1159. * removed ungettempoftype
  1160. Revision 1.87 1999/05/27 19:44:07 peter
  1161. * removed oldasm
  1162. * plabel -> pasmlabel
  1163. * -a switches to source writing automaticly
  1164. * assembler readers OOPed
  1165. * asmsymbol automaticly external
  1166. * jumptables and other label fixes for asm readers
  1167. Revision 1.86 1999/05/23 18:41:58 florian
  1168. * better error recovering in typed constants
  1169. * some problems with arrays of const fixed, some problems
  1170. due my previous
  1171. - the location type of array constructor is now LOC_MEM
  1172. - the pushing of high fixed
  1173. - parameter copying fixed
  1174. - zero temp. allocation removed
  1175. * small problem in the assembler writers fixed:
  1176. ref to nil wasn't written correctly
  1177. Revision 1.85 1999/05/21 13:54:44 peter
  1178. * NEWLAB for label as symbol
  1179. Revision 1.84 1999/05/18 22:34:26 pierre
  1180. * extedebug problem solved
  1181. Revision 1.83 1999/05/18 21:58:24 florian
  1182. * fixed some bugs related to temp. ansistrings and functions results
  1183. which return records/objects/arrays which need init/final.
  1184. Revision 1.82 1999/05/18 14:15:23 peter
  1185. * containsself fixes
  1186. * checktypes()
  1187. Revision 1.81 1999/05/18 09:52:17 peter
  1188. * procedure of object and addrn fixes
  1189. Revision 1.80 1999/05/17 23:51:37 peter
  1190. * with temp vars now use a reference with a persistant temp instead
  1191. of setting datasize
  1192. Revision 1.79 1999/05/17 21:56:59 florian
  1193. * new temporary ansistring handling
  1194. Revision 1.78 1999/05/01 13:24:02 peter
  1195. * merged nasm compiler
  1196. * old asm moved to oldasm/
  1197. Revision 1.77 1999/04/29 22:12:21 pierre
  1198. * fix for ID 388 removing real from stack was wrong
  1199. Revision 1.76 1999/04/25 22:33:19 pierre
  1200. * fix for TESTOBJEXT code
  1201. Revision 1.75 1999/04/19 09:45:46 pierre
  1202. + cdecl or stdcall push all args with longint size
  1203. * tempansi stuff cleaned up
  1204. Revision 1.74 1999/04/16 13:42:23 jonas
  1205. * more regalloc fixes (still not complete)
  1206. Revision 1.73 1999/04/16 10:26:56 pierre
  1207. * no add $0,%esp for cdecl functions without parameters
  1208. Revision 1.72 1999/04/09 08:41:48 peter
  1209. * define to get ansistring returns in ref instead of reg
  1210. Revision 1.71 1999/03/31 13:55:04 peter
  1211. * assembler inlining working for ag386bin
  1212. Revision 1.70 1999/03/24 23:16:46 peter
  1213. * fixed bugs 212,222,225,227,229,231,233
  1214. Revision 1.69 1999/02/25 21:02:21 peter
  1215. * ag386bin updates
  1216. + coff writer
  1217. Revision 1.68 1999/02/22 02:15:04 peter
  1218. * updates for ag386bin
  1219. Revision 1.67 1999/02/11 09:46:21 pierre
  1220. * fix for normal method calls inside static methods :
  1221. WARNING there were both parser and codegen errors !!
  1222. added static_call boolean to calln tree
  1223. Revision 1.66 1999/02/09 15:45:46 florian
  1224. + complex results for assembler functions, fixes bug0155
  1225. Revision 1.65 1999/02/08 11:29:04 pierre
  1226. * fix for bug0214
  1227. several problems where combined
  1228. search_class_member did not set srsymtable
  1229. => in do_member_read the call node got a wrong symtable
  1230. in cg386cal the vmt was pushed twice without chacking if it exists
  1231. now %esi is set to zero and pushed if not vmt
  1232. (not very efficient but should work !)
  1233. Revision 1.64 1999/02/04 10:49:39 florian
  1234. + range checking for ansi- and widestrings
  1235. * made it compilable with TP
  1236. Revision 1.63 1999/02/03 10:18:14 pierre
  1237. * conditionnal code for extended check of virtual methods
  1238. Revision 1.62 1999/02/02 23:52:32 florian
  1239. * problem with calls to method pointers in methods fixed
  1240. - double ansistrings temp management removed
  1241. Revision 1.61 1999/02/02 11:04:36 florian
  1242. * class destructors fixed, class instances weren't disposed correctly
  1243. Revision 1.60 1999/01/28 23:56:44 florian
  1244. * the reference in the result location of a function call wasn't resetted =>
  1245. problem with unallowed far pointer, is solved now
  1246. Revision 1.59 1999/01/27 00:13:52 florian
  1247. * "procedure of object"-stuff fixed
  1248. Revision 1.58 1999/01/21 22:10:35 peter
  1249. * fixed array of const
  1250. * generic platform independent high() support
  1251. Revision 1.57 1999/01/21 16:40:51 pierre
  1252. * fix for constructor inside with statements
  1253. Revision 1.56 1998/12/30 13:41:05 peter
  1254. * released valuepara
  1255. Revision 1.55 1998/12/22 13:10:58 florian
  1256. * memory leaks for ansistring type casts fixed
  1257. Revision 1.54 1998/12/19 00:23:41 florian
  1258. * ansistring memory leaks fixed
  1259. Revision 1.53 1998/12/11 00:02:47 peter
  1260. + globtype,tokens,version unit splitted from globals
  1261. Revision 1.52 1998/12/10 14:39:29 florian
  1262. * bug with p(const a : ansistring) fixed
  1263. * duplicate constant ansistrings were handled wrong, fixed
  1264. Revision 1.51 1998/12/10 09:47:15 florian
  1265. + basic operations with int64/qord (compiler with -dint64)
  1266. + rtti of enumerations extended: names are now written
  1267. Revision 1.50 1998/12/06 13:12:44 florian
  1268. * better code generation for classes which are passed as parameters to
  1269. subroutines
  1270. Revision 1.49 1998/11/30 09:43:00 pierre
  1271. * some range check bugs fixed (still not working !)
  1272. + added DLL writing support for win32 (also accepts variables)
  1273. + TempAnsi for code that could be used for Temporary ansi strings
  1274. handling
  1275. Revision 1.48 1998/11/27 14:50:30 peter
  1276. + open strings, $P switch support
  1277. Revision 1.47 1998/11/26 21:30:03 peter
  1278. * fix for valuepara
  1279. Revision 1.46 1998/11/26 14:39:10 peter
  1280. * ansistring -> pchar fixed
  1281. * ansistring constants fixed
  1282. * ansistring constants are now written once
  1283. Revision 1.45 1998/11/18 15:44:07 peter
  1284. * VALUEPARA for tp7 compatible value parameters
  1285. Revision 1.44 1998/11/16 15:35:36 peter
  1286. * rename laod/copystring -> load/copyshortstring
  1287. * fixed int-bool cnv bug
  1288. + char-ansistring conversion
  1289. Revision 1.43 1998/11/15 16:32:33 florian
  1290. * some stuff of Pavel implement (win32 dll creation)
  1291. * bug with ansistring function results fixed
  1292. Revision 1.42 1998/11/13 15:40:13 pierre
  1293. + added -Se in Makefile cvstest target
  1294. + lexlevel cleanup
  1295. normal_function_level main_program_level and unit_init_level defined
  1296. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  1297. (test added in code !)
  1298. * -Un option was wrong
  1299. * _FAIL and _SELF only keyword inside
  1300. constructors and methods respectively
  1301. Revision 1.41 1998/11/12 11:19:40 pierre
  1302. * fix for first line of function break
  1303. Revision 1.40 1998/11/10 10:09:08 peter
  1304. * va_list -> array of const
  1305. Revision 1.39 1998/11/09 11:44:33 peter
  1306. + va_list for printf support
  1307. Revision 1.38 1998/10/21 15:12:49 pierre
  1308. * bug fix for IOCHECK inside a procedure with iocheck modifier
  1309. * removed the GPF for unexistant overloading
  1310. (firstcall was called with procedinition=nil !)
  1311. * changed typen to what Florian proposed
  1312. gentypenode(p : pdef) sets the typenodetype field
  1313. and resulttype is only set if inside bt_type block !
  1314. Revision 1.37 1998/10/21 08:39:57 florian
  1315. + ansistring operator +
  1316. + $h and string[n] for n>255 added
  1317. * small problem with TP fixed
  1318. Revision 1.36 1998/10/20 08:06:39 pierre
  1319. * several memory corruptions due to double freemem solved
  1320. => never use p^.loc.location:=p^.left^.loc.location;
  1321. + finally I added now by default
  1322. that ra386dir translates global and unit symbols
  1323. + added a first field in tsymtable and
  1324. a nextsym field in tsym
  1325. (this allows to obtain ordered type info for
  1326. records and objects in gdb !)
  1327. Revision 1.35 1998/10/16 08:51:45 peter
  1328. + target_os.stackalignment
  1329. + stack can be aligned at 2 or 4 byte boundaries
  1330. Revision 1.34 1998/10/09 08:56:22 pierre
  1331. * several memory leaks fixed
  1332. Revision 1.33 1998/10/06 17:16:39 pierre
  1333. * some memory leaks fixed (thanks to Peter for heaptrc !)
  1334. Revision 1.32 1998/10/01 09:22:52 peter
  1335. * fixed value openarray
  1336. * ungettemp of arrayconstruct
  1337. Revision 1.31 1998/09/28 16:57:15 pierre
  1338. * changed all length(p^.value_str^) into str_length(p)
  1339. to get it work with and without ansistrings
  1340. * changed sourcefiles field of tmodule to a pointer
  1341. Revision 1.30 1998/09/26 15:03:02 florian
  1342. * small problems with DOM and excpetions fixed (code generation
  1343. of raise was wrong and self was sometimes destroyed :()
  1344. Revision 1.29 1998/09/25 00:04:00 florian
  1345. * problems when calling class methods fixed
  1346. Revision 1.28 1998/09/24 14:27:37 peter
  1347. * some better support for openarray
  1348. Revision 1.27 1998/09/24 09:02:13 peter
  1349. * rewritten isconvertable to use case
  1350. * array of .. and single variable are compatible
  1351. Revision 1.26 1998/09/21 08:45:06 pierre
  1352. + added vmt_offset in tobjectdef.write for fututre use
  1353. (first steps to have objects without vmt if no virtual !!)
  1354. + added fpu_used field for tabstractprocdef :
  1355. sets this level to 2 if the functions return with value in FPU
  1356. (is then set to correct value at parsing of implementation)
  1357. THIS MIGHT refuse some code with FPU expression too complex
  1358. that were accepted before and even in some cases
  1359. that don't overflow in fact
  1360. ( like if f : float; is a forward that finally in implementation
  1361. only uses one fpu register !!)
  1362. Nevertheless I think that it will improve security on
  1363. FPU operations !!
  1364. * most other changes only for UseBrowser code
  1365. (added symtable references for record and objects)
  1366. local switch for refs to args and local of each function
  1367. (static symtable still missing)
  1368. UseBrowser still not stable and probably broken by
  1369. the definition hash array !!
  1370. Revision 1.25 1998/09/20 12:26:35 peter
  1371. * merged fixes
  1372. Revision 1.24 1998/09/17 09:42:10 peter
  1373. + pass_2 for cg386
  1374. * Message() -> CGMessage() for pass_1/pass_2
  1375. Revision 1.23 1998/09/14 10:43:45 peter
  1376. * all internal RTL functions start with FPC_
  1377. Revision 1.22.2.1 1998/09/20 12:20:06 peter
  1378. * Fixed stack not on 4 byte boundary when doing a call
  1379. Revision 1.22 1998/09/04 08:41:37 peter
  1380. * updated some error CGMessages
  1381. Revision 1.21 1998/09/01 12:47:57 peter
  1382. * use pdef^.size instead of orddef^.typ
  1383. Revision 1.20 1998/08/31 12:22:15 peter
  1384. * secondinline moved to cg386inl
  1385. Revision 1.19 1998/08/31 08:52:03 peter
  1386. * fixed error 10 with succ() and pref()
  1387. Revision 1.18 1998/08/20 21:36:38 peter
  1388. * fixed 'with object do' bug
  1389. Revision 1.17 1998/08/19 16:07:36 jonas
  1390. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1391. Revision 1.16 1998/08/18 09:24:36 pierre
  1392. * small warning position bug fixed
  1393. * support_mmx switches splitting was missing
  1394. * rhide error and warning output corrected
  1395. Revision 1.15 1998/08/13 11:00:09 peter
  1396. * fixed procedure<>procedure construct
  1397. Revision 1.14 1998/08/11 14:05:33 peter
  1398. * fixed sizeof(array of char)
  1399. Revision 1.13 1998/08/10 14:49:45 peter
  1400. + localswitches, moduleswitches, globalswitches splitting
  1401. Revision 1.12 1998/07/30 13:30:31 florian
  1402. * final implemenation of exception support, maybe it needs
  1403. some fixes :)
  1404. Revision 1.11 1998/07/24 22:16:52 florian
  1405. * internal error 10 together with array access fixed. I hope
  1406. that's the final fix.
  1407. Revision 1.10 1998/07/18 22:54:23 florian
  1408. * some ansi/wide/longstring support fixed:
  1409. o parameter passing
  1410. o returning as result from functions
  1411. Revision 1.9 1998/07/07 17:40:37 peter
  1412. * packrecords 4 works
  1413. * word aligning of parameters
  1414. Revision 1.8 1998/07/06 15:51:15 michael
  1415. Added length checking for string reading
  1416. Revision 1.7 1998/07/06 14:19:51 michael
  1417. + Added calls for reading/writing ansistrings
  1418. Revision 1.6 1998/07/01 15:28:48 peter
  1419. + better writeln/readln handling, now 100% like tp7
  1420. Revision 1.5 1998/06/25 14:04:17 peter
  1421. + internal inc/dec
  1422. Revision 1.4 1998/06/25 08:48:06 florian
  1423. * first version of rtti support
  1424. Revision 1.3 1998/06/09 16:01:33 pierre
  1425. + added procedure directive parsing for procvars
  1426. (accepted are popstack cdecl and pascal)
  1427. + added C vars with the following syntax
  1428. var C calias 'true_c_name';(can be followed by external)
  1429. reason is that you must add the Cprefix
  1430. which is target dependent
  1431. Revision 1.2 1998/06/08 13:13:29 pierre
  1432. + temporary variables now in temp_gen.pas unit
  1433. because it is processor independent
  1434. * mppc68k.bat modified to undefine i386 and support_mmx
  1435. (which are defaults for i386)
  1436. Revision 1.1 1998/06/05 17:44:10 peter
  1437. * splitted cgi386
  1438. }