ncgutil.pas 86 KB

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