ncgutil.pas 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  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,cpuinfo,
  23. globtype,
  24. cpubase,cpupara,
  25. aasmbase,aasmtai,aasmcpu,
  26. cginfo,symbase,symdef,symtype,
  27. {$ifndef cpu64bit}
  28. cg64f32,
  29. {$endif cpu64bit}
  30. rgobj;
  31. type
  32. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  33. tmaybesave = record
  34. saved : boolean;
  35. ref : treference;
  36. end;
  37. procedure firstcomplex(p : tbinarynode);
  38. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  39. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  40. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  41. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  42. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  43. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  44. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  45. procedure push_value_para(p:tnode;calloption:tproccalloption;
  46. para_offset:longint;alignment : longint;
  47. const locpara : tparalocation);
  48. procedure genentrycode(list : TAAsmoutput;
  49. make_global:boolean;
  50. stackframe:longint;
  51. var parasize:longint;var nostackframe:boolean;
  52. inlined : boolean);
  53. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  54. procedure genimplicitunitinit(list : TAAsmoutput);
  55. procedure genimplicitunitfinal(list : TAAsmoutput);
  56. {#
  57. Allocate the buffers for exception management and setjmp environment.
  58. Return a pointer to these buffers, send them to the utility routine
  59. so they are registered, and then call setjmp.
  60. Then compare the result of setjmp with 0, and if not equal
  61. to zero, then jump to exceptlabel.
  62. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  63. It is to note that this routine may be called *after* the stackframe of a
  64. routine has been called, therefore on machines where the stack cannot
  65. be modified, all temps should be allocated on the heap instead of the
  66. stack.
  67. }
  68. procedure new_exception(list : taasmoutput;const jmpbuf,envbuf, href : treference;
  69. a : aword; exceptlabel : tasmlabel);
  70. procedure free_exception(list : taasmoutput;const jmpbuf, envbuf, href : treference;
  71. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  72. implementation
  73. uses
  74. {$ifdef Delphi}
  75. Sysutils,
  76. {$else}
  77. strings,
  78. {$endif}
  79. cutils,cclasses,
  80. globals,systems,verbose,
  81. symconst,symsym,symtable,defutil,
  82. paramgr,fmodule,
  83. cgbase,regvars,
  84. {$ifdef GDB}
  85. gdb,
  86. {$endif GDB}
  87. ncon,
  88. tgobj,cgobj,cgcpu;
  89. {*****************************************************************************
  90. Misc Helpers
  91. *****************************************************************************}
  92. { DO NOT RELY on the fact that the tnode is not yet swaped
  93. because of inlining code PM }
  94. procedure firstcomplex(p : tbinarynode);
  95. var
  96. hp : tnode;
  97. begin
  98. { always calculate boolean AND and OR from left to right }
  99. if (p.nodetype in [orn,andn]) and
  100. is_boolean(p.left.resulttype.def) then
  101. begin
  102. if nf_swaped in p.flags then
  103. internalerror(234234);
  104. end
  105. else
  106. if (
  107. (p.location.loc=LOC_FPUREGISTER) and
  108. (p.right.registersfpu > p.left.registersfpu)
  109. ) or
  110. (
  111. (
  112. (
  113. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  114. (p.location.loc<>LOC_FPUREGISTER)
  115. ) and
  116. (p.left.registers32<p.right.registers32)
  117. ) and
  118. { the following check is appropriate, because all }
  119. { 4 registers are rarely used and it is thereby }
  120. { achieved that the extra code is being dropped }
  121. { by exchanging not commutative operators }
  122. (p.right.registers32<=c_countusableregsint)
  123. ) then
  124. begin
  125. hp:=p.left;
  126. p.left:=p.right;
  127. p.right:=hp;
  128. if nf_swaped in p.flags then
  129. exclude(p.flags,nf_swaped)
  130. else
  131. include(p.flags,nf_swaped);
  132. end;
  133. end;
  134. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  135. {
  136. produces jumps to true respectively false labels using boolean expressions
  137. depending on whether the loading of regvars is currently being
  138. synchronized manually (such as in an if-node) or automatically (most of
  139. the other cases where this procedure is called), loadregvars can be
  140. "lr_load_regvars" or "lr_dont_load_regvars"
  141. }
  142. var
  143. opsize : tcgsize;
  144. storepos : tfileposinfo;
  145. begin
  146. if nf_error in p.flags then
  147. exit;
  148. storepos:=aktfilepos;
  149. aktfilepos:=p.fileinfo;
  150. if is_boolean(p.resulttype.def) then
  151. begin
  152. if loadregvars = lr_load_regvars then
  153. load_all_regvars(list);
  154. if is_constboolnode(p) then
  155. begin
  156. if tordconstnode(p).value<>0 then
  157. cg.a_jmp_always(list,truelabel)
  158. else
  159. cg.a_jmp_always(list,falselabel)
  160. end
  161. else
  162. begin
  163. opsize:=def_cgsize(p.resulttype.def);
  164. case p.location.loc of
  165. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  166. begin
  167. if (p.location.loc = LOC_CREGISTER) then
  168. load_regvar_reg(list,p.location.register);
  169. cg.a_cmp_const_loc_label(list,opsize,OC_NE,
  170. 0,p.location,truelabel);
  171. { !!! should happen right after cmp (JM) }
  172. location_release(list,p.location);
  173. cg.a_jmp_always(list,falselabel);
  174. end;
  175. {$ifdef cpuflags}
  176. LOC_FLAGS :
  177. begin
  178. cg.a_jmp_flags(list,p.location.resflags,
  179. truelabel);
  180. cg.a_jmp_always(list,falselabel);
  181. end;
  182. {$endif cpuflags}
  183. end;
  184. end;
  185. end
  186. else
  187. internalerror(200112305);
  188. aktfilepos:=storepos;
  189. end;
  190. procedure remove_non_regvars_from_loc(const t: tlocation; var regs: tregisterset);
  191. begin
  192. case t.loc of
  193. LOC_REGISTER:
  194. begin
  195. { can't be a regvar, since it would be LOC_CREGISTER then }
  196. if t.register.enum>lastreg then
  197. internalerror(200201081);
  198. if t.registerhigh.enum>lastreg then
  199. internalerror(200201081);
  200. exclude(regs,t.register.enum);
  201. if t.registerhigh.enum <> R_NO then
  202. exclude(regs,t.registerhigh.enum);
  203. end;
  204. LOC_CREFERENCE,LOC_REFERENCE:
  205. begin
  206. if t.reference.base.enum>lastreg then
  207. internalerror(200201081);
  208. if t.reference.index.enum>lastreg then
  209. internalerror(200201081);
  210. if not(cs_regalloc in aktglobalswitches) or
  211. (t.reference.base.enum in rg.usableregsint) then
  212. exclude(regs,t.reference.base.enum);
  213. if not(cs_regalloc in aktglobalswitches) or
  214. (t.reference.index.enum in rg.usableregsint) then
  215. exclude(regs,t.reference.index.enum);
  216. end;
  217. end;
  218. end;
  219. {*****************************************************************************
  220. EXCEPTION MANAGEMENT
  221. *****************************************************************************}
  222. procedure new_exception(list : taasmoutput;const jmpbuf,envbuf, href : treference;
  223. a : aword; exceptlabel : tasmlabel);
  224. var r:Tregister;
  225. begin
  226. cg.a_paramaddr_ref(list,envbuf,paramanager.getintparaloc(3));
  227. cg.a_paramaddr_ref(list,jmpbuf,paramanager.getintparaloc(2));
  228. { push type of exceptionframe }
  229. cg.a_param_const(list,OS_S32,1,paramanager.getintparaloc(1));
  230. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  231. r.enum:=accumulator;
  232. cg.a_param_reg(list,OS_ADDR,r,paramanager.getintparaloc(1));
  233. cg.a_call_name(list,'FPC_SETJMP');
  234. cg.g_exception_reason_save(list, href);
  235. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,r,exceptlabel);
  236. end;
  237. procedure free_exception(list : taasmoutput;const jmpbuf, envbuf, href : treference;
  238. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  239. var r:Tregister;
  240. begin
  241. cg.a_call_name(list,'FPC_POPADDRSTACK');
  242. if not onlyfree then
  243. begin
  244. cg.g_exception_reason_load(list, href);
  245. r.enum:=accumulator;
  246. cg.a_cmp_const_reg_label(list,OS_S32,OC_EQ,a,r,endexceptlabel);
  247. end;
  248. end;
  249. {*****************************************************************************
  250. TLocation
  251. *****************************************************************************}
  252. {$ifndef cpu64bit}
  253. { 32-bit version }
  254. procedure location_force(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  255. var
  256. hregister,
  257. hregisterhi : tregister;
  258. hreg64 : tregister64;
  259. hl : tasmlabel;
  260. begin
  261. { handle transformations to 64bit separate }
  262. if dst_size in [OS_64,OS_S64] then
  263. begin
  264. if not (l.size in [OS_64,OS_S64]) then
  265. begin
  266. { load a smaller size to OS_64 }
  267. if l.loc=LOC_REGISTER then
  268. begin
  269. hregister:=rg.makeregsize(l.registerlow,OS_INT);
  270. cg.a_load_reg_reg(list,l.size,OS_32,l.registerlow,hregister);
  271. end
  272. else
  273. hregister:=rg.getregisterint(list);
  274. { load value in low register }
  275. case l.loc of
  276. LOC_FLAGS :
  277. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  278. LOC_JUMP :
  279. begin
  280. cg.a_label(list,truelabel);
  281. cg.a_load_const_reg(list,OS_INT,1,hregister);
  282. objectlibrary.getlabel(hl);
  283. cg.a_jmp_always(list,hl);
  284. cg.a_label(list,falselabel);
  285. cg.a_load_const_reg(list,OS_INT,0,hregister);
  286. cg.a_label(list,hl);
  287. end;
  288. else
  289. cg.a_load_loc_reg(list,l,hregister);
  290. end;
  291. { reset hi part, take care of the signed bit of the current value }
  292. hregisterhi:=rg.getregisterint(list);
  293. if (dst_size=OS_S64) and
  294. (l.size in [OS_S8,OS_S16,OS_S32]) then
  295. begin
  296. if l.loc=LOC_CONSTANT then
  297. begin
  298. if (longint(l.value)<0) then
  299. cg.a_load_const_reg(list,OS_32,$ffffffff,hregisterhi)
  300. else
  301. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  302. end
  303. else
  304. begin
  305. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  306. hregisterhi);
  307. end;
  308. end
  309. else
  310. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  311. location_reset(l,LOC_REGISTER,dst_size);
  312. l.registerlow:=hregister;
  313. l.registerhigh:=hregisterhi;
  314. end
  315. else
  316. begin
  317. { 64bit to 64bit }
  318. if (l.loc=LOC_REGISTER) or
  319. ((l.loc=LOC_CREGISTER) and maybeconst) then
  320. begin
  321. hregister:=l.registerlow;
  322. hregisterhi:=l.registerhigh;
  323. end
  324. else
  325. begin
  326. hregister:=rg.getregisterint(list);
  327. hregisterhi:=rg.getregisterint(list);
  328. end;
  329. hreg64.reglo:=hregister;
  330. hreg64.reghi:=hregisterhi;
  331. { load value in new register }
  332. cg64.a_load64_loc_reg(list,l,hreg64);
  333. location_reset(l,LOC_REGISTER,dst_size);
  334. l.registerlow:=hregister;
  335. l.registerhigh:=hregisterhi;
  336. end;
  337. end
  338. else
  339. begin
  340. { transformations to 32bit or smaller }
  341. if l.loc=LOC_REGISTER then
  342. begin
  343. { if the previous was 64bit release the high register }
  344. if l.size in [OS_64,OS_S64] then
  345. begin
  346. rg.ungetregisterint(list,l.registerhigh);
  347. l.registerhigh.enum:=R_NO;
  348. end;
  349. hregister:=l.register;
  350. end
  351. else
  352. begin
  353. { get new register }
  354. if (l.loc=LOC_CREGISTER) and
  355. maybeconst and
  356. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  357. hregister:=l.register
  358. else
  359. hregister:=rg.getregisterint(list);
  360. end;
  361. hregister:=rg.makeregsize(hregister,dst_size);
  362. { load value in new register }
  363. case l.loc of
  364. LOC_FLAGS :
  365. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  366. LOC_JUMP :
  367. begin
  368. cg.a_label(list,truelabel);
  369. cg.a_load_const_reg(list,dst_size,1,hregister);
  370. objectlibrary.getlabel(hl);
  371. cg.a_jmp_always(list,hl);
  372. cg.a_label(list,falselabel);
  373. cg.a_load_const_reg(list,dst_size,0,hregister);
  374. cg.a_label(list,hl);
  375. end;
  376. else
  377. begin
  378. { load_loc_reg can only handle size >= l.size, when the
  379. new size is smaller then we need to adjust the size
  380. of the orignal and maybe recalculate l.register for i386 }
  381. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  382. begin
  383. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  384. l.register:=rg.makeregsize(l.register,dst_size);
  385. { for big endian systems, the reference's offset must }
  386. { be increased in this case, since they have the }
  387. { MSB first in memory and e.g. byte(word_var) should }
  388. { return the second byte in this case (JM) }
  389. if (target_info.endian = ENDIAN_BIG) and
  390. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  391. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  392. l.size:=dst_size;
  393. end;
  394. cg.a_load_loc_reg(list,l,hregister);
  395. end;
  396. end;
  397. location_reset(l,LOC_REGISTER,dst_size);
  398. l.register:=hregister;
  399. end;
  400. end;
  401. {$else cpu64bit}
  402. { 64-bit version }
  403. procedure location_force(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  404. var
  405. hregister : tregister;
  406. hl : tasmlabel;
  407. begin
  408. { handle transformations to 64bit separate }
  409. if dst_size in [OS_64,OS_S64] then
  410. begin
  411. { load a smaller size to OS_64 }
  412. if l.loc=LOC_REGISTER then
  413. hregister:=rg.makeregsize(l.register,OS_INT)
  414. else
  415. hregister:=rg.getregisterint(list);
  416. { load value in low register }
  417. case l.loc of
  418. {$ifdef cpuflags}
  419. LOC_FLAGS :
  420. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  421. {$endif cpuflags}
  422. LOC_JUMP :
  423. begin
  424. cg.a_label(list,truelabel);
  425. cg.a_load_const_reg(list,OS_INT,1,hregister);
  426. objectlibrary.getlabel(hl);
  427. cg.a_jmp_always(list,hl);
  428. cg.a_label(list,falselabel);
  429. cg.a_load_const_reg(list,OS_INT,0,hregister);
  430. cg.a_label(list,hl);
  431. end;
  432. else
  433. cg.a_load_loc_reg(list,l,hregister);
  434. end;
  435. location_reset(l,LOC_REGISTER,dst_size);
  436. l.register:=hregister;
  437. end
  438. else
  439. begin
  440. { transformations to 32bit or smaller }
  441. if l.loc=LOC_REGISTER then
  442. begin
  443. hregister:=l.register;
  444. end
  445. else
  446. begin
  447. { get new register }
  448. if (l.loc=LOC_CREGISTER) and
  449. maybeconst and
  450. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  451. hregister:=l.register
  452. else
  453. hregister:=rg.getregisterint(list);
  454. end;
  455. hregister:=rg.makeregsize(hregister,dst_size);
  456. { load value in new register }
  457. case l.loc of
  458. {$ifdef cpuflags}
  459. LOC_FLAGS :
  460. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  461. {$endif cpuflags}
  462. LOC_JUMP :
  463. begin
  464. cg.a_label(list,truelabel);
  465. cg.a_load_const_reg(list,dst_size,1,hregister);
  466. objectlibrary.getlabel(hl);
  467. cg.a_jmp_always(list,hl);
  468. cg.a_label(list,falselabel);
  469. cg.a_load_const_reg(list,dst_size,0,hregister);
  470. cg.a_label(list,hl);
  471. end;
  472. else
  473. begin
  474. { load_loc_reg can only handle size >= l.size, when the
  475. new size is smaller then we need to adjust the size
  476. of the orignal and maybe recalculate l.register for i386 }
  477. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  478. begin
  479. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  480. l.register:=rg.makeregsize(l.register,dst_size);
  481. { for big endian systems, the reference's offset must }
  482. { be increased in this case, since they have the }
  483. { MSB first in memory and e.g. byte(word_var) should }
  484. { return the second byte in this case (JM) }
  485. if (target_info.endian = ENDIAN_BIG) and
  486. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  487. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  488. l.size:=dst_size;
  489. end;
  490. cg.a_load_loc_reg(list,l,hregister);
  491. end;
  492. end;
  493. location_reset(l,LOC_REGISTER,dst_size);
  494. l.register:=hregister;
  495. end;
  496. end;
  497. {$endif cpu64bit}
  498. procedure location_force_reg(list: TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  499. begin
  500. { release previous location before demanding a new register }
  501. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  502. begin
  503. location_freetemp(list,l);
  504. location_release(list,l);
  505. end;
  506. location_force(list, l, dst_size, maybeconst)
  507. end;
  508. procedure location_force_mem(list: TAAsmoutput;var l:tlocation);
  509. var
  510. r : treference;
  511. begin
  512. case l.loc of
  513. LOC_FPUREGISTER,
  514. LOC_CFPUREGISTER :
  515. begin
  516. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  517. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  518. location_release(exprasmlist,l);
  519. location_reset(l,LOC_REFERENCE,l.size);
  520. l.reference:=r;
  521. end;
  522. LOC_CONSTANT,
  523. LOC_REGISTER,
  524. LOC_CREGISTER :
  525. begin
  526. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  527. if l.size in [OS_64,OS_S64] then
  528. cg64.a_load64_loc_ref(list,l,r)
  529. else
  530. cg.a_load_loc_ref(list,l,r);
  531. location_release(exprasmlist,l);
  532. location_reset(l,LOC_REFERENCE,l.size);
  533. l.reference:=r;
  534. end;
  535. LOC_CREFERENCE,
  536. LOC_REFERENCE : ;
  537. else
  538. internalerror(200203219);
  539. end;
  540. end;
  541. {*****************************************************************************
  542. Maybe_Save
  543. *****************************************************************************}
  544. procedure maybe_save(list:taasmoutput;needed:integer;var l:tlocation;var s:tmaybesave);
  545. begin
  546. s.saved:=false;
  547. if l.loc=LOC_CREGISTER then
  548. begin
  549. s.saved:=true;
  550. exit;
  551. end;
  552. if needed>rg.countunusedregsint then
  553. begin
  554. case l.loc of
  555. LOC_REGISTER :
  556. begin
  557. {$ifndef cpu64bit}
  558. if l.size in [OS_64,OS_S64] then
  559. begin
  560. tg.GetTemp(exprasmlist,8,tt_normal,s.ref);
  561. cg64.a_load64_reg_ref(exprasmlist,joinreg64(l.registerlow,l.registerhigh),s.ref);
  562. end
  563. else
  564. {$endif cpu64bit}
  565. begin
  566. tg.GetTemp(exprasmlist,TCGSize2Size[l.size],tt_normal,s.ref);
  567. cg.a_load_reg_ref(exprasmlist,l.size,l.register,s.ref);
  568. end;
  569. location_release(exprasmlist,l);
  570. s.saved:=true;
  571. end;
  572. LOC_REFERENCE,
  573. LOC_CREFERENCE :
  574. begin
  575. if l.reference.base.enum>lastreg then
  576. internalerror(200101081);
  577. if ((l.reference.base.enum<>R_NO) or
  578. (l.reference.index.enum<>R_NO)) then
  579. begin
  580. { load address into a single base register }
  581. if l.reference.index.enum<>R_NO then
  582. begin
  583. cg.a_loadaddr_ref_reg(list,l.reference,l.reference.index);
  584. rg.ungetregister(list,l.reference.base);
  585. reference_reset_base(l.reference,l.reference.index,0);
  586. end
  587. else
  588. begin
  589. cg.a_loadaddr_ref_reg(list,l.reference,l.reference.base);
  590. rg.ungetregister(list,l.reference.index);
  591. reference_reset_base(l.reference,l.reference.base,0);
  592. end;
  593. { save base register }
  594. tg.GetTemp(exprasmlist,TCGSize2Size[OS_ADDR],tt_normal,s.ref);
  595. cg.a_load_reg_ref(exprasmlist,OS_ADDR,l.reference.base,s.ref);
  596. { release }
  597. location_release(exprasmlist,l);
  598. s.saved:=true;
  599. end;
  600. end;
  601. end;
  602. end;
  603. end;
  604. procedure maybe_restore(list:taasmoutput;var l:tlocation;const s:tmaybesave);
  605. begin
  606. if not s.saved then
  607. exit;
  608. if l.loc=LOC_CREGISTER then
  609. begin
  610. load_regvar_reg(list,l.register);
  611. exit;
  612. end;
  613. case l.loc of
  614. LOC_REGISTER :
  615. begin
  616. {$ifndef cpu64bit}
  617. if l.size in [OS_64,OS_S64] then
  618. begin
  619. l.registerlow:=rg.getregisterint(exprasmlist);
  620. l.registerhigh:=rg.getregisterint(exprasmlist);
  621. cg64.a_load64_ref_reg(exprasmlist,s.ref,joinreg64(l.registerlow,l.registerhigh));
  622. end
  623. else
  624. {$endif cpu64bit}
  625. begin
  626. l.register:=rg.getregisterint(exprasmlist);
  627. cg.a_load_ref_reg(exprasmlist,OS_INT,s.ref,l.register);
  628. end;
  629. end;
  630. LOC_CREFERENCE,
  631. LOC_REFERENCE :
  632. begin
  633. reference_reset(l.reference);
  634. l.reference.base:=rg.getaddressregister(exprasmlist);
  635. cg.a_load_ref_reg(exprasmlist,OS_ADDR,s.ref,l.reference.base);
  636. end;
  637. end;
  638. tg.ungetiftemp(exprasmlist,s.ref);
  639. end;
  640. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  641. begin
  642. if (needed>=maxfpuregs) and
  643. (l.loc = LOC_FPUREGISTER) then
  644. begin
  645. location_force_mem(list,l);
  646. maybe_pushfpu:=true;
  647. end
  648. else
  649. maybe_pushfpu:=false;
  650. end;
  651. {*****************************************************************************
  652. Push Value Para
  653. *****************************************************************************}
  654. procedure push_value_para(p:tnode;calloption:tproccalloption;
  655. para_offset:longint;alignment : longint;
  656. const locpara : tparalocation);
  657. var
  658. tempreference : treference;
  659. href : treference;
  660. hreg : tregister;
  661. sizetopush,
  662. size : longint;
  663. cgsize : tcgsize;
  664. r:Tregister;
  665. begin
  666. { we've nothing to push when the size of the parameter is 0 }
  667. if p.resulttype.def.size=0 then
  668. exit;
  669. { Move flags and jump in register to make it less complex }
  670. if p.location.loc in [LOC_FLAGS,LOC_JUMP] then
  671. location_force_reg(exprasmlist,p.location,def_cgsize(p.resulttype.def),false);
  672. { Handle Floating point types differently }
  673. if p.resulttype.def.deftype=floatdef then
  674. begin
  675. case p.location.loc of
  676. LOC_FPUREGISTER,
  677. LOC_CFPUREGISTER:
  678. begin
  679. size:=align(tfloatdef(p.resulttype.def).size,alignment);
  680. inc(pushedparasize,size);
  681. if calloption<>pocall_inline then
  682. cg.g_stackpointer_alloc(exprasmlist,size);
  683. {$ifdef GDB}
  684. if (cs_debuginfo in aktmoduleswitches) and
  685. (exprasmList.first=exprasmList.last) then
  686. exprasmList.concat(Tai_force_line.Create);
  687. {$endif GDB}
  688. { this is the easiest case for inlined !! }
  689. r.enum:=stack_pointer_reg;
  690. if calloption=pocall_inline then
  691. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize)
  692. else
  693. reference_reset_base(href,r,0);
  694. cg.a_loadfpu_reg_ref(exprasmlist,
  695. def_cgsize(p.resulttype.def),p.location.register,href);
  696. end;
  697. LOC_REFERENCE,
  698. LOC_CREFERENCE :
  699. begin
  700. sizetopush:=align(p.resulttype.def.size,alignment);
  701. tempreference:=p.location.reference;
  702. inc(tempreference.offset,sizetopush);
  703. while (sizetopush>0) do
  704. begin
  705. if sizetopush>=4 then
  706. begin
  707. cgsize:=OS_32;
  708. inc(pushedparasize,4);
  709. dec(tempreference.offset,4);
  710. dec(sizetopush,4);
  711. end
  712. else
  713. begin
  714. cgsize:=OS_16;
  715. inc(pushedparasize,2);
  716. dec(tempreference.offset,2);
  717. dec(sizetopush,2);
  718. end;
  719. if calloption=pocall_inline then
  720. begin
  721. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  722. cg.a_load_ref_ref(exprasmlist,cgsize,tempreference,href);
  723. end
  724. else
  725. cg.a_param_ref(exprasmlist,cgsize,tempreference,locpara);
  726. end;
  727. end;
  728. else
  729. internalerror(200204243);
  730. end;
  731. location_release(exprasmlist,p.location);
  732. end
  733. else
  734. begin
  735. { copy the value on the stack or use normal parameter push? }
  736. if paramanager.copy_value_on_stack(p.resulttype.def,calloption) then
  737. begin
  738. if not (p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  739. internalerror(200204241);
  740. { push on stack }
  741. size:=align(p.resulttype.def.size,alignment);
  742. inc(pushedparasize,size);
  743. cg.g_stackpointer_alloc(exprasmlist,size);
  744. r.enum:=stack_pointer_reg;
  745. reference_reset_base(href,r,0);
  746. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false);
  747. end
  748. else
  749. begin
  750. case p.location.loc of
  751. LOC_CONSTANT,
  752. LOC_REGISTER,
  753. LOC_CREGISTER,
  754. LOC_REFERENCE,
  755. LOC_CREFERENCE :
  756. begin
  757. cgsize:=def_cgsize(p.resulttype.def);
  758. if cgsize in [OS_64,OS_S64] then
  759. begin
  760. inc(pushedparasize,8);
  761. if calloption=pocall_inline then
  762. begin
  763. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  764. if p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  765. begin
  766. size:=align(p.resulttype.def.size,alignment);
  767. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false)
  768. end
  769. else
  770. cg64.a_load64_loc_ref(exprasmlist,p.location,href);
  771. end
  772. else
  773. cg64.a_param64_loc(exprasmlist,p.location,locpara);
  774. end
  775. else
  776. begin
  777. case cgsize of
  778. OS_8,OS_S8 :
  779. begin
  780. if alignment=4 then
  781. cgsize:=OS_32
  782. else
  783. cgsize:=OS_16;
  784. end;
  785. OS_16,OS_S16 :
  786. begin
  787. if alignment=4 then
  788. cgsize:=OS_32;
  789. end;
  790. end;
  791. { update register to use to match alignment }
  792. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  793. begin
  794. hreg:=p.location.register;
  795. p.location.register:=rg.makeregsize(p.location.register,cgsize);
  796. end;
  797. inc(pushedparasize,alignment);
  798. if calloption=pocall_inline then
  799. begin
  800. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  801. if p.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  802. begin
  803. size:=align(p.resulttype.def.size,alignment);
  804. cg.g_concatcopy(exprasmlist,p.location.reference,href,size,false,false)
  805. end
  806. else
  807. cg.a_load_loc_ref(exprasmlist,p.location,href);
  808. end
  809. else
  810. cg.a_param_loc(exprasmlist,p.location,locpara);
  811. { restore old register }
  812. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  813. p.location.register:=hreg;
  814. end;
  815. location_release(exprasmlist,p.location);
  816. end;
  817. {$ifdef SUPPORT_MMX}
  818. LOC_MMXREGISTER,
  819. LOC_CMMXREGISTER:
  820. begin
  821. inc(pushedparasize,8);
  822. if calloption=pocall_inline then
  823. begin
  824. reference_reset_base(href,procinfo.framepointer,para_offset-pushedparasize);
  825. cg.a_loadmm_reg_ref(exprasmlist,p.location.register,href);
  826. end
  827. else
  828. cg.a_parammm_reg(exprasmlist,p.location.register);
  829. end;
  830. {$endif SUPPORT_MMX}
  831. else
  832. internalerror(200204241);
  833. end;
  834. end;
  835. end;
  836. end;
  837. {****************************************************************************
  838. Entry/Exit Code
  839. ****************************************************************************}
  840. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  841. var
  842. href1,href2 : treference;
  843. list : taasmoutput;
  844. begin
  845. list:=taasmoutput(arg);
  846. if (tsym(p).typ=varsym) and
  847. (tvarsym(p).varspez=vs_value) and
  848. (paramanager.push_addr_param(tvarsym(p).vartype.def,procinfo.procdef.proccalloption)) then
  849. begin
  850. reference_reset_base(href1,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  851. if is_open_array(tvarsym(p).vartype.def) or
  852. is_array_of_const(tvarsym(p).vartype.def) then
  853. cg.g_copyvaluepara_openarray(list,href1,tarraydef(tvarsym(p).vartype.def).elesize)
  854. else
  855. begin
  856. reference_reset_base(href2,procinfo.framepointer,-tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup);
  857. if is_shortstring(tvarsym(p).vartype.def) then
  858. cg.g_copyshortstring(list,href1,href2,tstringdef(tvarsym(p).vartype.def).len,false,true)
  859. else
  860. cg.g_concatcopy(list,href1,href2,tvarsym(p).vartype.def.size,true,true);
  861. end;
  862. end;
  863. end;
  864. procedure removevalueparas(p : tnamedindexitem;arg:pointer);
  865. var
  866. href1 : treference;
  867. list : taasmoutput;
  868. begin
  869. list:=taasmoutput(arg);
  870. if (tsym(p).typ=varsym) and
  871. (tvarsym(p).varspez=vs_value) and
  872. (is_open_array(tvarsym(p).vartype.def) or
  873. is_array_of_const(tvarsym(p).vartype.def)) and
  874. (paramanager.push_addr_param(tvarsym(p).vartype.def,procinfo.procdef.proccalloption)) then
  875. begin
  876. reference_reset_base(href1,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  877. cg.g_removevaluepara_openarray(list,href1,tarraydef(tvarsym(p).vartype.def).elesize);
  878. end;
  879. end;
  880. { generates the code for initialisation of local data }
  881. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  882. var
  883. href : treference;
  884. list : taasmoutput;
  885. begin
  886. list:=taasmoutput(arg);
  887. if (tsym(p).typ=varsym) and
  888. not(vo_is_local_copy in tvarsym(p).varoptions) and
  889. assigned(tvarsym(p).vartype.def) and
  890. not(is_class(tvarsym(p).vartype.def)) and
  891. tvarsym(p).vartype.def.needs_inittable then
  892. begin
  893. if assigned(procinfo) and
  894. (cs_implicit_exceptions in aktmoduleswitches) then
  895. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  896. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  897. reference_reset_base(href,procinfo.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  898. else
  899. reference_reset_symbol(href,objectlibrary.newasmsymbol(tvarsym(p).mangledname),0);
  900. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  901. end;
  902. end;
  903. { generates the code for finalisation of local data }
  904. procedure finalize_data(p : tnamedindexitem;arg:pointer);
  905. var
  906. href : treference;
  907. list : taasmoutput;
  908. begin
  909. list:=taasmoutput(arg);
  910. case tsym(p).typ of
  911. varsym :
  912. begin
  913. if not(vo_is_local_copy in tvarsym(p).varoptions) and
  914. assigned(tvarsym(p).vartype.def) and
  915. not(is_class(tvarsym(p).vartype.def)) and
  916. tvarsym(p).vartype.def.needs_inittable then
  917. begin
  918. if tsym(p).owner.symtabletype in [localsymtable,inlinelocalsymtable] then
  919. reference_reset_base(href,procinfo.framepointer,-tvarsym(p).address+tvarsym(p).owner.address_fixup)
  920. else
  921. reference_reset_symbol(href,objectlibrary.newasmsymbol(tvarsym(p).mangledname),0);
  922. cg.g_finalize(list,tvarsym(p).vartype.def,href,false);
  923. end;
  924. end;
  925. typedconstsym :
  926. begin
  927. if ttypedconstsym(p).is_writable and
  928. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  929. begin
  930. reference_reset_symbol(href,objectlibrary.newasmsymbol(ttypedconstsym(p).mangledname),0);
  931. cg.g_finalize(list,ttypedconstsym(p).typedconsttype.def,href,false);
  932. end;
  933. end;
  934. end;
  935. end;
  936. { generates the code for incrementing the reference count of parameters and
  937. initialize out parameters }
  938. procedure init_paras(p : tnamedindexitem;arg:pointer);
  939. var
  940. href : treference;
  941. tmpreg : tregister;
  942. list : taasmoutput;
  943. begin
  944. list:=taasmoutput(arg);
  945. if (tsym(p).typ=varsym) and
  946. not is_class(tvarsym(p).vartype.def) and
  947. tvarsym(p).vartype.def.needs_inittable then
  948. begin
  949. case tvarsym(p).varspez of
  950. vs_value :
  951. begin
  952. if (cs_implicit_exceptions in aktmoduleswitches) then
  953. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  954. if assigned(tvarsym(p).localvarsym) then
  955. reference_reset_base(href,procinfo.framepointer,
  956. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  957. else
  958. reference_reset_base(href,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  959. cg.g_incrrefcount(list,tvarsym(p).vartype.def,href);
  960. end;
  961. vs_out :
  962. begin
  963. reference_reset_base(href,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  964. tmpreg:=cg.get_scratch_reg_address(list);
  965. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  966. reference_reset_base(href,tmpreg,0);
  967. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  968. cg.free_scratch_reg(list,tmpreg);
  969. end;
  970. end;
  971. end;
  972. end;
  973. { generates the code for decrementing the reference count of parameters }
  974. procedure final_paras(p : tnamedindexitem;arg:pointer);
  975. var
  976. href : treference;
  977. list : taasmoutput;
  978. begin
  979. list:=taasmoutput(arg);
  980. if (tsym(p).typ=varsym) and
  981. not is_class(tvarsym(p).vartype.def) and
  982. tvarsym(p).vartype.def.needs_inittable then
  983. begin
  984. if (tvarsym(p).varspez=vs_value) then
  985. begin
  986. if assigned(tvarsym(p).localvarsym) then
  987. reference_reset_base(href,procinfo.framepointer,
  988. -tvarsym(p).localvarsym.address+tvarsym(p).localvarsym.owner.address_fixup)
  989. else
  990. reference_reset_base(href,procinfo.framepointer,tvarsym(p).address+procinfo.para_offset);
  991. cg.g_decrrefcount(list,tvarsym(p).vartype.def,href);
  992. end;
  993. end;
  994. end;
  995. { Initialize temp ansi/widestrings,interfaces }
  996. procedure inittempvariables(list:taasmoutput);
  997. var
  998. hp : ptemprecord;
  999. href : treference;
  1000. begin
  1001. hp:=tg.templist;
  1002. while assigned(hp) do
  1003. begin
  1004. if hp^.temptype in [tt_ansistring,tt_freeansistring,
  1005. tt_widestring,tt_freewidestring,
  1006. tt_interfacecom,tt_freeinterfacecom] then
  1007. begin
  1008. if (cs_implicit_exceptions in aktmoduleswitches) then
  1009. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1010. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1011. cg.a_load_const_ref(list,OS_ADDR,0,href);
  1012. end;
  1013. hp:=hp^.next;
  1014. end;
  1015. end;
  1016. procedure finalizetempvariables(list:taasmoutput);
  1017. var
  1018. hp : ptemprecord;
  1019. href : treference;
  1020. begin
  1021. hp:=tg.templist;
  1022. while assigned(hp) do
  1023. begin
  1024. case hp^.temptype of
  1025. tt_ansistring,
  1026. tt_freeansistring :
  1027. begin
  1028. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1029. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(1));
  1030. cg.a_call_name(list,'FPC_ANSISTR_DECR_REF');
  1031. end;
  1032. tt_widestring,
  1033. tt_freewidestring :
  1034. begin
  1035. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1036. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1037. cg.a_call_name(list,'FPC_WIDESTR_DECR_REF');
  1038. end;
  1039. tt_interfacecom :
  1040. begin
  1041. reference_reset_base(href,procinfo.framepointer,hp^.pos);
  1042. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(2));
  1043. cg.a_call_name(list,'FPC_INTF_DECR_REF');
  1044. end;
  1045. end;
  1046. hp:=hp^.next;
  1047. end;
  1048. end;
  1049. procedure handle_return_value(list:TAAsmoutput; inlined : boolean;var uses_acc,uses_acchi,uses_fpu : boolean);
  1050. var
  1051. href : treference;
  1052. hreg,r,r2 : tregister;
  1053. cgsize : TCGSize;
  1054. begin
  1055. if not is_void(aktprocdef.rettype.def) then
  1056. begin
  1057. if (tfuncretsym(aktprocdef.funcretsym).funcretstate<>vs_assigned) and
  1058. (not inlined) then
  1059. CGMessage(sym_w_function_result_not_set);
  1060. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1061. cgsize:=def_cgsize(aktprocdef.rettype.def);
  1062. case aktprocdef.rettype.def.deftype of
  1063. orddef,
  1064. enumdef :
  1065. begin
  1066. uses_acc:=true;
  1067. {$WARNING accumulator was replaced by return_result_reg}
  1068. {Here, we return the function result. In most architectures, the value is
  1069. passed into the accumulator, but in a windowed architecure like sparc a
  1070. function returns in a register and the caller receives it in an other one}
  1071. r.enum:=return_result_reg;
  1072. cg.a_reg_alloc(list,r);
  1073. {$ifndef cpu64bit}
  1074. if cgsize in [OS_64,OS_S64] then
  1075. begin
  1076. uses_acchi:=true;
  1077. r.enum:=accumulatorhigh;
  1078. cg.a_reg_alloc(list,r);
  1079. r.enum:=accumulator;
  1080. r2.enum:=accumulatorhigh;
  1081. cg64.a_load64_ref_reg(list,href,joinreg64(r,r2));
  1082. end
  1083. else
  1084. {$endif cpu64bit}
  1085. begin
  1086. {$WARNING accumulator was replaced by return_result_reg}
  1087. {Here, we return the function result. In most architectures, the value is
  1088. passed into the accumulator, but in a windowed architecure like sparc a
  1089. function returns in a register and the caller receives it in an other one}
  1090. r.enum:=return_result_reg;
  1091. hreg:=rg.makeregsize(r,cgsize);
  1092. cg.a_load_ref_reg(list,cgsize,href,hreg);
  1093. end;
  1094. end;
  1095. floatdef :
  1096. begin
  1097. uses_fpu := true;
  1098. r.enum:=fpu_result_reg;
  1099. cg.a_loadfpu_ref_reg(list,cgsize,href,r);
  1100. end;
  1101. else
  1102. begin
  1103. if paramanager.ret_in_acc(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1104. begin
  1105. uses_acc:=true;
  1106. r.enum:=accumulator;
  1107. cg.a_reg_alloc(list,r);
  1108. {$ifndef cpu64bit}
  1109. { Win32 can return records in EAX:EDX }
  1110. if cgsize in [OS_64,OS_S64] then
  1111. begin
  1112. uses_acchi:=true;
  1113. r.enum:=accumulatorhigh;
  1114. cg.a_reg_alloc(list,r);
  1115. r.enum:=accumulator;
  1116. r2.enum:=accumulatorhigh;
  1117. cg64.a_load64_ref_reg(list,href,joinreg64(r,r2));
  1118. end
  1119. else
  1120. {$endif cpu64bit}
  1121. r.enum:=accumulator;
  1122. cg.a_load_ref_reg(list,cgsize,href,r);
  1123. end
  1124. end;
  1125. end;
  1126. end;
  1127. end;
  1128. procedure handle_fast_exit_return_value(list:TAAsmoutput);
  1129. var
  1130. href : treference;
  1131. hreg : tregister;
  1132. cgsize : TCGSize;
  1133. r,r2 : Tregister;
  1134. begin
  1135. if not is_void(aktprocdef.rettype.def) then
  1136. begin
  1137. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1138. cgsize:=def_cgsize(aktprocdef.rettype.def);
  1139. case aktprocdef.rettype.def.deftype of
  1140. orddef,
  1141. enumdef :
  1142. begin
  1143. {$ifndef cpu64bit}
  1144. r.enum:=accumulator;
  1145. r2.enum:=accumulatorhigh;
  1146. if cgsize in [OS_64,OS_S64] then
  1147. cg64.a_load64_reg_ref(list,joinreg64(r,r2),href)
  1148. else
  1149. {$endif cpu64bit}
  1150. begin
  1151. hreg:=rg.makeregsize(r,cgsize);
  1152. cg.a_load_reg_ref(list,cgsize,hreg,href);
  1153. end;
  1154. end;
  1155. floatdef :
  1156. begin
  1157. r.enum:=fpu_result_reg;
  1158. cg.a_loadfpu_reg_ref(list,cgsize,r,href);
  1159. end;
  1160. else
  1161. begin
  1162. r.enum:=accumulator;
  1163. if paramanager.ret_in_acc(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1164. cg.a_load_reg_ref(list,cgsize,r,href);
  1165. end;
  1166. end;
  1167. end;
  1168. end;
  1169. procedure genentrycode(list : TAAsmoutput;
  1170. make_global:boolean;
  1171. stackframe:longint;
  1172. var parasize:longint;var nostackframe:boolean;
  1173. inlined : boolean);
  1174. var
  1175. hs : string;
  1176. href : treference;
  1177. stackalloclist : taasmoutput;
  1178. hp : tparaitem;
  1179. paraloc : tparalocation;
  1180. r:Tregister;
  1181. begin
  1182. if not inlined then
  1183. stackalloclist:=taasmoutput.Create;
  1184. { the actual stack allocation code, symbol entry point and
  1185. gdb stabs information is generated AFTER the rest of this
  1186. code, since temp. allocation might occur before - carl
  1187. }
  1188. { for the save all registers we can simply use a pusha,popa which
  1189. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1190. if (po_saveregisters in aktprocdef.procoptions) then
  1191. cg.g_save_all_registers(list)
  1192. else
  1193. { should we save edi,esi,ebx like C ? }
  1194. if (po_savestdregs in aktprocdef.procoptions) then
  1195. cg.g_save_standard_registers(list,aktprocdef.usedregisters);
  1196. { the actual profile code can clobber some registers,
  1197. therefore if the context must be saved, do it before
  1198. the actual call to the profile code
  1199. }
  1200. if (cs_profile in aktmoduleswitches)
  1201. and not(po_assembler in aktprocdef.procoptions)
  1202. and not(inlined) then
  1203. begin
  1204. { non-win32 can call mcout even in main }
  1205. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1206. cg.g_profilecode(list)
  1207. else
  1208. { wdosx, and win32 should not call mcount before monstartup has been called }
  1209. if not (aktprocdef.proctypeoption=potype_proginit) then
  1210. cg.g_profilecode(list);
  1211. end;
  1212. { a constructor needs a help procedure }
  1213. if (aktprocdef.proctypeoption=potype_constructor) then
  1214. cg.g_call_constructor_helper(list);
  1215. { don't load ESI, does the caller }
  1216. { we must do it for local function }
  1217. { that can be called from a foreach_static }
  1218. { of another object than self !! PM }
  1219. if assigned(procinfo._class) and { !!!!! shouldn't we load ESI always? }
  1220. (lexlevel>normal_function_level) then
  1221. cg.g_maybe_loadself(list);
  1222. { When message method contains self as a parameter,
  1223. we must load it into ESI }
  1224. If (po_containsself in aktprocdef.procoptions) then
  1225. begin
  1226. r.enum:=self_pointer_reg;
  1227. list.concat(tai_regalloc.Alloc(r));
  1228. reference_reset_base(href,procinfo.framepointer,procinfo.selfpointer_offset);
  1229. cg.a_load_ref_reg(list,OS_ADDR,href,r);
  1230. end;
  1231. if not is_void(aktprocdef.rettype.def) then
  1232. begin
  1233. { for now the pointer to the result can't be a register }
  1234. if not(paramanager.ret_in_reg(aktprocdef.rettype.def,aktprocdef.proccalloption)) then
  1235. begin
  1236. paraloc:=paramanager.getfuncretparaloc(aktprocdef);
  1237. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1238. case paraloc.loc of
  1239. LOC_CREGISTER,
  1240. LOC_REGISTER:
  1241. if not(paraloc.size in [OS_64,OS_S64]) then
  1242. cg.a_load_reg_ref(list,paraloc.size,paraloc.register,href)
  1243. else
  1244. cg64.a_load64_reg_ref(list,paraloc.register64,href);
  1245. LOC_CFPUREGISTER,
  1246. LOC_FPUREGISTER:
  1247. cg.a_load_reg_ref(list,paraloc.size,paraloc.register,href);
  1248. LOC_CMMREGISTER,
  1249. LOC_MMREGISTER:
  1250. cg.a_loadmm_reg_ref(list,paraloc.register,href);
  1251. end;
  1252. end;
  1253. { initialize return value }
  1254. if (aktprocdef.rettype.def.needs_inittable) then
  1255. begin
  1256. if (cs_implicit_exceptions in aktmoduleswitches) then
  1257. procinfo.flags:=procinfo.flags or pi_needs_implicit_finally;
  1258. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1259. cg.g_initialize(list,aktprocdef.rettype.def,href,paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption));
  1260. end;
  1261. end;
  1262. { initialisize local data like ansistrings }
  1263. case aktprocdef.proctypeoption of
  1264. potype_unitinit:
  1265. begin
  1266. { using current_module.globalsymtable is hopefully }
  1267. { more robust than symtablestack and symtablestack.next }
  1268. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1269. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1270. end;
  1271. { units have seperate code for initilization and finalization }
  1272. potype_unitfinalize: ;
  1273. { program init/final is generated in separate procedure }
  1274. potype_proginit: ;
  1275. else
  1276. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1277. end;
  1278. { initialisizes temp. ansi/wide string data }
  1279. inittempvariables(list);
  1280. { generate copies of call by value parameters }
  1281. if not(po_assembler in aktprocdef.procoptions) then
  1282. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1283. if assigned(aktprocdef.parast) then
  1284. begin
  1285. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1286. { move register parameters which aren't regable into memory }
  1287. { we do this after init_paras because it saves some code in init_paras if parameters are in register }
  1288. { instead in memory }
  1289. hp:=tparaitem(procinfo.procdef.para.first);
  1290. while assigned(hp) do
  1291. begin
  1292. if Tvarsym(hp.parasym).reg.enum>lastreg then
  1293. internalerror(200301081);
  1294. if (tvarsym(hp.parasym).reg.enum<>R_NO) then
  1295. case hp.paraloc.loc of
  1296. LOC_CREGISTER,
  1297. LOC_REGISTER:
  1298. // if not(hp.paraloc.size in [OS_S64,OS_64]) then
  1299. cg.a_load_reg_reg(list,hp.paraloc.size,OS_32,hp.paraloc.register,tvarsym(hp.parasym).reg);
  1300. // else
  1301. // cg64.a_load64_reg_reg(list,hp.paraloc.register64,tvarsym(hp.parasym).reg);
  1302. LOC_CFPUREGISTER,
  1303. LOC_FPUREGISTER:
  1304. cg.a_loadfpu_reg_reg(list,hp.paraloc.register,tvarsym(hp.parasym).reg);
  1305. end
  1306. else if (hp.paraloc.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER,
  1307. LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER]) and
  1308. (tvarsym(hp.parasym).reg.enum=R_NO) then
  1309. begin
  1310. reference_reset_base(href,procinfo.framepointer,tvarsym(hp.parasym).address+
  1311. tvarsym(hp.parasym).owner.address_fixup);
  1312. case hp.paraloc.loc of
  1313. LOC_CREGISTER,
  1314. LOC_REGISTER:
  1315. if not(hp.paraloc.size in [OS_S64,OS_64]) then
  1316. cg.a_load_reg_ref(list,hp.paraloc.size,hp.paraloc.register,href)
  1317. else
  1318. cg64.a_load64_reg_ref(list,hp.paraloc.register64,href);
  1319. LOC_FPUREGISTER,
  1320. LOC_CFPUREGISTER:
  1321. cg.a_loadfpu_reg_ref(list,hp.paraloc.size,hp.paraloc.register,href);
  1322. else
  1323. internalerror(2002081302);
  1324. end;
  1325. end;
  1326. hp:=tparaitem(hp.next);
  1327. end;
  1328. end;
  1329. if (not inlined) then
  1330. begin
  1331. { call startup helpers from main program }
  1332. if (aktprocdef.proctypeoption=potype_proginit) then
  1333. begin
  1334. { initialize profiling for win32 }
  1335. if (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1336. (cs_profile in aktmoduleswitches) then
  1337. begin
  1338. reference_reset_symbol(href,objectlibrary.newasmsymbol('etext'),0);
  1339. cg.a_paramaddr_ref(list,href,paraloc);
  1340. reference_reset_symbol(href,objectlibrary.newasmsymbol('__image_base__'),0);
  1341. cg.a_paramaddr_ref(list,href,paraloc);
  1342. cg.a_call_name(list,'_monstartup');
  1343. end;
  1344. { initialize units }
  1345. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1346. end;
  1347. { do we need an exception frame because of ansi/widestrings/interfaces ? }
  1348. if ((procinfo.flags and pi_needs_implicit_finally)<>0) and
  1349. { but it's useless in init/final code of units }
  1350. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1351. begin
  1352. include(rg.usedinproc,accumulator);
  1353. tg.GetTemp(list,JMP_BUF_SIZE,tt_noreuse,procinfo.exception_jmp_ref);
  1354. tg.GetTemp(list,12,tt_noreuse,procinfo.exception_env_ref);
  1355. tg.GetTemp(list,sizeof(aword),tt_noreuse,procinfo.exception_result_ref);
  1356. new_exception(list,procinfo.exception_jmp_ref,
  1357. procinfo.exception_env_ref,
  1358. procinfo.exception_result_ref,1,aktexitlabel);
  1359. { probably we've to reload self here }
  1360. cg.g_maybe_loadself(list);
  1361. end;
  1362. {$ifdef GDB}
  1363. if (cs_debuginfo in aktmoduleswitches) then
  1364. list.concat(Tai_force_line.Create);
  1365. {$endif GDB}
  1366. end;
  1367. if inlined then
  1368. load_regvars(list,nil);
  1369. {************************* Stack allocation **************************}
  1370. { and symbol entry point as well as debug information }
  1371. { will be inserted in front of the rest of this list. }
  1372. { Insert alignment and assembler names }
  1373. if not inlined then
  1374. begin
  1375. { Align, gprof uses 16 byte granularity }
  1376. if (cs_profile in aktmoduleswitches) then
  1377. stackalloclist.concat(Tai_align.Create(16))
  1378. else
  1379. stackalloclist.concat(Tai_align.Create(aktalignment.procalign));
  1380. if (cs_profile in aktmoduleswitches) or
  1381. (aktprocdef.owner.symtabletype=globalsymtable) or
  1382. (assigned(procinfo._class) and (procinfo._class.owner.symtabletype=globalsymtable)) then
  1383. make_global:=true;
  1384. if make_global or ((procinfo.flags and pi_is_global) <> 0) then
  1385. aktprocsym.is_global := True;
  1386. {$ifdef GDB}
  1387. if (cs_debuginfo in aktmoduleswitches) then
  1388. begin
  1389. aktprocdef.concatstabto(stackalloclist);
  1390. aktprocsym.isstabwritten:=true;
  1391. end;
  1392. {$endif GDB}
  1393. repeat
  1394. hs:=aktprocdef.aliasnames.getfirst;
  1395. if hs='' then
  1396. break;
  1397. {$ifdef GDB}
  1398. if (cs_debuginfo in aktmoduleswitches) and
  1399. target_info.use_function_relative_addresses then
  1400. stackalloclist.concat(Tai_stab_function_name.Create(strpnew(hs)));
  1401. {$endif GDB}
  1402. if make_global then
  1403. stackalloclist.concat(Tai_symbol.Createname_global(hs,0))
  1404. else
  1405. stackalloclist.concat(Tai_symbol.Createname(hs,0));
  1406. until false;
  1407. stackframe:=stackframe+tg.gettempsize;
  1408. {$ifndef m68k}
  1409. { give a warning if the limit of local variables is reached }
  1410. if stackframe > maxlocalsize then
  1411. Message(cg_w_localsize_too_big);
  1412. {$endif}
  1413. {$ifndef powerpc}
  1414. { at least for the ppc this applies always, so this code isn't usable (FK) }
  1415. { omit stack frame ? }
  1416. if procinfo.framepointer.enum>lastreg then
  1417. internalerror(2003010801);
  1418. if (procinfo.framepointer.enum=STACK_POINTER_REG) then
  1419. begin
  1420. CGMessage(cg_d_stackframe_omited);
  1421. nostackframe:=true;
  1422. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1423. parasize:=0
  1424. else
  1425. parasize:=aktprocdef.parast.datasize+procinfo.para_offset-4;
  1426. if stackframe<>0 then
  1427. cg.g_stackpointer_alloc(stackalloclist,stackframe);
  1428. end
  1429. else
  1430. {$endif powerpc}
  1431. begin
  1432. nostackframe:=false;
  1433. if (aktprocdef.proctypeoption in [potype_unitinit,potype_proginit,potype_unitfinalize]) then
  1434. parasize:=0
  1435. else
  1436. parasize:=aktprocdef.parast.datasize+procinfo.para_offset-target_info.first_parm_offset;
  1437. if (po_interrupt in aktprocdef.procoptions) then
  1438. cg.g_interrupt_stackframe_entry(stackalloclist);
  1439. cg.g_stackframe_entry(stackalloclist,stackframe);
  1440. { never call stack checking before the standard system unit
  1441. has not been initialized
  1442. }
  1443. if (cs_check_stack in aktlocalswitches) and (aktprocdef.proctypeoption<>potype_proginit) then
  1444. cg.g_stackcheck(stackalloclist,stackframe);
  1445. end;
  1446. list.insertlist(stackalloclist);
  1447. stackalloclist.free;
  1448. end;
  1449. {************************* End Stack allocation **************************}
  1450. end;
  1451. procedure genexitcode(list : TAAsmoutput;parasize:longint;nostackframe,inlined:boolean);
  1452. var
  1453. {$ifdef GDB}
  1454. stabsendlabel : tasmlabel;
  1455. mangled_length : longint;
  1456. p : pchar;
  1457. st : string[2];
  1458. {$endif GDB}
  1459. okexitlabel,
  1460. noreraiselabel,nodestroycall : tasmlabel;
  1461. tmpreg : tregister;
  1462. href : treference;
  1463. usesacc,
  1464. usesacchi,
  1465. usesself,usesfpu : boolean;
  1466. pd : tprocdef;
  1467. r,r2:Tregister;
  1468. begin
  1469. if aktexit2label.is_used and
  1470. ((procinfo.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0) then
  1471. begin
  1472. cg.a_jmp_always(list,aktexitlabel);
  1473. cg.a_label(list,aktexit2label);
  1474. handle_fast_exit_return_value(list);
  1475. end;
  1476. if aktexitlabel.is_used then
  1477. list.concat(Tai_label.Create(aktexitlabel));
  1478. cleanup_regvars(list);
  1479. { call the destructor help procedure }
  1480. if (aktprocdef.proctypeoption=potype_destructor) and
  1481. assigned(procinfo._class) then
  1482. cg.g_call_destructor_helper(list);
  1483. { finalize temporary data }
  1484. finalizetempvariables(list);
  1485. { finalize local data like ansistrings}
  1486. case aktprocdef.proctypeoption of
  1487. potype_unitfinalize:
  1488. begin
  1489. { using current_module.globalsymtable is hopefully }
  1490. { more robust than symtablestack and symtablestack.next }
  1491. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1492. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1493. end;
  1494. { units/progs have separate code for initialization and finalization }
  1495. potype_unitinit: ;
  1496. { program init/final is generated in separate procedure }
  1497. potype_proginit: ;
  1498. else
  1499. aktprocdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1500. end;
  1501. { finalize paras data }
  1502. if assigned(aktprocdef.parast) then
  1503. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1504. { do we need to handle exceptions because of ansi/widestrings ? }
  1505. if not inlined and
  1506. ((procinfo.flags and pi_needs_implicit_finally)<>0) and
  1507. { but it's useless in init/final code of units }
  1508. not(aktprocdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  1509. begin
  1510. { the exception helper routines modify all registers }
  1511. aktprocdef.usedregisters:=all_registers;
  1512. objectlibrary.getlabel(noreraiselabel);
  1513. free_exception(list,
  1514. procinfo.exception_jmp_ref,
  1515. procinfo.exception_env_ref,
  1516. procinfo.exception_result_ref,0,
  1517. noreraiselabel,false);
  1518. tg.Ungettemp(list,procinfo.exception_jmp_ref);
  1519. tg.Ungettemp(list,procinfo.exception_env_ref);
  1520. tg.Ungettemp(list,procinfo.exception_result_ref);
  1521. if (aktprocdef.proctypeoption=potype_constructor) then
  1522. begin
  1523. if assigned(procinfo._class) then
  1524. begin
  1525. pd:=procinfo._class.searchdestructor;
  1526. if assigned(pd) then
  1527. begin
  1528. objectlibrary.getlabel(nodestroycall);
  1529. reference_reset_base(href,procinfo.framepointer,procinfo.selfpointer_offset);
  1530. cg.a_cmp_const_ref_label(list,OS_ADDR,OC_EQ,0,href,nodestroycall);
  1531. r.enum:=self_pointer_reg;
  1532. if is_class(procinfo._class) then
  1533. begin
  1534. cg.a_param_const(list,OS_INT,1,paramanager.getintparaloc(2));
  1535. cg.a_param_reg(list,OS_ADDR,r,paramanager.getintparaloc(1));
  1536. end
  1537. else if is_object(procinfo._class) then
  1538. begin
  1539. cg.a_param_reg(list,OS_ADDR,r,paramanager.getintparaloc(2));
  1540. reference_reset_symbol(href,objectlibrary.newasmsymbol(procinfo._class.vmt_mangledname),0);
  1541. cg.a_paramaddr_ref(list,href,paramanager.getintparaloc(1));
  1542. end
  1543. else
  1544. Internalerror(200006164);
  1545. if (po_virtualmethod in pd.procoptions) then
  1546. begin
  1547. reference_reset_base(href,r,0);
  1548. tmpreg:=cg.get_scratch_reg_address(list);
  1549. cg.a_load_ref_reg(list,OS_ADDR,href,tmpreg);
  1550. reference_reset_base(href,tmpreg,procinfo._class.vmtmethodoffset(pd.extnumber));
  1551. cg.free_scratch_reg(list,tmpreg);
  1552. cg.a_call_ref(list,href);
  1553. end
  1554. else
  1555. cg.a_call_name(list,pd.mangledname);
  1556. { not necessary because the result is never assigned in the
  1557. case of an exception (FK) }
  1558. cg.a_label(list,nodestroycall);
  1559. end;
  1560. end
  1561. end
  1562. else
  1563. begin
  1564. { no constructor }
  1565. { must be the return value finalized before reraising the exception? }
  1566. if (not is_void(aktprocdef.rettype.def)) and
  1567. (aktprocdef.rettype.def.needs_inittable) and
  1568. ((aktprocdef.rettype.def.deftype<>objectdef) or
  1569. not is_class(aktprocdef.rettype.def)) then
  1570. begin
  1571. reference_reset_base(href,procinfo.framepointer,procinfo.return_offset);
  1572. cg.g_finalize(list,aktprocdef.rettype.def,href,paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption));
  1573. end;
  1574. end;
  1575. cg.a_call_name(list,'FPC_RERAISE');
  1576. cg.a_label(list,noreraiselabel);
  1577. end;
  1578. { call __EXIT for main program }
  1579. if (not DLLsource) and
  1580. (not inlined) and
  1581. (aktprocdef.proctypeoption=potype_proginit) then
  1582. begin
  1583. cg.a_call_name(list,'FPC_DO_EXIT');
  1584. end;
  1585. { handle return value, this is not done for assembler routines when
  1586. they didn't reference the result variable }
  1587. usesacc:=false;
  1588. usesacchi:=false;
  1589. usesself:=false;
  1590. if not(po_assembler in aktprocdef.procoptions) or
  1591. (assigned(aktprocdef.funcretsym) and
  1592. (tfuncretsym(aktprocdef.funcretsym).refcount>1)) then
  1593. begin
  1594. if (aktprocdef.proctypeoption<>potype_constructor) then
  1595. handle_return_value(list,inlined,usesacc,usesacchi,usesfpu)
  1596. else
  1597. begin
  1598. { successful constructor deletes the zero flag }
  1599. { and returns self in eax }
  1600. { eax must be set to zero if the allocation failed !!! }
  1601. objectlibrary.getlabel(okexitlabel);
  1602. cg.a_jmp_always(list,okexitlabel);
  1603. cg.a_label(list,faillabel);
  1604. cg.g_call_fail_helper(list);
  1605. cg.a_label(list,okexitlabel);
  1606. { for classes this is done after the call to }
  1607. { AfterConstruction }
  1608. if is_object(procinfo._class) then
  1609. begin
  1610. r.enum:=self_pointer_reg;
  1611. r2.enum:=accumulator;
  1612. cg.a_reg_alloc(list,r2);
  1613. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,r,r2);
  1614. usesacc:=true;
  1615. end;
  1616. {$ifdef i386}
  1617. r.enum:=R_ESI;
  1618. list.concat(taicpu.op_reg_reg(A_TEST,S_L,r,r));
  1619. {$else}
  1620. {$warning constructor returns in flags for i386}
  1621. {$endif i386}
  1622. usesself:=true;
  1623. end;
  1624. end;
  1625. if aktexit2label.is_used and not aktexit2label.is_set then
  1626. cg.a_label(list,aktexit2label);
  1627. {$ifdef GDB}
  1628. if ((cs_debuginfo in aktmoduleswitches) and not inlined) then
  1629. begin
  1630. objectlibrary.getlabel(stabsendlabel);
  1631. cg.a_label(list,stabsendlabel);
  1632. end;
  1633. {$endif GDB}
  1634. { remove copies of call by value parameters when there are also
  1635. registers saved on the stack }
  1636. if ((po_saveregisters in aktprocdef.procoptions) or
  1637. (po_savestdregs in aktprocdef.procoptions)) and
  1638. not(po_assembler in aktprocdef.procoptions) then
  1639. aktprocdef.parast.foreach_static({$ifndef TP}@{$endif}removevalueparas,list);
  1640. { for the save all registers we can simply use a pusha,popa which
  1641. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1642. if (po_saveregisters in aktprocdef.procoptions) then
  1643. cg.g_restore_all_registers(list,usesself,usesacc,usesacchi)
  1644. else
  1645. { should we restore edi ? }
  1646. if (po_savestdregs in aktprocdef.procoptions) then
  1647. cg.g_restore_standard_registers(list,aktprocdef.usedregisters);
  1648. { remove stackframe }
  1649. if not inlined then
  1650. begin
  1651. if (not nostackframe) then
  1652. cg.g_restore_frame_pointer(list)
  1653. else
  1654. if (tg.gettempsize<>0) then
  1655. begin
  1656. r.enum:=stack_pointer_reg;
  1657. cg.a_op_const_reg(list,OP_ADD,tg.gettempsize,r);
  1658. end;
  1659. end;
  1660. { at last, the return is generated }
  1661. if not inlined then
  1662. begin
  1663. if (po_interrupt in aktprocdef.procoptions) then
  1664. cg.g_interrupt_stackframe_exit(list,usesself,usesacc,usesacchi)
  1665. else
  1666. begin
  1667. {$ifndef i386}
  1668. { give a warning if the limit of parameters allowed for
  1669. certain processors is reached.
  1670. }
  1671. if (parasize > maxparasize) then
  1672. Message(cg_w_parasize_too_big);
  1673. {$endif}
  1674. cg.g_return_from_proc(list,parasize);
  1675. end;
  1676. end;
  1677. if not inlined then
  1678. list.concat(Tai_symbol_end.Createname(aktprocdef.mangledname));
  1679. {$ifdef GDB}
  1680. if (cs_debuginfo in aktmoduleswitches) and not inlined then
  1681. begin
  1682. if assigned(procinfo._class) then
  1683. if (not assigned(procinfo.parent) or
  1684. not assigned(procinfo.parent._class)) then
  1685. begin
  1686. if (po_classmethod in aktprocdef.procoptions) or
  1687. ((po_virtualmethod in aktprocdef.procoptions) and
  1688. (potype_constructor=aktprocdef.proctypeoption)) or
  1689. (po_staticmethod in aktprocdef.procoptions) then
  1690. begin
  1691. list.concat(Tai_stabs.Create(strpnew(
  1692. '"pvmt:p'+tstoreddef(pvmttype.def).numberstring+'",'+
  1693. tostr(N_tsym)+',0,0,'+tostr(procinfo.selfpointer_offset))));
  1694. end
  1695. else
  1696. begin
  1697. if not(is_class(procinfo._class)) then
  1698. st:='v'
  1699. else
  1700. st:='p';
  1701. list.concat(Tai_stabs.Create(strpnew(
  1702. '"$t:'+st+procinfo._class.numberstring+'",'+
  1703. tostr(N_tsym)+',0,0,'+tostr(procinfo.selfpointer_offset))));
  1704. end;
  1705. end
  1706. else
  1707. begin
  1708. if not is_class(procinfo._class) then
  1709. st:='*'
  1710. else
  1711. st:='';
  1712. list.concat(Tai_stabs.Create(strpnew(
  1713. '"$t:r'+st+procinfo._class.numberstring+'",'+
  1714. tostr(N_RSYM)+',0,0,'+tostr(stab_regindex[SELF_POINTER_REG]))));
  1715. end;
  1716. { define calling EBP as pseudo local var PM }
  1717. { this enables test if the function is a local one !! }
  1718. if assigned(procinfo.parent) and (lexlevel>normal_function_level) then
  1719. list.concat(Tai_stabs.Create(strpnew(
  1720. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1721. tostr(N_LSYM)+',0,0,'+tostr(procinfo.framepointer_offset))));
  1722. if (not is_void(aktprocdef.rettype.def)) then
  1723. begin
  1724. if paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1725. list.concat(Tai_stabs.Create(strpnew(
  1726. '"'+aktprocsym.name+':X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1727. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))))
  1728. else
  1729. list.concat(Tai_stabs.Create(strpnew(
  1730. '"'+aktprocsym.name+':X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1731. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))));
  1732. if (m_result in aktmodeswitches) then
  1733. if paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1734. list.concat(Tai_stabs.Create(strpnew(
  1735. '"RESULT:X*'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1736. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))))
  1737. else
  1738. list.concat(Tai_stabs.Create(strpnew(
  1739. '"RESULT:X'+tstoreddef(aktprocdef.rettype.def).numberstring+'",'+
  1740. tostr(N_tsym)+',0,0,'+tostr(procinfo.return_offset))));
  1741. end;
  1742. mangled_length:=length(aktprocdef.mangledname);
  1743. getmem(p,2*mangled_length+50);
  1744. strpcopy(p,'192,0,0,');
  1745. strpcopy(strend(p),aktprocdef.mangledname);
  1746. if (target_info.use_function_relative_addresses) then
  1747. begin
  1748. strpcopy(strend(p),'-');
  1749. strpcopy(strend(p),aktprocdef.mangledname);
  1750. end;
  1751. list.concat(Tai_stabn.Create(strnew(p)));
  1752. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1753. +aktprocdef.mangledname))));
  1754. p[0]:='2';p[1]:='2';p[2]:='4';
  1755. strpcopy(strend(p),'_end');}
  1756. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1757. if (target_info.use_function_relative_addresses) then
  1758. begin
  1759. strpcopy(strend(p),'-');
  1760. strpcopy(strend(p),aktprocdef.mangledname);
  1761. end;
  1762. list.concatlist(withdebuglist);
  1763. list.concat(Tai_stabn.Create(strnew(p)));
  1764. { strpnew('224,0,0,'
  1765. +aktprocdef.mangledname+'_end'))));}
  1766. freemem(p,2*mangled_length+50);
  1767. end;
  1768. {$endif GDB}
  1769. if inlined then
  1770. cleanup_regvars(list);
  1771. end;
  1772. procedure genimplicitunitinit(list : TAAsmoutput);
  1773. begin
  1774. {$ifdef GDB}
  1775. if (cs_debuginfo in aktmoduleswitches) and
  1776. target_info.use_function_relative_addresses then
  1777. list.concat(Tai_stab_function_name.Create(strpnew('INIT$$'+current_module.modulename^)));
  1778. {$endif GDB}
  1779. list.concat(Tai_symbol.Createname_global('INIT$$'+current_module.modulename^,0));
  1780. list.concat(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_init',0));
  1781. { using current_module.globalsymtable is hopefully }
  1782. { more robust than symtablestack and symtablestack.next }
  1783. if assigned(current_module.globalsymtable) then
  1784. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1785. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1786. cg.g_return_from_proc(list,0);
  1787. end;
  1788. procedure genimplicitunitfinal(list : TAAsmoutput);
  1789. begin
  1790. {$ifdef GDB}
  1791. if (cs_debuginfo in aktmoduleswitches) and
  1792. target_info.use_function_relative_addresses then
  1793. list.concat(Tai_stab_function_name.Create(strpnew('FINALIZE$$'+current_module.modulename^)));
  1794. {$endif GDB}
  1795. list.concat(Tai_symbol.Createname_global('FINALIZE$$'+current_module.modulename^,0));
  1796. list.concat(Tai_symbol.Createname_global(target_info.cprefix+current_module.modulename^+'_finalize',0));
  1797. { using current_module.globalsymtable is hopefully }
  1798. { more robust than symtablestack and symtablestack.next }
  1799. if assigned(current_module.globalsymtable) then
  1800. tsymtable(current_module.globalsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1801. tsymtable(current_module.localsymtable).foreach_static({$ifdef FPCPROCVAR}@{$endif}finalize_data,list);
  1802. cg.g_return_from_proc(list,0);
  1803. end;
  1804. end.
  1805. {
  1806. $Log$
  1807. Revision 1.73 2003-01-08 18:43:56 daniel
  1808. * Tregister changed into a record
  1809. Revision 1.72 2002/12/29 23:51:43 florian
  1810. * web bug 2214 fixed: ie 10 in const array constructors
  1811. Revision 1.71 2002/12/24 15:56:50 peter
  1812. * stackpointer_alloc added for adjusting ESP. Win32 needs
  1813. this for the pageprotection
  1814. Revision 1.70 2002/12/05 14:39:21 florian
  1815. * added missing then, Carl did you really a make fullcycle :) ?
  1816. Revision 1.69 2002/12/03 22:13:39 carl
  1817. * bugfix of problems with profile code which clobbers some registers
  1818. Revision 1.68 2002/12/01 22:06:59 carl
  1819. * warning of portabilitiy problems with parasize / localsize
  1820. Revision 1.67 2002/11/30 18:44:57 carl
  1821. + profiling support for Win32
  1822. Revision 1.66 2002/11/30 14:39:15 carl
  1823. * try to fix profiling for win32
  1824. Revision 1.65 2002/11/28 23:28:14 florian
  1825. * push_value_para didn't release floatdef locations, fixes tw2045
  1826. Revision 1.64 2002/11/27 02:33:19 peter
  1827. * copy_value_on_stack method added for cdecl record passing
  1828. Revision 1.63 2002/11/25 17:43:18 peter
  1829. * splitted defbase in defutil,symutil,defcmp
  1830. * merged isconvertable and is_equal into compare_defs(_ext)
  1831. * made operator search faster by walking the list only once
  1832. Revision 1.62 2002/11/18 17:31:55 peter
  1833. * pass proccalloption to ret_in_xxx and push_xxx functions
  1834. Revision 1.61 2002/11/17 17:49:08 mazen
  1835. + return_result_reg and function_result_reg are now used, in all plateforms, to pass functions result between called function and its caller. See the explanation of each one
  1836. Revision 1.60 2002/11/17 16:31:56 carl
  1837. * memory optimization (3-4%) : cleanup of tai fields,
  1838. cleanup of tdef and tsym fields.
  1839. * make it work for m68k
  1840. Revision 1.59 2002/11/15 01:58:51 peter
  1841. * merged changes from 1.0.7 up to 04-11
  1842. - -V option for generating bug report tracing
  1843. - more tracing for option parsing
  1844. - errors for cdecl and high()
  1845. - win32 import stabs
  1846. - win32 records<=8 are returned in eax:edx (turned off by default)
  1847. - heaptrc update
  1848. - more info for temp management in .s file with EXTDEBUG
  1849. Revision 1.58 2002/11/10 19:07:45 mazen
  1850. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  1851. Revision 1.57 2002/11/03 20:22:40 mazen
  1852. * parameter handling updated
  1853. Revision 1.56 2002/10/16 19:01:43 peter
  1854. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1855. implicit exception frames for procedures with initialized variables
  1856. and for constructors. The default is on for compatibility
  1857. Revision 1.55 2002/10/14 19:42:33 peter
  1858. * only use init tables for threadvars
  1859. Revision 1.54 2002/10/06 19:41:30 peter
  1860. * Add finalization of typed consts
  1861. * Finalization of globals in the main program
  1862. Revision 1.53 2002/10/05 15:18:42 carl
  1863. * fix heap leaks
  1864. Revision 1.52 2002/09/30 07:00:46 florian
  1865. * fixes to common code to get the alpha compiler compiled applied
  1866. Revision 1.51 2002/09/22 14:02:35 carl
  1867. * stack checking cannot be called before system unit is initialized
  1868. * MC68020 define
  1869. Revision 1.50 2002/09/17 18:54:03 jonas
  1870. * a_load_reg_reg() now has two size parameters: source and dest. This
  1871. allows some optimizations on architectures that don't encode the
  1872. register size in the register name.
  1873. Revision 1.49 2002/09/10 21:48:30 florian
  1874. * improved handling of procedures with register calling conventions
  1875. Revision 1.48 2002/09/07 15:25:03 peter
  1876. * old logs removed and tabs fixed
  1877. Revision 1.47 2002/09/02 18:44:48 peter
  1878. * fixed (not) pushing of empty parameters
  1879. * fixed implicit initialization/finalization generation
  1880. * fixed/optimized local copy of value arguments init/final
  1881. Revision 1.46 2002/09/01 19:27:34 peter
  1882. * use index register when available for generating a reference with
  1883. only a signle register. Using the base register could possibly
  1884. destroy the framepointer
  1885. Revision 1.45 2002/09/01 18:50:20 peter
  1886. * fixed maybe_save that did not support a reference with only
  1887. a index register. It now also updates the location with the new
  1888. base register only
  1889. Revision 1.44 2002/09/01 14:42:41 peter
  1890. * removevaluepara added to fix the stackpointer so restoring of
  1891. saved registers works
  1892. Revision 1.43 2002/08/25 19:25:18 peter
  1893. * sym.insert_in_data removed
  1894. * symtable.insertvardata/insertconstdata added
  1895. * removed insert_in_data call from symtable.insert, it needs to be
  1896. called separatly. This allows to deref the address calculation
  1897. * procedures now calculate the parast addresses after the procedure
  1898. directives are parsed. This fixes the cdecl parast problem
  1899. * push_addr_param has an extra argument that specifies if cdecl is used
  1900. or not
  1901. Revision 1.42 2002/08/24 18:38:26 peter
  1902. * really use tt_noreuse for exception frame buffers
  1903. Revision 1.41 2002/08/23 16:14:49 peter
  1904. * tempgen cleanup
  1905. * tt_noreuse temp type added that will be used in genentrycode
  1906. Revision 1.40 2002/08/18 10:42:37 florian
  1907. * remaining assembler writer bugs fixed, the errors in the
  1908. system unit are inline assembler problems
  1909. Revision 1.39 2002/08/17 09:23:36 florian
  1910. * first part of procinfo rewrite
  1911. Revision 1.38 2002/08/16 14:24:57 carl
  1912. * issameref() to test if two references are the same (then emit no opcodes)
  1913. + ret_in_reg to replace ret_in_acc
  1914. (fix some register allocation bugs at the same time)
  1915. + save_std_register now has an extra parameter which is the
  1916. usedinproc registers
  1917. Revision 1.37 2002/08/15 15:15:55 carl
  1918. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  1919. * more generic nodes for maths
  1920. * several fixes for better m68k support
  1921. Revision 1.36 2002/08/14 19:25:09 carl
  1922. * fix Florian's last commit for m68k compilation
  1923. Revision 1.35 2002/08/13 21:40:56 florian
  1924. * more fixes for ppc calling conventions
  1925. Revision 1.34 2002/08/12 15:08:39 carl
  1926. + stab register indexes for powerpc (moved from gdb to cpubase)
  1927. + tprocessor enumeration moved to cpuinfo
  1928. + linker in target_info is now a class
  1929. * many many updates for m68k (will soon start to compile)
  1930. - removed some ifdef or correct them for correct cpu
  1931. Revision 1.33 2002/08/11 14:32:27 peter
  1932. * renamed current_library to objectlibrary
  1933. Revision 1.32 2002/08/11 13:24:12 peter
  1934. * saving of asmsymbols in ppu supported
  1935. * asmsymbollist global is removed and moved into a new class
  1936. tasmlibrarydata that will hold the info of a .a file which
  1937. corresponds with a single module. Added librarydata to tmodule
  1938. to keep the library info stored for the module. In the future the
  1939. objectfiles will also be stored to the tasmlibrarydata class
  1940. * all getlabel/newasmsymbol and friends are moved to the new class
  1941. Revision 1.31 2002/08/09 19:16:57 carl
  1942. * stack allocation is now done separately (at the end) of genentrycode
  1943. so temps. can be allocated before.
  1944. * fix generic exception handling
  1945. Revision 1.30 2002/08/06 20:55:21 florian
  1946. * first part of ppc calling conventions fix
  1947. Revision 1.29 2002/08/04 19:09:22 carl
  1948. + added generic exception support (still does not work!)
  1949. + more documentation
  1950. Revision 1.28 2002/07/29 21:23:42 florian
  1951. * more fixes for the ppc
  1952. + wrappers for the tcnvnode.first_* stuff introduced
  1953. }