cg386cal.pas 65 KB

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