cg386cal.pas 70 KB

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