ncgutil.pas 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Helper routines for all code generators
  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 ncgutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. cginfo,cpubase,aasmbase,aasmtai,aasmcpu,
  24. rgobj;
  25. type
  26. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  27. tmaybesave = record
  28. saved : boolean;
  29. ref : treference;
  30. end;
  31. procedure firstcomplex(p : tbinarynode);
  32. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  33. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  34. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  35. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  36. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  37. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  38. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  39. procedure push_value_para(p:tnode;inlined,is_cdecl:boolean;
  40. para_offset:longint;alignment : longint);
  41. procedure genentrycode(list : TAAsmoutput;
  42. make_global:boolean;
  43. stackframe:longint;
  44. var parasize:longint;var nostackframe:boolean;
  45. inlined : boolean);
  46. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  47. procedure genimplicitunitinit(list : TAAsmoutput);
  48. procedure genimplicitunitfinal(list : TAAsmoutput);
  49. implementation
  50. uses
  51. {$ifdef Delphi}
  52. Sysutils,
  53. {$else}
  54. strings,
  55. {$endif}
  56. cutils,cclasses,globtype,globals,systems,verbose,
  57. symbase,symconst,symtype,symsym,symdef,symtable,types,
  58. fmodule,
  59. cgbase,regvars,
  60. {$ifdef GDB}
  61. gdb,
  62. {$endif GDB}
  63. ncon,
  64. tgobj,cpuinfo,cgobj,cgcpu,cg64f32;
  65. {*****************************************************************************
  66. Misc Helpers
  67. *****************************************************************************}
  68. { DO NOT RELY on the fact that the tnode is not yet swaped
  69. because of inlining code PM }
  70. procedure firstcomplex(p : tbinarynode);
  71. var
  72. hp : tnode;
  73. begin
  74. { always calculate boolean AND and OR from left to right }
  75. if (p.nodetype in [orn,andn]) and
  76. (p.left.resulttype.def.deftype=orddef) and
  77. (torddef(p.left.resulttype.def).typ in [bool8bit,bool16bit,bool32bit]) then
  78. begin
  79. { p.swaped:=false}
  80. if nf_swaped in p.flags then
  81. internalerror(234234);
  82. end
  83. else
  84. if (((p.location.loc=LOC_FPUREGISTER) and
  85. (p.right.registersfpu > p.left.registersfpu)) or
  86. ((((p.left.registersfpu = 0) and
  87. (p.right.registersfpu = 0)) or
  88. (p.location.loc<>LOC_FPUREGISTER)) and
  89. (p.left.registers32<p.right.registers32))) and
  90. { the following check is appropriate, because all }
  91. { 4 registers are rarely used and it is thereby }
  92. { achieved that the extra code is being dropped }
  93. { by exchanging not commutative operators }
  94. (p.right.registers32<=c_countusableregsint) then
  95. begin
  96. hp:=p.left;
  97. p.left:=p.right;
  98. p.right:=hp;
  99. if nf_swaped in p.flags then
  100. exclude(p.flags,nf_swaped)
  101. else
  102. include(p.flags,nf_swaped);
  103. end;
  104. end;
  105. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  106. {
  107. produces jumps to true respectively false labels using boolean expressions
  108. depending on whether the loading of regvars is currently being
  109. synchronized manually (such as in an if-node) or automatically (most of
  110. the other cases where this procedure is called), loadregvars can be
  111. "lr_load_regvars" or "lr_dont_load_regvars"
  112. }
  113. var
  114. opsize : tcgsize;
  115. storepos : tfileposinfo;
  116. begin
  117. if nf_error in p.flags then
  118. exit;
  119. storepos:=aktfilepos;
  120. aktfilepos:=p.fileinfo;
  121. if is_boolean(p.resulttype.def) then
  122. begin
  123. if loadregvars = lr_load_regvars then
  124. load_all_regvars(list);
  125. if is_constboolnode(p) then
  126. begin
  127. if tordconstnode(p).value<>0 then
  128. cg.a_jmp_always(list,truelabel)
  129. else
  130. cg.a_jmp_always(list,falselabel)
  131. end
  132. else
  133. begin
  134. opsize:=def_cgsize(p.resulttype.def);
  135. case p.location.loc of
  136. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  137. begin
  138. if (p.location.loc = LOC_CREGISTER) then
  139. load_regvar_reg(list,p.location.register);
  140. cg.a_cmp_const_loc_label(list,opsize,OC_NE,
  141. 0,p.location,truelabel);
  142. { !!! should happen right after cmp (JM) }
  143. location_release(list,p.location);
  144. cg.a_jmp_always(list,falselabel);
  145. end;
  146. LOC_FLAGS :
  147. begin
  148. cg.a_jmp_flags(list,p.location.resflags,
  149. truelabel);
  150. cg.a_jmp_always(list,falselabel);
  151. end;
  152. end;
  153. end;
  154. end
  155. else
  156. internalerror(200112305);
  157. aktfilepos:=storepos;
  158. end;
  159. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  160. begin
  161. case t.loc of
  162. LOC_REGISTER:
  163. begin
  164. { can't be a regvar, since it would be LOC_CREGISTER then }
  165. exclude(regs,t.register);
  166. if t.registerhigh <> R_NO then
  167. exclude(regs,t.registerhigh);
  168. end;
  169. LOC_CREFERENCE,LOC_REFERENCE:
  170. begin
  171. if not(cs_regalloc in aktglobalswitches) or
  172. (t.reference.base in rg.usableregsint) then
  173. exclude(regs,t.reference.base);
  174. if not(cs_regalloc in aktglobalswitches) or
  175. (t.reference.index in rg.usableregsint) then
  176. exclude(regs,t.reference.index);
  177. end;
  178. end;
  179. end;
  180. {*****************************************************************************
  181. TLocation
  182. *****************************************************************************}
  183. { 32-bit version }
  184. procedure location_force_reg32(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  185. var
  186. hregister,
  187. hregisterhi : tregister;
  188. hreg64 : tregister64;
  189. hl : tasmlabel;
  190. begin
  191. { handle transformations to 64bit separate }
  192. if dst_size in [OS_64,OS_S64] then
  193. begin
  194. if not (l.size in [OS_64,OS_S64]) then
  195. begin
  196. { load a smaller size to OS_64 }
  197. if l.loc=LOC_REGISTER then
  198. hregister:=rg.makeregsize(l.registerlow,OS_INT)
  199. else
  200. hregister:=rg.getregisterint(list);
  201. { load value in low register }
  202. case l.loc of
  203. LOC_FLAGS :
  204. cg.g_flags2reg(list,l.resflags,hregister);
  205. LOC_JUMP :
  206. begin
  207. cg.a_label(list,truelabel);
  208. cg.a_load_const_reg(list,OS_INT,1,hregister);
  209. getlabel(hl);
  210. cg.a_jmp_always(list,hl);
  211. cg.a_label(list,falselabel);
  212. cg.a_load_const_reg(list,OS_INT,0,hregister);
  213. cg.a_label(list,hl);
  214. end;
  215. else
  216. cg.a_load_loc_reg(list,l,hregister);
  217. end;
  218. { reset hi part, take care of the signed bit of the current value }
  219. hregisterhi:=rg.getregisterint(list);
  220. if (dst_size=OS_S64) and
  221. (l.size in [OS_S8,OS_S16,OS_S32]) then
  222. begin
  223. if l.loc=LOC_CONSTANT then
  224. begin
  225. if (longint(l.value)<0) then
  226. cg.a_load_const_reg(list,OS_32,$ffffffff,hregisterhi)
  227. else
  228. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  229. end
  230. else
  231. begin
  232. cg.a_load_reg_reg(list,OS_32,hregister,hregisterhi);
  233. cg.a_op_const_reg(list,OP_SAR,31,hregisterhi);
  234. end;
  235. end
  236. else
  237. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  238. location_reset(l,LOC_REGISTER,dst_size);
  239. l.registerlow:=hregister;
  240. l.registerhigh:=hregisterhi;
  241. end
  242. else
  243. begin
  244. { 64bit to 64bit }
  245. if (l.loc=LOC_REGISTER) or
  246. ((l.loc=LOC_CREGISTER) and maybeconst) then
  247. begin
  248. hregister:=l.registerlow;
  249. hregisterhi:=l.registerhigh;
  250. end
  251. else
  252. begin
  253. hregister:=rg.getregisterint(list);
  254. hregisterhi:=rg.getregisterint(list);
  255. end;
  256. hreg64.reglo:=hregister;
  257. hreg64.reghi:=hregisterhi;
  258. { load value in new register }
  259. cg64.a_load64_loc_reg(list,l,hreg64);
  260. location_reset(l,LOC_REGISTER,dst_size);
  261. l.registerlow:=hregister;
  262. l.registerhigh:=hregisterhi;
  263. end;
  264. end
  265. else
  266. begin
  267. { transformations to 32bit or smaller }
  268. if l.loc=LOC_REGISTER then
  269. begin
  270. { if the previous was 64bit release the high register }
  271. if l.size in [OS_64,OS_S64] then
  272. begin
  273. rg.ungetregisterint(list,l.registerhigh);
  274. l.registerhigh:=R_NO;
  275. end;
  276. hregister:=l.register;
  277. end
  278. else
  279. begin
  280. { get new register }
  281. if (l.loc=LOC_CREGISTER) and
  282. maybeconst and
  283. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  284. hregister:=l.register
  285. else
  286. hregister:=rg.getregisterint(list);
  287. end;
  288. hregister:=rg.makeregsize(hregister,dst_size);
  289. { load value in new register }
  290. case l.loc of
  291. LOC_FLAGS :
  292. cg.g_flags2reg(list,l.resflags,hregister);
  293. LOC_JUMP :
  294. begin
  295. cg.a_label(list,truelabel);
  296. cg.a_load_const_reg(list,dst_size,1,hregister);
  297. getlabel(hl);
  298. cg.a_jmp_always(list,hl);
  299. cg.a_label(list,falselabel);
  300. cg.a_load_const_reg(list,dst_size,0,hregister);
  301. cg.a_label(list,hl);
  302. end;
  303. else
  304. begin
  305. { load_loc_reg can only handle size >= l.size, when the
  306. new size is smaller then we need to adjust the size
  307. of the orignal and maybe recalculate l.register for i386 }
  308. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  309. begin
  310. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  311. l.register:=rg.makeregsize(l.register,dst_size);
  312. l.size:=dst_size;
  313. end;
  314. cg.a_load_loc_reg(list,l,hregister);
  315. end;
  316. end;
  317. location_reset(l,LOC_REGISTER,dst_size);
  318. l.register:=hregister;
  319. end;
  320. end;
  321. { 64-bit version }
  322. procedure location_force_reg64(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  323. var
  324. hregister : tregister;
  325. hl : tasmlabel;
  326. begin
  327. { handle transformations to 64bit separate }
  328. if dst_size in [OS_64,OS_S64] then
  329. begin
  330. { load a smaller size to OS_64 }
  331. if l.loc=LOC_REGISTER then
  332. hregister:=rg.makeregsize(l.register,OS_INT)
  333. else
  334. hregister:=rg.getregisterint(list);
  335. { load value in low register }
  336. case l.loc of
  337. LOC_FLAGS :
  338. cg.g_flags2reg(list,l.resflags,hregister);
  339. LOC_JUMP :
  340. begin
  341. cg.a_label(list,truelabel);
  342. cg.a_load_const_reg(list,OS_INT,1,hregister);
  343. getlabel(hl);
  344. cg.a_jmp_always(list,hl);
  345. cg.a_label(list,falselabel);
  346. cg.a_load_const_reg(list,OS_INT,0,hregister);
  347. cg.a_label(list,hl);
  348. end;
  349. else
  350. cg.a_load_loc_reg(list,l,hregister);
  351. end;
  352. location_reset(l,LOC_REGISTER,dst_size);
  353. l.register:=hregister;
  354. end
  355. else
  356. begin
  357. { transformations to 32bit or smaller }
  358. if l.loc=LOC_REGISTER then
  359. begin
  360. hregister:=l.register;
  361. end
  362. else
  363. begin
  364. { get new register }
  365. if (l.loc=LOC_CREGISTER) and
  366. maybeconst and
  367. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  368. hregister:=l.register
  369. else
  370. hregister:=rg.getregisterint(list);
  371. end;
  372. hregister:=rg.makeregsize(hregister,dst_size);
  373. { load value in new register }
  374. case l.loc of
  375. LOC_FLAGS :
  376. cg.g_flags2reg(list,l.resflags,hregister);
  377. LOC_JUMP :
  378. begin
  379. cg.a_label(list,truelabel);
  380. cg.a_load_const_reg(list,dst_size,1,hregister);
  381. getlabel(hl);
  382. cg.a_jmp_always(list,hl);
  383. cg.a_label(list,falselabel);
  384. cg.a_load_const_reg(list,dst_size,0,hregister);
  385. cg.a_label(list,hl);
  386. end;
  387. else
  388. begin
  389. { load_loc_reg can only handle size >= l.size, when the
  390. new size is smaller then we need to adjust the size
  391. of the orignal and maybe recalculate l.register for i386 }
  392. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  393. begin
  394. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  395. l.register:=rg.makeregsize(l.register,dst_size);
  396. l.size:=dst_size;
  397. end;
  398. cg.a_load_loc_reg(list,l,hregister);
  399. end;
  400. end;
  401. location_reset(l,LOC_REGISTER,dst_size);
  402. l.register:=hregister;
  403. end;
  404. end;
  405. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  406. begin
  407. { release previous location before demanding a new register }
  408. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  409. begin
  410. location_freetemp(list,l);
  411. location_release(list,l);
  412. end;
  413. if sizeof(aword) < 8 then
  414. location_force_reg32(list, l, dst_size, maybeconst)
  415. else
  416. location_force_reg64(list, l, dst_size, maybeconst);
  417. end;
  418. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  419. var
  420. r : treference;
  421. begin
  422. case l.loc of
  423. LOC_FPUREGISTER,
  424. LOC_CFPUREGISTER :
  425. begin
  426. tg.gettempofsizereference(list,TCGSize2Size[l.size],r);
  427. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  428. location_reset(l,LOC_REFERENCE,l.size);
  429. l.reference:=r;
  430. end;
  431. LOC_CONSTANT,
  432. LOC_REGISTER,
  433. LOC_CREGISTER :
  434. begin
  435. tg.gettempofsizereference(list,TCGSize2Size[l.size],r);
  436. if l.size in [OS_64,OS_S64] then
  437. cg64.a_load64_loc_ref(list,l,r)
  438. else
  439. cg.a_load_loc_ref(list,l,r);
  440. location_reset(l,LOC_REFERENCE,l.size);
  441. l.reference:=r;
  442. end;
  443. LOC_CREFERENCE,
  444. LOC_REFERENCE : ;
  445. else
  446. internalerror(200203219);
  447. end;
  448. end;
  449. {*****************************************************************************
  450. Maybe_Save
  451. *****************************************************************************}
  452. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  453. begin
  454. s.saved:=false;
  455. if l.loc=LOC_CREGISTER then
  456. begin
  457. s.saved:=true;
  458. exit;
  459. end;
  460. if needed>rg.countunusedregsint then
  461. begin
  462. case l.loc of
  463. LOC_REGISTER :
  464. begin
  465. if l.size in [OS_64,OS_S64] then
  466. begin
  467. tg.gettempofsizereference(exprasmlist,8,s.ref);
  468. cg64.a_load64_reg_ref(exprasmlist,joinreg64(l.registerlow,l.registerhigh),s.ref);
  469. end
  470. else
  471. begin
  472. tg.gettempofsizereference(exprasmlist,TCGSize2Size[l.size],s.ref);
  473. cg.a_load_reg_ref(exprasmlist,l.size,l.register,s.ref);
  474. end;
  475. location_release(exprasmlist,l);
  476. s.saved:=true;
  477. end;
  478. LOC_REFERENCE,
  479. LOC_CREFERENCE :
  480. begin
  481. if ((l.reference.base<>R_NO) or
  482. (l.reference.index<>R_NO)) then
  483. begin
  484. { load address into a single base register }
  485. cg.a_loadaddr_ref_reg(list,l.reference,l.reference.base);
  486. { save base register }
  487. tg.gettempofsizereference(exprasmlist,TCGSize2Size[OS_ADDR],s.ref);
  488. cg.a_load_reg_ref(exprasmlist,OS_ADDR,l.reference.base,s.ref);
  489. { release }
  490. location_release(exprasmlist,l);
  491. s.saved:=true;
  492. end;
  493. end;
  494. end;
  495. end;
  496. end;
  497. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  498. begin
  499. if not s.saved then
  500. exit;
  501. if l.loc=LOC_CREGISTER then
  502. begin
  503. load_regvar_reg(list,l.register);
  504. exit;
  505. end;
  506. case l.loc of
  507. LOC_REGISTER :
  508. begin
  509. if l.size in [OS_64,OS_S64] then
  510. begin
  511. l.registerlow:=rg.getregisterint(exprasmlist);
  512. l.registerhigh:=rg.getregisterint(exprasmlist);
  513. cg64.a_load64_ref_reg(exprasmlist,s.ref,joinreg64(l.registerlow,l.registerhigh));
  514. end
  515. else
  516. begin
  517. l.register:=rg.getregisterint(exprasmlist);
  518. cg.a_load_ref_reg(exprasmlist,OS_INT,s.ref,l.register);
  519. end;
  520. end;
  521. LOC_CREFERENCE,
  522. LOC_REFERENCE :
  523. begin
  524. reference_reset(l.reference);
  525. l.reference.base:=rg.getaddressregister(exprasmlist);
  526. cg.a_load_ref_reg(exprasmlist,OS_ADDR,s.ref,l.reference.base);
  527. end;
  528. end;
  529. tg.ungetiftemp(exprasmlist,s.ref);
  530. end;
  531. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  532. begin
  533. if needed>=maxfpuregs then
  534. begin
  535. if l.loc = LOC_FPUREGISTER then
  536. begin
  537. location_force_mem(list,l);
  538. maybe_pushfpu:=true;
  539. end
  540. else
  541. maybe_pushfpu:=false;
  542. end
  543. else
  544. maybe_pushfpu:=false;
  545. end;
  546. {*****************************************************************************
  547. Push Value Para
  548. *****************************************************************************}
  549. procedure push_value_para(p:tnode;inlined,is_cdecl:boolean;
  550. para_offset:longint;alignment : longint);
  551. var
  552. tempreference : treference;
  553. href : treference;
  554. hreg : tregister;
  555. sizetopush,
  556. size : longint;
  557. cgsize : tcgsize;
  558. begin
  559. { Move flags and jump in register to make it less complex }
  560. if p.location.loc in [LOC_FLAGS,LOC_JUMP] then
  561. location_force_reg(exprasmlist,p.location,def_cgsize(p.resulttype.def),false);
  562. { Handle Floating point types differently }
  563. if p.resulttype.def.deftype=floatdef then
  564. begin
  565. case p.location.loc of
  566. LOC_FPUREGISTER,
  567. LOC_CFPUREGISTER:
  568. begin
  569. size:=align(tfloatdef(p.resulttype.def).size,alignment);
  570. inc(pushedparasize,size);
  571. if not inlined then
  572. cg.a_op_const_reg(exprasmlist,OP_SUB,size,STACK_POINTER_REG);
  573. {$ifdef GDB}
  574. if (cs_debuginfo in aktmoduleswitches) and
  575. (exprasmList.first=exprasmList.last) then
  576. exprasmList.concat(Tai_force_line.Create);
  577. {$endif GDB}
  578. { this is the easiest case for inlined !! }
  579. if inlined then
  580. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize)
  581. else
  582. reference_reset_base(href,stack_pointer_reg,0);
  583. cg.a_loadfpu_reg_ref(exprasmlist,
  584. def_cgsize(p.resulttype.def),p.location.register,href);
  585. end;
  586. LOC_REFERENCE,
  587. LOC_CREFERENCE :
  588. begin
  589. sizetopush:=align(p.resulttype.def.size,alignment);
  590. tempreference:=p.location.reference;
  591. inc(tempreference.offset,sizetopush);
  592. while (sizetopush>0) do
  593. begin
  594. if sizetopush>=4 then
  595. begin
  596. cgsize:=OS_32;
  597. inc(pushedparasize,4);
  598. dec(tempreference.offset,4);
  599. dec(sizetopush,4);
  600. end
  601. else
  602. begin
  603. cgsize:=OS_16;
  604. inc(pushedparasize,2);
  605. dec(tempreference.offset,2);
  606. dec(sizetopush,2);
  607. end;
  608. if inlined then
  609. begin
  610. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  611. cg.a_load_ref_ref(exprasmlist,cgsize,tempreference,href);
  612. end
  613. else
  614. cg.a_param_ref(exprasmlist,cgsize,tempreference,-1);
  615. end;
  616. end;
  617. else
  618. internalerror(200204243);
  619. end;
  620. end
  621. else
  622. begin
  623. { call by value open array ? }
  624. if is_cdecl and
  625. push_addr_param(p.resulttype.def) then
  626. begin
  627. if not (p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  628. internalerror(200204241);
  629. { push on stack }
  630. size:=align(p.resulttype.def.size,alignment);
  631. inc(pushedparasize,size);
  632. cg.a_op_const_reg(exprasmlist,OP_SUB,size,STACK_POINTER_REG);
  633. reference_reset_base(href,STACK_POINTER_REG,0);
  634. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false);
  635. end
  636. else
  637. begin
  638. case p.location.loc of
  639. LOC_CONSTANT,
  640. LOC_REGISTER,
  641. LOC_CREGISTER,
  642. LOC_REFERENCE,
  643. LOC_CREFERENCE :
  644. begin
  645. cgsize:=def_cgsize(p.resulttype.def);
  646. if cgsize in [OS_64,OS_S64] then
  647. begin
  648. inc(pushedparasize,8);
  649. if inlined then
  650. begin
  651. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  652. cg64.a_load64_loc_ref(exprasmlist,p.location,href);
  653. end
  654. else
  655. cg64.a_param64_loc(exprasmlist,p.location,-1);
  656. end
  657. else
  658. begin
  659. case cgsize of
  660. OS_8,OS_S8 :
  661. begin
  662. if alignment=4 then
  663. cgsize:=OS_32
  664. else
  665. cgsize:=OS_16;
  666. end;
  667. OS_16,OS_S16 :
  668. begin
  669. if alignment=4 then
  670. cgsize:=OS_32;
  671. end;
  672. end;
  673. { update register to use to match alignment }
  674. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  675. begin
  676. hreg:=p.location.register;
  677. p.location.register:=rg.makeregsize(p.location.register,cgsize);
  678. end;
  679. inc(pushedparasize,alignment);
  680. if inlined then
  681. begin
  682. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  683. cg.a_load_loc_ref(exprasmlist,p.location,href);
  684. end
  685. else
  686. cg.a_param_loc(exprasmlist,p.location,-1);
  687. { restore old register }
  688. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  689. p.location.register:=hreg;
  690. end;
  691. location_release(exprasmlist,p.location);
  692. end;
  693. {$ifdef SUPPORT_MMX}
  694. LOC_MMXREGISTER,
  695. LOC_CMMXREGISTER:
  696. begin
  697. inc(pushedparasize,8);
  698. if inlined then
  699. begin
  700. reference_reset_base(href,procinfo^.framepointer,para_offset-pushedparasize);
  701. cg.a_loadmm_reg_ref(exprasmlist,p.location.register,href);
  702. end
  703. else
  704. cg.a_parammm_reg(exprasmlist,p.location.register);
  705. end;
  706. {$endif SUPPORT_MMX}
  707. else
  708. internalerror(200204241);
  709. end;
  710. end;
  711. end;
  712. end;
  713. {****************************************************************************
  714. Entry/Exit Code
  715. ****************************************************************************}
  716. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  717. var
  718. href1,href2 : treference;
  719. list : taasmoutput;
  720. begin
  721. list:=taasmoutput(arg);
  722. if (tsym(p).typ=varsym) and
  723. (tvarsym(p).varspez=vs_value) and
  724. (push_addr_param(tvarsym(p).vartype.def)) then
  725. begin
  726. reference_reset_base(href1,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  727. if is_open_array(tvarsym(p).vartype.def) or
  728. is_array_of_const(tvarsym(p).vartype.def) then
  729. cg.g_copyvaluepara_openarray(list,href1,tarraydef(tvarsym(p).vartype.def).elesize)
  730. else
  731. begin
  732. reference_reset_base(href2,procinfo^.framepointer,-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup);
  733. if is_shortstring(tvarsym(p).vartype.def) then
  734. cg.g_copyshortstring(list,href1,href2,tstringdef(tvarsym(p).vartype.def).len,false,true)
  735. else
  736. cg.g_concatcopy(list,href1,href2,tvarsym(p).vartype.def.size,true,true);
  737. end;
  738. end;
  739. end;
  740. procedure initialize_threadvar(p : tnamedindexitem;arg:pointer);
  741. var
  742. href : treference;
  743. list : taasmoutput;
  744. begin
  745. list:=taasmoutput(arg);
  746. if (tsym(p).typ=varsym) and
  747. (vo_is_thread_var in tvarsym(p).varoptions) then
  748. begin
  749. cg.a_param_const(list,OS_INT,tvarsym(p).getsize,2);
  750. reference_reset_symbol(href,newasmsymbol(tvarsym(p).mangledname),0);
  751. cg.a_paramaddr_ref(list,href,2);
  752. rg.saveregvars(list,all_registers);
  753. cg.a_call_name(list,'FPC_INIT_THREADVAR');
  754. end;
  755. end;
  756. { generates the code for initialisation of local data }
  757. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  758. var
  759. href : treference;
  760. list : taasmoutput;
  761. begin
  762. list:=taasmoutput(arg);
  763. if (tsym(p).typ=varsym) and
  764. assigned(tvarsym(p).vartype.def) and
  765. not(is_class(tvarsym(p).vartype.def)) and
  766. tvarsym(p).vartype.def.needs_inittable then
  767. begin
  768. if assigned(procinfo) then
  769. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  770. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  771. reference_reset_base(href,procinfo^.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  772. else
  773. reference_reset_symbol(href,newasmsymbol(tvarsym(p).mangledname),0);
  774. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  775. end;
  776. end;
  777. { generates the code for finalisation of local data }
  778. procedure finalize_data(p : tnamedindexitem;arg:pointer);
  779. var
  780. href : treference;
  781. list : taasmoutput;
  782. begin
  783. list:=taasmoutput(arg);
  784. if (tsym(p).typ=varsym) and
  785. assigned(tvarsym(p).vartype.def) and
  786. not(is_class(tvarsym(p).vartype.def)) and
  787. tvarsym(p).vartype.def.needs_inittable then
  788. begin
  789. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  790. reference_reset_base(href,procinfo^.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  791. else
  792. reference_reset_symbol(href,newasmsymbol(tvarsym(p).mangledname),0);
  793. cg.g_finalize(list,tvarsym(p).vartype.def,href,false);
  794. end;
  795. end;
  796. { generates the code for incrementing the reference count of parameters and
  797. initialize out parameters }
  798. procedure init_paras(p : tnamedindexitem;arg:pointer);
  799. var
  800. href : treference;
  801. tmpreg : tregister;
  802. list : taasmoutput;
  803. begin
  804. list:=taasmoutput(arg);
  805. if (tsym(p).typ=varsym) and
  806. not is_class(tvarsym(p).vartype.def) and
  807. tvarsym(p).vartype.def.needs_inittable then
  808. begin
  809. case tvarsym(p).varspez of
  810. vs_value :
  811. begin
  812. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  813. if assigned(tvarsym(p).localvarsym) then
  814. reference_reset_base(href,procinfo^.framepointer,
  815. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  816. else
  817. reference_reset_base(href,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  818. cg.g_incrrefcount(list,tvarsym(p).vartype.def,href);
  819. end;
  820. vs_out :
  821. begin
  822. reference_reset_base(href,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  823. tmpreg:=cg.get_scratch_reg_address(list);
  824. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  825. reference_reset_base(href,tmpreg,0);
  826. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  827. cg.free_scratch_reg(list,tmpreg);
  828. end;
  829. end;
  830. end;
  831. end;
  832. { generates the code for decrementing the reference count of parameters }
  833. procedure final_paras(p : tnamedindexitem;arg:pointer);
  834. var
  835. href : treference;
  836. list : taasmoutput;
  837. begin
  838. list:=taasmoutput(arg);
  839. if (tsym(p).typ=varsym) and
  840. not is_class(tvarsym(p).vartype.def) and
  841. tvarsym(p).vartype.def.needs_inittable then
  842. begin
  843. if (tvarsym(p).varspez=vs_value) then
  844. begin
  845. if assigned(tvarsym(p).localvarsym) then
  846. reference_reset_base(href,procinfo^.framepointer,
  847. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  848. else
  849. reference_reset_base(href,procinfo^.framepointer,tvarsym(p).address+procinfo^.para_offset);
  850. cg.g_decrrefcount(list,tvarsym(p).vartype.def,href);
  851. end;
  852. end;
  853. end;
  854. { Initialize temp ansi/widestrings,interfaces }
  855. procedure inittempvariables(list:taasmoutput);
  856. var
  857. hp : ptemprecord;
  858. href : treference;
  859. begin
  860. hp:=tg.templist;
  861. while assigned(hp) do
  862. begin
  863. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  864. tt_widestring,tt_freewidestring,
  865. tt_interfacecom] then
  866. begin
  867. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  868. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  869. cg.a_load_const_ref(list,OS_ADDR,0,href);
  870. end;
  871. hp:=hp^.next;
  872. end;
  873. end;
  874. procedure finalizetempvariables(list:taasmoutput);
  875. var
  876. hp : ptemprecord;
  877. href : treference;
  878. begin
  879. hp:=tg.templist;
  880. while assigned(hp) do
  881. begin
  882. case hp^.temptype of
  883. tt_ansistring,
  884. tt_freeansistring :
  885. begin
  886. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  887. cg.a_paramaddr_ref(list,href,1);
  888. cg.a_call_name(list,'FPC_ANSISTR_DECR_REF');
  889. end;
  890. tt_widestring,
  891. tt_freewidestring :
  892. begin
  893. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  894. cg.a_paramaddr_ref(list,href,1);
  895. cg.a_call_name(list,'FPC_WIDESTR_DECR_REF');
  896. end;
  897. tt_interfacecom :
  898. begin
  899. reference_reset_base(href,procinfo^.framepointer,hp^.pos);
  900. cg.a_paramaddr_ref(list,href,1);
  901. cg.a_call_name(list,'FPC_INTF_DECR_REF');
  902. end;
  903. end;
  904. hp:=hp^.next;
  905. end;
  906. end;
  907. procedure handle_return_value(list:TAAsmoutput; inlined : boolean;var uses_acc,uses_acchi : boolean);
  908. var
  909. href : treference;
  910. hreg : tregister;
  911. cgsize : TCGSize;
  912. begin
  913. if not is_void(aktprocdef.rettype.def) then
  914. begin
  915. if (tfuncretsym(aktprocdef.funcretsym).funcretstate<>vs_assigned) and
  916. (not inlined) then
  917. CGMessage(sym_w_function_result_not_set);
  918. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  919. cgsize:=def_cgsize(aktprocdef.rettype.def);
  920. case aktprocdef.rettype.def.deftype of
  921. orddef,
  922. enumdef :
  923. begin
  924. uses_acc:=true;
  925. cg.a_reg_alloc(list,accumulator);
  926. if cgsize in [OS_64,OS_S64] then
  927. begin
  928. uses_acchi:=true;
  929. cg.a_reg_alloc(list,accumulatorhigh);
  930. cg64.a_load64_ref_reg(list,href,joinreg64(accumulator,accumulatorhigh));
  931. end
  932. else
  933. begin
  934. hreg:=rg.makeregsize(accumulator,cgsize);
  935. cg.a_load_ref_reg(list,cgsize,href,hreg);
  936. end;
  937. end;
  938. floatdef :
  939. begin
  940. cg.a_loadfpu_ref_reg(list,cgsize,href,fpuresultreg);
  941. end;
  942. else
  943. begin
  944. if ret_in_acc(aktprocdef.rettype.def) then
  945. begin
  946. uses_acc:=true;
  947. cg.a_reg_alloc(list,accumulator);
  948. cg.a_load_ref_reg(list,cgsize,href,accumulator);
  949. end
  950. end;
  951. end;
  952. end;
  953. end;
  954. procedure handle_fast_exit_return_value(list:TAAsmoutput);
  955. var
  956. href : treference;
  957. hreg : tregister;
  958. cgsize : TCGSize;
  959. begin
  960. if not is_void(aktprocdef.rettype.def) then
  961. begin
  962. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  963. cgsize:=def_cgsize(aktprocdef.rettype.def);
  964. case aktprocdef.rettype.def.deftype of
  965. orddef,
  966. enumdef :
  967. begin
  968. if cgsize in [OS_64,OS_S64] then
  969. cg64.a_load64_reg_ref(list,joinreg64(accumulator,accumulatorhigh),href)
  970. else
  971. begin
  972. hreg:=rg.makeregsize(accumulator,cgsize);
  973. cg.a_load_reg_ref(list,cgsize,hreg,href);
  974. end;
  975. end;
  976. floatdef :
  977. begin
  978. cg.a_loadfpu_reg_ref(list,cgsize,fpuresultreg,href);
  979. end;
  980. else
  981. begin
  982. if ret_in_acc(aktprocdef.rettype.def) then
  983. cg.a_load_reg_ref(list,cgsize,accumulator,href);
  984. end;
  985. end;
  986. end;
  987. end;
  988. procedure genentrycode(list : TAAsmoutput;
  989. make_global:boolean;
  990. stackframe:longint;
  991. var parasize:longint;var nostackframe:boolean;
  992. inlined : boolean);
  993. var
  994. hs : string;
  995. href : treference;
  996. p : tsymtable;
  997. tempbuf : treference;
  998. tmpreg : tregister;
  999. begin
  1000. { Insert alignment and assembler names }
  1001. if not inlined then
  1002. begin
  1003. { Align, gprof uses 16 byte granularity }
  1004. if (cs_profile in aktmoduleswitches) then
  1005. list.concat(Tai_align.Create_op(16,$90))
  1006. else
  1007. list.concat(Tai_align.Create(aktalignment.procalign));
  1008. if (cs_profile in aktmoduleswitches) or
  1009. (aktprocdef.owner.symtabletype=globalsymtable) or
  1010. (assigned(procinfo^._class) and (procinfo^._class.owner.symtabletype=globalsymtable)) then
  1011. make_global:=true;
  1012. if make_global or ((procinfo^.flags and pi_is_global) <> 0) then
  1013. aktprocsym.is_global := True;
  1014. {$ifdef GDB}
  1015. if (cs_debuginfo in aktmoduleswitches) then
  1016. begin
  1017. aktprocdef.concatstabto(list);
  1018. aktprocsym.isstabwritten:=true;
  1019. end;
  1020. {$endif GDB}
  1021. repeat
  1022. hs:=aktprocdef.aliasnames.getfirst;
  1023. if hs='' then
  1024. break;
  1025. {$ifdef GDB}
  1026. if (cs_debuginfo in aktmoduleswitches) and
  1027. target_info.use_function_relative_addresses then
  1028. list.concat(Tai_stab_function_name.Create(strpnew(hs)));
  1029. {$endif GDB}
  1030. if make_global then
  1031. list.concat(Tai_symbol.Createname_global(hs,0))
  1032. else
  1033. list.concat(Tai_symbol.Createname(hs,0));
  1034. until false;
  1035. { omit stack frame ? }
  1036. if (procinfo^.framepointer=STACK_POINTER_REG) then
  1037. begin
  1038. CGMessage(cg_d_stackframe_omited);
  1039. nostackframe:=true;
  1040. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1041. parasize:=0
  1042. else
  1043. parasize:=aktprocdef.parast.datasize+procinfo^.para_offset-4;
  1044. if stackframe<>0 then
  1045. cg.a_op_const_reg(list,OP_SUB,stackframe,procinfo^.framepointer);
  1046. end
  1047. else
  1048. begin
  1049. nostackframe:=false;
  1050. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1051. parasize:=0
  1052. else
  1053. parasize:=aktprocdef.parast.datasize+procinfo^.para_offset-target_info.first_parm_offset;
  1054. if (po_interrupt in aktprocdef.procoptions) then
  1055. cg.g_interrupt_stackframe_entry(list);
  1056. cg.g_stackframe_entry(list,stackframe);
  1057. if (cs_check_stack in aktlocalswitches) then
  1058. cg.g_stackcheck(list,stackframe);
  1059. end;
  1060. if (cs_profile in aktmoduleswitches) and
  1061. not(po_assembler in aktprocdef.procoptions) then
  1062. cg.g_profilecode(list);
  1063. end;
  1064. { for the save all registers we can simply use a pusha,popa which
  1065. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1066. if (po_saveregisters in aktprocdef.procoptions) then
  1067. cg.g_save_all_registers(list)
  1068. else
  1069. { should we save edi,esi,ebx like C ? }
  1070. if (po_savestdregs in aktprocdef.procoptions) then
  1071. cg.g_save_standard_registers(list);
  1072. { a constructor needs a help procedure }
  1073. if (aktprocdef.proctypeoption=potype_constructor) then
  1074. cg.g_call_constructor_helper(list);
  1075. { don't load ESI, does the caller }
  1076. { we must do it for local function }
  1077. { that can be called from a foreach_static }
  1078. { of another object than self !! PM }
  1079. if assigned(procinfo^._class) and { !!!!! shouldn't we load ESI always? }
  1080. (lexlevel>normal_function_level) then
  1081. cg.g_maybe_loadself(list);
  1082. { When message method contains self as a parameter,
  1083. we must load it into ESI }
  1084. If (po_containsself in aktprocdef.procoptions) then
  1085. begin
  1086. list.concat(tai_regalloc.Alloc(self_pointer_reg));
  1087. reference_reset_base(href,procinfo^.framepointer,procinfo^.selfpointer_offset);
  1088. cg.a_load_ref_reg(list,OS_ADDR,href,self_pointer_reg);
  1089. end;
  1090. { initialize return value }
  1091. if (not is_void(aktprocdef.rettype.def)) and
  1092. (aktprocdef.rettype.def.needs_inittable) then
  1093. begin
  1094. procinfo^.flags:=procinfo^.flags or pi_needs_implicit_finally;
  1095. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  1096. cg.g_initialize(list,aktprocdef.rettype.def,href,ret_in_param(aktprocdef.rettype.def));
  1097. end;
  1098. { initialisize local data like ansistrings }
  1099. case aktprocdef.proctypeoption of
  1100. potype_unitinit:
  1101. begin
  1102. { using current_module.globalsymtable is hopefully }
  1103. { more robust than symtablestack and symtablestack.next }
  1104. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1105. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1106. end;
  1107. { units have seperate code for initilization and finalization }
  1108. potype_unitfinalize: ;
  1109. else
  1110. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1111. end;
  1112. { initialisizes temp. ansi/wide string data }
  1113. inittempvariables(list);
  1114. { generate copies of call by value parameters }
  1115. if not(po_assembler in aktprocdef.procoptions) and
  1116. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_palmossyscall,pocall_system]) then
  1117. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1118. if assigned( aktprocdef.parast) then
  1119. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1120. if (not inlined) then
  1121. begin
  1122. { call startup helpers from main program }
  1123. if (aktprocdef.proctypeoption=potype_proginit) then
  1124. begin
  1125. { initialize profiling for win32 }
  1126. if (target_info.target in [target_I386_WIN32,target_I386_wdosx]) and
  1127. (cs_profile in aktmoduleswitches) then
  1128. cg.a_call_name(list,'__monstartup');
  1129. { add local threadvars in units (only if needed because not all platforms
  1130. have threadvar support) }
  1131. if have_local_threadvars then
  1132. cg.a_call_name(list,'FPC_INITIALIZELOCALTHREADVARS');
  1133. { add global threadvars }
  1134. p:=symtablestack;
  1135. while assigned(p) do
  1136. begin
  1137. p.foreach_static({$ifndef TP}@{$endif}initialize_threadvar,list);
  1138. p:=p.next;
  1139. end;
  1140. { initialize units }
  1141. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1142. end;
  1143. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  1144. if ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  1145. { but it's useless in init/final code of units }
  1146. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1147. begin
  1148. include(rg.usedinproc,accumulator);
  1149. { allocate exception frame buffer }
  1150. cg.a_op_const_reg(list,OP_SUB,36,STACK_POINTER_REG);
  1151. tmpreg:=rg.getaddressregister(list);
  1152. cg.a_load_reg_reg(list,OS_ADDR,STACK_POINTER_REG,tmpreg);
  1153. reference_reset_base(tempbuf,tmpreg,0);
  1154. cg.g_push_exception(list,tempbuf,1,aktexitlabel);
  1155. reference_release(list,tempbuf);
  1156. { probably we've to reload self here }
  1157. cg.g_maybe_loadself(list);
  1158. end;
  1159. {$ifdef GDB}
  1160. if (cs_debuginfo in aktmoduleswitches) then
  1161. list.concat(Tai_force_line.Create);
  1162. {$endif GDB}
  1163. end;
  1164. if inlined then
  1165. load_regvars(list,nil);
  1166. end;
  1167. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  1168. var
  1169. {$ifdef GDB}
  1170. stabsendlabel : tasmlabel;
  1171. mangled_length : longint;
  1172. p : pchar;
  1173. st : string[2];
  1174. {$endif GDB}
  1175. okexitlabel,
  1176. noreraiselabel,nodestroycall : tasmlabel;
  1177. tmpreg : tregister;
  1178. href : treference;
  1179. usesacc,
  1180. usesacchi,
  1181. usesself : boolean;
  1182. pd : tprocdef;
  1183. begin
  1184. if aktexit2label.is_used and
  1185. ((procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  1186. begin
  1187. cg.a_jmp_always(list,aktexitlabel);
  1188. cg.a_label(list,aktexit2label);
  1189. handle_fast_exit_return_value(list);
  1190. end;
  1191. if aktexitlabel.is_used then
  1192. list.concat(Tai_label.Create(aktexitlabel));
  1193. cleanup_regvars(list);
  1194. { call the destructor help procedure }
  1195. if (aktprocdef.proctypeoption=potype_destructor) and
  1196. assigned(procinfo^._class) then
  1197. cg.g_call_destructor_helper(list);
  1198. { finalize temporary data }
  1199. finalizetempvariables(list);
  1200. { finalize local data like ansistrings}
  1201. case aktprocdef.proctypeoption of
  1202. potype_unitfinalize:
  1203. begin
  1204. { using current_module.globalsymtable is hopefully }
  1205. { more robust than symtablestack and symtablestack.next }
  1206. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1207. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1208. end;
  1209. { units have seperate code for initialization and finalization }
  1210. potype_unitinit: ;
  1211. else
  1212. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1213. end;
  1214. { finalize paras data }
  1215. if assigned(aktprocdef.parast) then
  1216. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1217. { do we need to handle exceptions because of ansi/widestrings ? }
  1218. if not inlined and
  1219. ((procinfo^.flags and pi_needs_implicit_finally)<>0) and
  1220. { but it's useless in init/final code of units }
  1221. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1222. begin
  1223. { the exception helper routines modify all registers }
  1224. aktprocdef.usedregisters:=all_registers;
  1225. getlabel(noreraiselabel);
  1226. cg.g_pop_exception(list,noreraiselabel);
  1227. if (aktprocdef.proctypeoption=potype_constructor) then
  1228. begin
  1229. if assigned(procinfo^._class) then
  1230. begin
  1231. pd:=procinfo^._class.searchdestructor;
  1232. if assigned(pd) then
  1233. begin
  1234. getlabel(nodestroycall);
  1235. reference_reset_base(href,procinfo^.framepointer,procinfo^.selfpointer_offset);
  1236. cg.a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nodestroycall);
  1237. if is_class(procinfo^._class) then
  1238. begin
  1239. cg.a_param_const(list,OS_INT,1,2);
  1240. cg.a_param_reg(list,OS_ADDR,self_pointer_reg,1);
  1241. end
  1242. else if is_object(procinfo^._class) then
  1243. begin
  1244. cg.a_param_reg(list,OS_ADDR,self_pointer_reg,2);
  1245. reference_reset_symbol(href,newasmsymbol(procinfo^._class.vmt_mangledname),0);
  1246. cg.a_paramaddr_ref(list,href,1);
  1247. end
  1248. else
  1249. Internalerror(200006164);
  1250. if (po_virtualmethod in pd.procoptions) then
  1251. begin
  1252. reference_reset_base(href,self_pointer_reg,0);
  1253. tmpreg:=cg.get_scratch_reg_address(list);
  1254. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  1255. reference_reset_base(href,tmpreg,procinfo^._class.vmtmethodoffset(pd.extnumber));
  1256. cg.free_scratch_reg(list,tmpreg);
  1257. cg.a_call_ref(list,href);
  1258. end
  1259. else
  1260. cg.a_call_name(list,pd.mangledname);
  1261. { not necessary because the result is never assigned in the
  1262. case of an exception (FK) }
  1263. cg.a_label(list,nodestroycall);
  1264. end;
  1265. end
  1266. end
  1267. else
  1268. begin
  1269. { no constructor }
  1270. { must be the return value finalized before reraising the exception? }
  1271. if (not is_void(aktprocdef.rettype.def)) and
  1272. (aktprocdef.rettype.def.needs_inittable) and
  1273. ((aktprocdef.rettype.def.deftype<>objectdef) or
  1274. not is_class(aktprocdef.rettype.def)) then
  1275. begin
  1276. reference_reset_base(href,procinfo^.framepointer,procinfo^.return_offset);
  1277. cg.g_finalize(list,aktprocdef.rettype.def,href,ret_in_param(aktprocdef.rettype.def));
  1278. end;
  1279. end;
  1280. cg.a_call_name(list,'FPC_RERAISE');
  1281. cg.a_label(list,noreraiselabel);
  1282. end;
  1283. { call __EXIT for main program }
  1284. if (not DLLsource) and
  1285. (not inlined) and
  1286. (aktprocdef.proctypeoption=potype_proginit) then
  1287. cg.a_call_name(list,'FPC_DO_EXIT');
  1288. { handle return value, this is not done for assembler routines when
  1289. they didn't reference the result variable }
  1290. usesacc:=false;
  1291. usesacchi:=false;
  1292. usesself:=false;
  1293. if not(po_assembler in aktprocdef.procoptions) or
  1294. (assigned(aktprocdef.funcretsym) and
  1295. (tfuncretsym(aktprocdef.funcretsym).refcount>1)) then
  1296. begin
  1297. if (aktprocdef.proctypeoption<>potype_constructor) then
  1298. handle_return_value(list,inlined,usesacc,usesacchi)
  1299. else
  1300. begin
  1301. { successful constructor deletes the zero flag }
  1302. { and returns self in eax }
  1303. { eax must be set to zero if the allocation failed !!! }
  1304. getlabel(okexitlabel);
  1305. cg.a_jmp_always(list,okexitlabel);
  1306. cg.a_label(list,faillabel);
  1307. cg.g_call_fail_helper(list);
  1308. cg.a_label(list,okexitlabel);
  1309. { for classes this is done after the call to }
  1310. { AfterConstruction }
  1311. if is_object(procinfo^._class) then
  1312. begin
  1313. cg.a_reg_alloc(list,accumulator);
  1314. cg.a_load_reg_reg(list,OS_ADDR,self_pointer_reg,accumulator);
  1315. usesacc:=true;
  1316. end;
  1317. {$ifdef i386}
  1318. list.concat(taicpu.op_reg_reg(A_TEST,S_L,R_ESI,R_ESI));
  1319. {$else}
  1320. {$warning constructor returns in flags for i386}
  1321. {$endif i386}
  1322. usesself:=true;
  1323. end;
  1324. end;
  1325. if aktexit2label.is_used and not aktexit2label.is_set then
  1326. cg.a_label(list,aktexit2label);
  1327. {$ifdef GDB}
  1328. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  1329. begin
  1330. getlabel(stabsendlabel);
  1331. cg.a_label(list,stabsendlabel);
  1332. end;
  1333. {$endif GDB}
  1334. { for the save all registers we can simply use a pusha,popa which
  1335. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1336. if (po_saveregisters in aktprocdef.procoptions) then
  1337. cg.g_restore_all_registers(list,usesself,usesacc,usesacchi)
  1338. else
  1339. { should we restore edi ? }
  1340. if (po_savestdregs in aktprocdef.procoptions) then
  1341. cg.g_restore_standard_registers(list);
  1342. { remove stackframe }
  1343. if not inlined then
  1344. begin
  1345. if (not nostackframe) then
  1346. cg.g_restore_frame_pointer(list)
  1347. else
  1348. if (tg.gettempsize<>0) then
  1349. cg.a_op_const_reg(list,OP_ADD,tg.gettempsize,STACK_POINTER_REG);
  1350. end;
  1351. { at last, the return is generated }
  1352. if not inlined then
  1353. begin
  1354. if (po_interrupt in aktprocdef.procoptions) then
  1355. cg.g_interrupt_stackframe_exit(list,usesself,usesacc,usesacchi)
  1356. else
  1357. cg.g_return_from_proc(list,parasize);
  1358. end;
  1359. if not inlined then
  1360. list.concat(Tai_symbol_end.Createname(aktprocdef.mangledname));
  1361. {$ifdef GDB}
  1362. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  1363. begin
  1364. if assigned(procinfo^._class) then
  1365. if (not assigned(procinfo^.parent) or
  1366. not assigned(procinfo^.parent^._class)) then
  1367. begin
  1368. if (po_classmethod in aktprocdef.procoptions) or
  1369. ((po_virtualmethod in aktprocdef.procoptions) and
  1370. (potype_constructor=aktprocdef.proctypeoption)) or
  1371. (po_staticmethod in aktprocdef.procoptions) then
  1372. begin
  1373. list.concat(Tai_stabs.Create(strpnew(
  1374. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  1375. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  1376. end
  1377. else
  1378. begin
  1379. if not(is_class(procinfo^._class)) then
  1380. st:='v'
  1381. else
  1382. st:='p';
  1383. list.concat(Tai_stabs.Create(strpnew(
  1384. '"$t:'+st+procinfo^._class.numberstring+'",'+
  1385. tostr(N_tsym)+',0,0,'+tostr(procinfo^.selfpointer_offset))));
  1386. end;
  1387. end
  1388. else
  1389. begin
  1390. if not is_class(procinfo^._class) then
  1391. st:='*'
  1392. else
  1393. st:='';
  1394. list.concat(Tai_stabs.Create(strpnew(
  1395. '"$t:r'+st+procinfo^._class.numberstring+'",'+
  1396. tostr(N_RSYM)+',0,0,'+tostr(GDB_i386index[SELF_POINTER_REG]))));
  1397. end;
  1398. { define calling EBP as pseudo local var PM }
  1399. { this enables test if the function is a local one !! }
  1400. if assigned(procinfo^.parent) and (lexlevel>normal_function_level) then
  1401. list.concat(Tai_stabs.Create(strpnew(
  1402. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1403. tostr(N_LSYM)+',0,0,'+tostr(procinfo^.framepointer_offset))));
  1404. if (not is_void(aktprocdef.rettype.def)) then
  1405. begin
  1406. if ret_in_param(aktprocdef.rettype.def) then
  1407. list.concat(Tai_stabs.Create(strpnew(
  1408. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1409. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  1410. else
  1411. list.concat(Tai_stabs.Create(strpnew(
  1412. '"'+aktprocsym.name+':X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1413. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  1414. if (m_result in aktmodeswitches) then
  1415. if ret_in_param(aktprocdef.rettype.def) then
  1416. list.concat(Tai_stabs.Create(strpnew(
  1417. '"RESULT:X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1418. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))))
  1419. else
  1420. list.concat(Tai_stabs.Create(strpnew(
  1421. '"RESULT:X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1422. tostr(N_tsym)+',0,0,'+tostr(procinfo^.return_offset))));
  1423. end;
  1424. mangled_length:=length(aktprocdef.mangledname);
  1425. getmem(p,2*mangled_length+50);
  1426. strpcopy(p,'192,0,0,');
  1427. strpcopy(strend(p),aktprocdef.mangledname);
  1428. if (target_info.use_function_relative_addresses) then
  1429. begin
  1430. strpcopy(strend(p),'-');
  1431. strpcopy(strend(p),aktprocdef.mangledname);
  1432. end;
  1433. list.concat(Tai_stabn.Create(strnew(p)));
  1434. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1435. +aktprocdef.mangledname))));
  1436. p[0]:='2';p[1]:='2';p[2]:='4';
  1437. strpcopy(strend(p),'_end');}
  1438. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1439. if (target_info.use_function_relative_addresses) then
  1440. begin
  1441. strpcopy(strend(p),'-');
  1442. strpcopy(strend(p),aktprocdef.mangledname);
  1443. end;
  1444. list.concatlist(withdebuglist);
  1445. list.concat(Tai_stabn.Create(strnew(p)));
  1446. { strpnew('224,0,0,'
  1447. +aktprocdef.mangledname+'_end'))));}
  1448. freemem(p,2*mangled_length+50);
  1449. end;
  1450. {$endif GDB}
  1451. if inlined then
  1452. cleanup_regvars(list);
  1453. end;
  1454. procedure genimplicitunitinit(list : TAAsmoutput);
  1455. begin
  1456. { using current_module.globalsymtable is hopefully }
  1457. { more robust than symtablestack and symtablestack.next }
  1458. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1459. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1460. list.insert(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  1461. list.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  1462. {$ifdef GDB}
  1463. if (cs_debuginfo in aktmoduleswitches) and
  1464. target_info.use_function_relative_addresses then
  1465. list.insert(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  1466. {$endif GDB}
  1467. cg.g_return_from_proc(list,0);
  1468. end;
  1469. procedure genimplicitunitfinal(list : TAAsmoutput);
  1470. begin
  1471. { using current_module.globalsymtable is hopefully }
  1472. { more robust than symtablestack and symtablestack.next }
  1473. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1474. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1475. list.insert(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  1476. list.insert(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  1477. {$ifdef GDB}
  1478. if (cs_debuginfo in aktmoduleswitches) and
  1479. target_info.use_function_relative_addresses then
  1480. list.insert(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  1481. {$endif GDB}
  1482. cg.g_return_from_proc(list,0);
  1483. end;
  1484. end.
  1485. {
  1486. $Log$
  1487. Revision 1.19 2002-07-01 18:46:23 peter
  1488. * internal linker
  1489. * reorganized aasm layer
  1490. Revision 1.18 2002/07/01 16:23:53 peter
  1491. * cg64 patch
  1492. * basics for currency
  1493. * asnode updates for class and interface (not finished)
  1494. Revision 1.17 2002/05/20 13:30:40 carl
  1495. * bugfix of hdisponen (base must be set, not index)
  1496. * more portability fixes
  1497. Revision 1.16 2002/05/18 13:34:09 peter
  1498. * readded missing revisions
  1499. Revision 1.15 2002/05/16 19:46:37 carl
  1500. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1501. + try to fix temp allocation (still in ifdef)
  1502. + generic constructor calls
  1503. + start of tassembler / tmodulebase class cleanup
  1504. Revision 1.13 2002/05/13 19:54:37 peter
  1505. * removed n386ld and n386util units
  1506. * maybe_save/maybe_restore added instead of the old maybe_push
  1507. Revision 1.12 2002/05/12 19:58:36 carl
  1508. * some small portability fixes
  1509. Revision 1.11 2002/05/12 16:53:07 peter
  1510. * moved entry and exitcode to ncgutil and cgobj
  1511. * foreach gets extra argument for passing local data to the
  1512. iterator function
  1513. * -CR checks also class typecasts at runtime by changing them
  1514. into as
  1515. * fixed compiler to cycle with the -CR option
  1516. * fixed stabs with elf writer, finally the global variables can
  1517. be watched
  1518. * removed a lot of routines from cga unit and replaced them by
  1519. calls to cgobj
  1520. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1521. u32bit then the other is typecasted also to u32bit without giving
  1522. a rangecheck warning/error.
  1523. * fixed pascal calling method with reversing also the high tree in
  1524. the parast, detected by tcalcst3 test
  1525. Revision 1.10 2002/04/21 19:02:03 peter
  1526. * removed newn and disposen nodes, the code is now directly
  1527. inlined from pexpr
  1528. * -an option that will write the secondpass nodes to the .s file, this
  1529. requires EXTDEBUG define to actually write the info
  1530. * fixed various internal errors and crashes due recent code changes
  1531. Revision 1.9 2002/04/21 15:24:38 carl
  1532. + a_jmp_cond -> a_jmp_always (a_jmp_cond is NOT portable)
  1533. + changeregsize -> rg.makeregsize
  1534. Revision 1.8 2002/04/19 15:39:34 peter
  1535. * removed some more routines from cga
  1536. * moved location_force_reg/mem to ncgutil
  1537. * moved arrayconstructnode secondpass to ncgld
  1538. Revision 1.7 2002/04/15 18:58:47 carl
  1539. + target_info.size_of_pointer -> pointer_Size
  1540. Revision 1.6 2002/04/06 18:10:42 jonas
  1541. * several powerpc-related additions and fixes
  1542. Revision 1.5 2002/04/04 19:05:57 peter
  1543. * removed unused units
  1544. * use tlocation.size in cg.a_*loc*() routines
  1545. Revision 1.4 2002/04/02 17:11:28 peter
  1546. * tlocation,treference update
  1547. * LOC_CONSTANT added for better constant handling
  1548. * secondadd splitted in multiple routines
  1549. * location_force_reg added for loading a location to a register
  1550. of a specified size
  1551. * secondassignment parses now first the right and then the left node
  1552. (this is compatible with Kylix). This saves a lot of push/pop especially
  1553. with string operations
  1554. * adapted some routines to use the new cg methods
  1555. Revision 1.3 2002/03/31 20:26:34 jonas
  1556. + a_loadfpu_* and a_loadmm_* methods in tcg
  1557. * register allocation is now handled by a class and is mostly processor
  1558. independent (+rgobj.pas and i386/rgcpu.pas)
  1559. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1560. * some small improvements and fixes to the optimizer
  1561. * some register allocation fixes
  1562. * some fpuvaroffset fixes in the unary minus node
  1563. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1564. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1565. also better optimizable)
  1566. * fixed and optimized register saving/restoring for new/dispose nodes
  1567. * LOC_FPU locations now also require their "register" field to be set to
  1568. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1569. - list field removed of the tnode class because it's not used currently
  1570. and can cause hard-to-find bugs
  1571. Revision 1.2 2002/03/04 19:10:11 peter
  1572. * removed compiler warnings
  1573. }