ncgutil.pas 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Helper routines for all code generators
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,cpuinfo,
  22. globtype,
  23. cpubase,cgbase,parabase,cgutils,
  24. aasmbase,aasmtai,aasmcpu,
  25. symconst,symbase,symdef,symsym,symtype,symtable
  26. {$ifndef cpu64bit}
  27. ,cg64f32
  28. {$endif cpu64bit}
  29. ;
  30. type
  31. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  32. procedure firstcomplex(p : tbinarynode);
  33. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  34. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  35. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  36. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  37. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  38. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  39. { Retrieve the location of the data pointed to in location l, when the location is
  40. a register it is expected to contain the address of the data }
  41. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  42. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  43. procedure alloc_proc_symbol(pd: tprocdef);
  44. procedure gen_proc_symbol(list:Taasmoutput);
  45. procedure gen_proc_symbol_end(list:Taasmoutput);
  46. procedure gen_proc_entry_code(list:Taasmoutput);
  47. procedure gen_proc_exit_code(list:Taasmoutput);
  48. procedure gen_stack_check_size_para(list:Taasmoutput);
  49. procedure gen_stack_check_call(list:Taasmoutput);
  50. procedure gen_save_used_regs(list:TAAsmoutput);
  51. procedure gen_restore_used_regs(list:TAAsmoutput);
  52. procedure gen_initialize_code(list:TAAsmoutput);
  53. procedure gen_finalize_code(list:TAAsmoutput);
  54. procedure gen_entry_code(list:TAAsmoutput);
  55. procedure gen_exit_code(list:TAAsmoutput);
  56. procedure gen_load_para_value(list:TAAsmoutput);
  57. procedure gen_load_return_value(list:TAAsmoutput);
  58. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  59. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  60. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  61. {#
  62. Allocate the buffers for exception management and setjmp environment.
  63. Return a pointer to these buffers, send them to the utility routine
  64. so they are registered, and then call setjmp.
  65. Then compare the result of setjmp with 0, and if not equal
  66. to zero, then jump to exceptlabel.
  67. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  68. It is to note that this routine may be called *after* the stackframe of a
  69. routine has been called, therefore on machines where the stack cannot
  70. be modified, all temps should be allocated on the heap instead of the
  71. stack.
  72. }
  73. const
  74. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  75. type
  76. texceptiontemps=record
  77. jmpbuf,
  78. envbuf,
  79. reasonbuf : treference;
  80. end;
  81. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  82. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  83. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  84. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  85. procedure insertbssdata(sym : tglobalvarsym);
  86. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  87. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  88. { rtti and init/final }
  89. procedure generate_rtti(p:Ttypesym);
  90. procedure generate_inittable(p:tsym);
  91. procedure location_free(list: taasmoutput; const location : TLocation);
  92. function getprocalign : longint;
  93. procedure gen_pic_helpers(list : taasmoutput);
  94. procedure gen_got_load(list : taasmoutput);
  95. implementation
  96. uses
  97. version,
  98. cutils,cclasses,
  99. globals,systems,verbose,
  100. ppu,defutil,
  101. procinfo,paramgr,fmodule,
  102. regvars,dwarf,dbgbase,
  103. pass_1,pass_2,
  104. ncon,nld,nutils,
  105. tgobj,cgobj
  106. {$ifdef powerpc}
  107. , cpupi
  108. {$endif}
  109. {$ifdef powerpc64}
  110. , cpupi
  111. {$endif}
  112. ;
  113. {*****************************************************************************
  114. Misc Helpers
  115. *****************************************************************************}
  116. procedure location_free(list: taasmoutput; const location : TLocation);
  117. begin
  118. case location.loc of
  119. LOC_VOID:
  120. ;
  121. LOC_REGISTER,
  122. LOC_CREGISTER:
  123. begin
  124. if getsupreg(location.register)<first_int_imreg then
  125. cg.ungetcpuregister(list,location.register);
  126. end;
  127. LOC_FPUREGISTER,
  128. LOC_CFPUREGISTER:
  129. begin
  130. if getsupreg(location.register)<first_fpu_imreg then
  131. cg.ungetcpuregister(list,location.register);
  132. end;
  133. LOC_MMREGISTER,
  134. LOC_CMMREGISTER :
  135. begin
  136. if getsupreg(location.register)<first_mm_imreg then
  137. cg.ungetcpuregister(list,location.register);
  138. end;
  139. LOC_REFERENCE,
  140. LOC_CREFERENCE :
  141. begin
  142. {$ifdef cputargethasfixedstack}
  143. location_freetemp(list,location);
  144. {$endif cputargethasfixedstack}
  145. end;
  146. else
  147. internalerror(2004110211);
  148. end;
  149. end;
  150. { DO NOT RELY on the fact that the tnode is not yet swaped
  151. because of inlining code PM }
  152. procedure firstcomplex(p : tbinarynode);
  153. var
  154. hp : tnode;
  155. begin
  156. { always calculate boolean AND and OR from left to right }
  157. if (p.nodetype in [orn,andn]) and
  158. is_boolean(p.left.resulttype.def) then
  159. begin
  160. if nf_swaped in p.flags then
  161. internalerror(234234);
  162. end
  163. else
  164. if (
  165. (p.expectloc=LOC_FPUREGISTER) and
  166. (p.right.registersfpu > p.left.registersfpu)
  167. ) or
  168. (
  169. (
  170. (
  171. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  172. (p.expectloc<>LOC_FPUREGISTER)
  173. ) and
  174. (p.left.registersint<p.right.registersint)
  175. )
  176. ) then
  177. begin
  178. hp:=p.left;
  179. p.left:=p.right;
  180. p.right:=hp;
  181. if nf_swaped in p.flags then
  182. exclude(p.flags,nf_swaped)
  183. else
  184. include(p.flags,nf_swaped);
  185. end;
  186. end;
  187. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  188. {
  189. produces jumps to true respectively false labels using boolean expressions
  190. depending on whether the loading of regvars is currently being
  191. synchronized manually (such as in an if-node) or automatically (most of
  192. the other cases where this procedure is called), loadregvars can be
  193. "lr_load_regvars" or "lr_dont_load_regvars"
  194. }
  195. var
  196. opsize : tcgsize;
  197. storepos : tfileposinfo;
  198. begin
  199. if nf_error in p.flags then
  200. exit;
  201. storepos:=aktfilepos;
  202. aktfilepos:=p.fileinfo;
  203. if is_boolean(p.resulttype.def) then
  204. begin
  205. {$ifdef OLDREGVARS}
  206. if loadregvars = lr_load_regvars then
  207. load_all_regvars(list);
  208. {$endif OLDREGVARS}
  209. if is_constboolnode(p) then
  210. begin
  211. if tordconstnode(p).value<>0 then
  212. cg.a_jmp_always(list,truelabel)
  213. else
  214. cg.a_jmp_always(list,falselabel)
  215. end
  216. else
  217. begin
  218. opsize:=def_cgsize(p.resulttype.def);
  219. case p.location.loc of
  220. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  221. begin
  222. {$ifdef OLDREGVARS}
  223. if (p.location.loc = LOC_CREGISTER) then
  224. load_regvar_reg(list,p.location.register);
  225. {$endif OLDREGVARS}
  226. cg.a_cmp_const_loc_label(list,opsize,OC_NE,0,p.location,truelabel);
  227. cg.a_jmp_always(list,falselabel);
  228. end;
  229. LOC_JUMP:
  230. ;
  231. {$ifdef cpuflags}
  232. LOC_FLAGS :
  233. begin
  234. cg.a_jmp_flags(list,p.location.resflags,truelabel);
  235. cg.a_jmp_always(list,falselabel);
  236. end;
  237. {$endif cpuflags}
  238. else
  239. begin
  240. printnode(output,p);
  241. internalerror(200308241);
  242. end;
  243. end;
  244. end;
  245. end
  246. else
  247. internalerror(200112305);
  248. aktfilepos:=storepos;
  249. end;
  250. (*
  251. This code needs fixing. It is not safe to use rgint; on the m68000 it
  252. would be rgaddr.
  253. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  254. begin
  255. case t.loc of
  256. LOC_REGISTER:
  257. begin
  258. { can't be a regvar, since it would be LOC_CREGISTER then }
  259. exclude(regs,getsupreg(t.register));
  260. if t.register64.reghi<>NR_NO then
  261. exclude(regs,getsupreg(t.register64.reghi));
  262. end;
  263. LOC_CREFERENCE,LOC_REFERENCE:
  264. begin
  265. if not(cs_regvars in aktglobalswitches) or
  266. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  267. exclude(regs,getsupreg(t.reference.base));
  268. if not(cs_regvars in aktglobalswitches) or
  269. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  270. exclude(regs,getsupreg(t.reference.index));
  271. end;
  272. end;
  273. end;
  274. *)
  275. {*****************************************************************************
  276. EXCEPTION MANAGEMENT
  277. *****************************************************************************}
  278. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  279. var
  280. sym : ttypesym;
  281. begin
  282. if jmp_buf_size=-1 then
  283. begin
  284. searchsystype('JMP_BUF',sym);
  285. jmp_buf_size:=sym.restype.def.size;
  286. end;
  287. tg.GetTemp(list,EXCEPT_BUF_SIZE,tt_persistent,t.envbuf);
  288. tg.GetTemp(list,jmp_buf_size,tt_persistent,t.jmpbuf);
  289. tg.GetTemp(list,sizeof(aint),tt_persistent,t.reasonbuf);
  290. end;
  291. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  292. begin
  293. tg.Ungettemp(list,t.jmpbuf);
  294. tg.ungettemp(list,t.envbuf);
  295. tg.ungettemp(list,t.reasonbuf);
  296. end;
  297. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  298. var
  299. paraloc1,paraloc2,paraloc3 : tcgpara;
  300. begin
  301. paraloc1.init;
  302. paraloc2.init;
  303. paraloc3.init;
  304. paramanager.getintparaloc(pocall_default,1,paraloc1);
  305. paramanager.getintparaloc(pocall_default,2,paraloc2);
  306. paramanager.getintparaloc(pocall_default,3,paraloc3);
  307. paramanager.allocparaloc(list,paraloc3);
  308. cg.a_paramaddr_ref(list,t.envbuf,paraloc3);
  309. paramanager.allocparaloc(list,paraloc2);
  310. cg.a_paramaddr_ref(list,t.jmpbuf,paraloc2);
  311. { push type of exceptionframe }
  312. paramanager.allocparaloc(list,paraloc1);
  313. cg.a_param_const(list,OS_S32,1,paraloc1);
  314. paramanager.freeparaloc(list,paraloc3);
  315. paramanager.freeparaloc(list,paraloc2);
  316. paramanager.freeparaloc(list,paraloc1);
  317. cg.allocallcpuregisters(list);
  318. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  319. cg.deallocallcpuregisters(list);
  320. paramanager.getintparaloc(pocall_default,1,paraloc1);
  321. paramanager.allocparaloc(list,paraloc1);
  322. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  323. paramanager.freeparaloc(list,paraloc1);
  324. cg.allocallcpuregisters(list);
  325. cg.a_call_name(list,'FPC_SETJMP');
  326. cg.deallocallcpuregisters(list);
  327. cg.g_exception_reason_save(list, t.reasonbuf);
  328. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,cg.makeregsize(list,NR_FUNCTION_RESULT_REG,OS_S32),exceptlabel);
  329. paraloc1.done;
  330. paraloc2.done;
  331. paraloc3.done;
  332. end;
  333. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  334. begin
  335. cg.allocallcpuregisters(list);
  336. cg.a_call_name(list,'FPC_POPADDRSTACK');
  337. cg.deallocallcpuregisters(list);
  338. if not onlyfree then
  339. begin
  340. cg.g_exception_reason_load(list, t.reasonbuf);
  341. cg.a_cmp_const_reg_label(list,OS_INT,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  342. end;
  343. end;
  344. {*****************************************************************************
  345. TLocation
  346. *****************************************************************************}
  347. {$ifndef cpu64bit}
  348. { 32-bit version }
  349. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  350. var
  351. hregister,
  352. hregisterhi : tregister;
  353. hreg64 : tregister64;
  354. hl : tasmlabel;
  355. oldloc : tlocation;
  356. const_location: boolean;
  357. begin
  358. oldloc:=l;
  359. if dst_size=OS_NO then
  360. internalerror(200309144);
  361. { handle transformations to 64bit separate }
  362. if dst_size in [OS_64,OS_S64] then
  363. begin
  364. if not (l.size in [OS_64,OS_S64]) then
  365. begin
  366. { load a smaller size to OS_64 }
  367. if l.loc=LOC_REGISTER then
  368. begin
  369. hregister:=cg.makeregsize(list,l.register64.reglo,OS_32);
  370. cg.a_load_reg_reg(list,l.size,OS_32,l.register64.reglo,hregister);
  371. end
  372. else
  373. hregister:=cg.getintregister(list,OS_INT);
  374. { load value in low register }
  375. case l.loc of
  376. LOC_FLAGS :
  377. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  378. LOC_JUMP :
  379. begin
  380. cg.a_label(list,truelabel);
  381. cg.a_load_const_reg(list,OS_INT,1,hregister);
  382. objectlibrary.getjumplabel(hl);
  383. cg.a_jmp_always(list,hl);
  384. cg.a_label(list,falselabel);
  385. cg.a_load_const_reg(list,OS_INT,0,hregister);
  386. cg.a_label(list,hl);
  387. end;
  388. else
  389. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  390. end;
  391. { reset hi part, take care of the signed bit of the current value }
  392. hregisterhi:=cg.getintregister(list,OS_INT);
  393. if (l.size in [OS_S8,OS_S16,OS_S32]) then
  394. begin
  395. if l.loc=LOC_CONSTANT then
  396. begin
  397. if (longint(l.value)<0) then
  398. cg.a_load_const_reg(list,OS_32,aint($ffffffff),hregisterhi)
  399. else
  400. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  401. end
  402. else
  403. begin
  404. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  405. hregisterhi);
  406. end;
  407. end
  408. else
  409. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  410. location_reset(l,LOC_REGISTER,dst_size);
  411. l.register64.reglo:=hregister;
  412. l.register64.reghi:=hregisterhi;
  413. end
  414. else
  415. begin
  416. { 64bit to 64bit }
  417. if ((l.loc=LOC_CREGISTER) and maybeconst) then
  418. begin
  419. hregister:=l.register64.reglo;
  420. hregisterhi:=l.register64.reghi;
  421. const_location := true;
  422. end
  423. else
  424. begin
  425. hregister:=cg.getintregister(list,OS_INT);
  426. hregisterhi:=cg.getintregister(list,OS_INT);
  427. const_location := false;
  428. end;
  429. hreg64.reglo:=hregister;
  430. hreg64.reghi:=hregisterhi;
  431. { load value in new register }
  432. cg64.a_load64_loc_reg(list,l,hreg64);
  433. if not const_location then
  434. location_reset(l,LOC_REGISTER,dst_size)
  435. else
  436. location_reset(l,LOC_CREGISTER,dst_size);
  437. l.register64.reglo:=hregister;
  438. l.register64.reghi:=hregisterhi;
  439. end;
  440. end
  441. else
  442. begin
  443. {Do not bother to recycle the existing register. The register
  444. allocator eliminates unnecessary moves, so it's not needed
  445. and trying to recycle registers can cause problems because
  446. the registers changes size and may need aditional constraints.
  447. Not if it's about LOC_CREGISTER's (JM)
  448. }
  449. const_location :=
  450. (maybeconst) and
  451. (l.loc = LOC_CREGISTER) and
  452. (TCGSize2Size[l.size] = TCGSize2Size[dst_size]) and
  453. ((l.size = dst_size) or
  454. (TCGSize2Size[l.size] = TCGSize2Size[OS_INT]));
  455. if not const_location then
  456. hregister:=cg.getintregister(list,dst_size)
  457. else
  458. hregister := l.register;
  459. { load value in new register }
  460. case l.loc of
  461. LOC_FLAGS :
  462. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  463. LOC_JUMP :
  464. begin
  465. cg.a_label(list,truelabel);
  466. cg.a_load_const_reg(list,dst_size,1,hregister);
  467. objectlibrary.getjumplabel(hl);
  468. cg.a_jmp_always(list,hl);
  469. cg.a_label(list,falselabel);
  470. cg.a_load_const_reg(list,dst_size,0,hregister);
  471. cg.a_label(list,hl);
  472. end;
  473. else
  474. begin
  475. { load_loc_reg can only handle size >= l.size, when the
  476. new size is smaller then we need to adjust the size
  477. of the orignal and maybe recalculate l.register for i386 }
  478. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  479. begin
  480. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  481. l.register:=cg.makeregsize(list,l.register,dst_size);
  482. { for big endian systems, the reference's offset must }
  483. { be increased in this case, since they have the }
  484. { MSB first in memory and e.g. byte(word_var) should }
  485. { return the second byte in this case (JM) }
  486. if (target_info.endian = ENDIAN_BIG) and
  487. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  488. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  489. {$ifdef x86}
  490. l.size:=dst_size;
  491. {$endif x86}
  492. end;
  493. cg.a_load_loc_reg(list,dst_size,l,hregister);
  494. {$ifndef x86}
  495. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  496. l.size:=dst_size;
  497. {$endif not x86}
  498. end;
  499. end;
  500. if not const_location then
  501. location_reset(l,LOC_REGISTER,dst_size)
  502. else
  503. location_reset(l,LOC_CREGISTER,dst_size);
  504. l.register:=hregister;
  505. end;
  506. { Release temp when it was a reference }
  507. if oldloc.loc=LOC_REFERENCE then
  508. location_freetemp(list,oldloc);
  509. end;
  510. {$else cpu64bit}
  511. { 64-bit version }
  512. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  513. var
  514. hregister : tregister;
  515. hl : tasmlabel;
  516. oldloc : tlocation;
  517. begin
  518. oldloc:=l;
  519. hregister:=cg.getintregister(list,dst_size);
  520. { load value in new register }
  521. case l.loc of
  522. LOC_FLAGS :
  523. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  524. LOC_JUMP :
  525. begin
  526. cg.a_label(list,truelabel);
  527. cg.a_load_const_reg(list,dst_size,1,hregister);
  528. objectlibrary.getjumplabel(hl);
  529. cg.a_jmp_always(list,hl);
  530. cg.a_label(list,falselabel);
  531. cg.a_load_const_reg(list,dst_size,0,hregister);
  532. cg.a_label(list,hl);
  533. end;
  534. else
  535. begin
  536. { load_loc_reg can only handle size >= l.size, when the
  537. new size is smaller then we need to adjust the size
  538. of the orignal and maybe recalculate l.register for i386 }
  539. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  540. begin
  541. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  542. l.register:=cg.makeregsize(list,l.register,dst_size);
  543. { for big endian systems, the reference's offset must }
  544. { be increased in this case, since they have the }
  545. { MSB first in memory and e.g. byte(word_var) should }
  546. { return the second byte in this case (JM) }
  547. if (target_info.endian = ENDIAN_BIG) and
  548. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  549. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  550. {$ifdef x86}
  551. l.size:=dst_size;
  552. {$endif x86}
  553. end;
  554. cg.a_load_loc_reg(list,dst_size,l,hregister);
  555. {$ifndef x86}
  556. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  557. l.size:=dst_size;
  558. {$endif not x86}
  559. end;
  560. end;
  561. if (l.loc <> LOC_CREGISTER) or
  562. not maybeconst then
  563. location_reset(l,LOC_REGISTER,dst_size)
  564. else
  565. location_reset(l,LOC_CREGISTER,dst_size);
  566. l.register:=hregister;
  567. { Release temp when it was a reference }
  568. if oldloc.loc=LOC_REFERENCE then
  569. location_freetemp(list,oldloc);
  570. end;
  571. {$endif cpu64bit}
  572. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  573. var
  574. reg : tregister;
  575. href : treference;
  576. begin
  577. if (l.loc<>LOC_FPUREGISTER) and
  578. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  579. begin
  580. { if it's in an mm register, store to memory first }
  581. if (l.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  582. begin
  583. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  584. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,href,mms_movescalar);
  585. location_reset(l,LOC_REFERENCE,l.size);
  586. l.reference:=href;
  587. end;
  588. reg:=cg.getfpuregister(list,l.size);
  589. cg.a_loadfpu_loc_reg(list,l,reg);
  590. location_freetemp(list,l);
  591. location_reset(l,LOC_FPUREGISTER,l.size);
  592. l.register:=reg;
  593. end;
  594. end;
  595. procedure location_force_mmregscalar(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  596. var
  597. reg : tregister;
  598. href : treference;
  599. begin
  600. if (l.loc<>LOC_MMREGISTER) and
  601. ((l.loc<>LOC_CMMREGISTER) or (not maybeconst)) then
  602. begin
  603. { if it's in an fpu register, store to memory first }
  604. if (l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  605. begin
  606. tg.GetTemp(list,tcgsize2size[l.size],tt_normal,href);
  607. cg.a_loadfpu_reg_ref(list,l.size,l.register,href);
  608. location_reset(l,LOC_REFERENCE,l.size);
  609. l.reference:=href;
  610. end;
  611. reg:=cg.getmmregister(list,l.size);
  612. cg.a_loadmm_loc_reg(list,l.size,l,reg,mms_movescalar);
  613. location_freetemp(list,l);
  614. location_reset(l,LOC_MMREGISTER,l.size);
  615. l.register:=reg;
  616. end;
  617. end;
  618. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  619. var
  620. r : treference;
  621. begin
  622. case l.loc of
  623. LOC_FPUREGISTER,
  624. LOC_CFPUREGISTER :
  625. begin
  626. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  627. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  628. location_reset(l,LOC_REFERENCE,l.size);
  629. l.reference:=r;
  630. end;
  631. LOC_MMREGISTER,
  632. LOC_CMMREGISTER:
  633. begin
  634. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  635. cg.a_loadmm_reg_ref(list,l.size,l.size,l.register,r,mms_movescalar);
  636. location_reset(l,LOC_REFERENCE,l.size);
  637. l.reference:=r;
  638. end;
  639. LOC_CONSTANT,
  640. LOC_REGISTER,
  641. LOC_CREGISTER :
  642. begin
  643. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  644. {$ifndef cpu64bit}
  645. if l.size in [OS_64,OS_S64] then
  646. cg64.a_load64_loc_ref(list,l,r)
  647. else
  648. {$endif cpu64bit}
  649. cg.a_load_loc_ref(list,l.size,l,r);
  650. location_reset(l,LOC_REFERENCE,l.size);
  651. l.reference:=r;
  652. end;
  653. LOC_CREFERENCE,
  654. LOC_REFERENCE : ;
  655. else
  656. internalerror(200203219);
  657. end;
  658. end;
  659. procedure location_get_data_ref(list:TAAsmoutput;const l:tlocation;var ref:treference;loadref:boolean);
  660. begin
  661. case l.loc of
  662. LOC_REGISTER,
  663. LOC_CREGISTER :
  664. begin
  665. if not loadref then
  666. internalerror(200410231);
  667. reference_reset_base(ref,l.register,0);
  668. end;
  669. LOC_REFERENCE,
  670. LOC_CREFERENCE :
  671. begin
  672. if loadref then
  673. begin
  674. reference_reset_base(ref,cg.getaddressregister(list),0);
  675. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,l.reference,ref.base);
  676. end
  677. else
  678. ref:=l.reference;
  679. end;
  680. else
  681. internalerror(200309181);
  682. end;
  683. end;
  684. {*****************************************************************************
  685. Maybe_Save
  686. *****************************************************************************}
  687. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  688. begin
  689. {$ifdef i386}
  690. if (needed>=maxfpuregs) and
  691. (l.loc = LOC_FPUREGISTER) then
  692. begin
  693. location_force_mem(list,l);
  694. maybe_pushfpu:=true;
  695. end
  696. else
  697. maybe_pushfpu:=false;
  698. {$else i386}
  699. maybe_pushfpu:=false;
  700. {$endif i386}
  701. end;
  702. {****************************************************************************
  703. Init/Finalize Code
  704. ****************************************************************************}
  705. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  706. var
  707. href : treference;
  708. hreg : tregister;
  709. list : TAAsmoutput;
  710. hsym : tparavarsym;
  711. l : longint;
  712. localcopyloc : tlocation;
  713. begin
  714. list:=taasmoutput(arg);
  715. if (tsym(p).typ=paravarsym) and
  716. (tparavarsym(p).varspez=vs_value) and
  717. (paramanager.push_addr_param(tparavarsym(p).varspez,tparavarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  718. begin
  719. location_get_data_ref(list,tparavarsym(p).localloc,href,true);
  720. if is_open_array(tparavarsym(p).vartype.def) or
  721. is_array_of_const(tparavarsym(p).vartype.def) then
  722. begin
  723. { cdecl functions don't have a high pointer so it is not possible to generate
  724. a local copy }
  725. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  726. begin
  727. hsym:=tparavarsym(tsym(p).owner.search('high'+p.name));
  728. if not assigned(hsym) then
  729. internalerror(200306061);
  730. hreg:=cg.getaddressregister(list);
  731. cg.g_copyvaluepara_openarray(list,href,hsym.localloc,tarraydef(tparavarsym(p).vartype.def).elesize,hreg);
  732. cg.a_load_reg_loc(list,OS_ADDR,hreg,tparavarsym(p).localloc);
  733. end;
  734. end
  735. else
  736. begin
  737. { Allocate space for the local copy }
  738. l:=tparavarsym(p).getsize;
  739. localcopyloc.loc:=LOC_REFERENCE;
  740. localcopyloc.size:=int_cgsize(l);
  741. tg.GetLocal(list,l,tparavarsym(p).vartype.def,localcopyloc.reference);
  742. { Copy data }
  743. if is_shortstring(tparavarsym(p).vartype.def) then
  744. begin
  745. { this code is only executed before the code for the body and the entry/exit code is generated
  746. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  747. }
  748. include(current_procinfo.flags,pi_do_call);
  749. cg.g_copyshortstring(list,href,localcopyloc.reference,tstringdef(tparavarsym(p).vartype.def).len)
  750. end
  751. else
  752. cg.g_concatcopy(list,href,localcopyloc.reference,tparavarsym(p).vartype.def.size);
  753. { update localloc of varsym }
  754. tg.Ungetlocal(list,tparavarsym(p).localloc.reference);
  755. tparavarsym(p).localloc:=localcopyloc;
  756. end;
  757. end;
  758. end;
  759. { initializes the regvars from staticsymtable with 0 }
  760. procedure initialize_regvars(p : tnamedindexitem;arg:pointer);
  761. begin
  762. if (tsym(p).typ=globalvarsym) then
  763. begin
  764. case tglobalvarsym(p).localloc.loc of
  765. LOC_CREGISTER :
  766. cg.a_load_const_reg(taasmoutput(arg),reg_cgsize(tglobalvarsym(p).localloc.register),0,
  767. tglobalvarsym(p).localloc.register);
  768. LOC_REFERENCE : ;
  769. LOC_CMMREGISTER :
  770. { clear the whole register }
  771. cg.a_opmm_reg_reg(taasmoutput(arg),OP_XOR,reg_cgsize(tglobalvarsym(p).localloc.register),
  772. tglobalvarsym(p).localloc.register,
  773. tglobalvarsym(p).localloc.register,
  774. nil);
  775. LOC_CFPUREGISTER :
  776. ;
  777. else
  778. internalerror(200410124);
  779. end;
  780. end;
  781. end;
  782. { generates the code for initialisation of local data }
  783. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  784. var
  785. oldexprasmlist : TAAsmoutput;
  786. hp : tnode;
  787. begin
  788. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  789. (tabstractvarsym(p).refs>0) and
  790. not(is_class(tabstractvarsym(p).vartype.def)) and
  791. tabstractvarsym(p).vartype.def.needs_inittable then
  792. begin
  793. oldexprasmlist:=exprasmlist;
  794. exprasmlist:=taasmoutput(arg);
  795. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  796. firstpass(hp);
  797. secondpass(hp);
  798. hp.free;
  799. exprasmlist:=oldexprasmlist;
  800. end;
  801. end;
  802. procedure finalize_sym(asmlist:taasmoutput;sym:tsym);
  803. var
  804. hp : tnode;
  805. oldexprasmlist : TAAsmoutput;
  806. begin
  807. include(current_procinfo.flags,pi_needs_implicit_finally);
  808. oldexprasmlist:=exprasmlist;
  809. exprasmlist:=asmlist;
  810. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  811. firstpass(hp);
  812. secondpass(hp);
  813. hp.free;
  814. exprasmlist:=oldexprasmlist;
  815. end;
  816. { generates the code for finalisation of local variables }
  817. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  818. begin
  819. if (tsym(p).typ=localvarsym) and
  820. (tlocalvarsym(p).refs>0) and
  821. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  822. not(is_class(tlocalvarsym(p).vartype.def)) and
  823. tlocalvarsym(p).vartype.def.needs_inittable then
  824. finalize_sym(taasmoutput(arg),tsym(p));
  825. end;
  826. { generates the code for finalisation of local typedconsts }
  827. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  828. var
  829. i : longint;
  830. pd : tprocdef;
  831. begin
  832. case tsym(p).typ of
  833. typedconstsym :
  834. begin
  835. if ttypedconstsym(p).is_writable and
  836. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  837. finalize_sym(taasmoutput(arg),tsym(p));
  838. end;
  839. procsym :
  840. begin
  841. for i:=1 to tprocsym(p).procdef_count do
  842. begin
  843. pd:=tprocsym(p).procdef[i];
  844. if assigned(pd.localst) and
  845. (pd.procsym=tprocsym(p)) and
  846. (pd.localst.symtabletype<>staticsymtable) then
  847. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  848. end;
  849. end;
  850. end;
  851. end;
  852. { generates the code for finalization of static symtable and
  853. all local (static) typedconsts }
  854. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  855. var
  856. i : longint;
  857. pd : tprocdef;
  858. begin
  859. case tsym(p).typ of
  860. globalvarsym :
  861. begin
  862. if (tglobalvarsym(p).refs>0) and
  863. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  864. not(is_class(tglobalvarsym(p).vartype.def)) and
  865. tglobalvarsym(p).vartype.def.needs_inittable then
  866. finalize_sym(taasmoutput(arg),tsym(p));
  867. end;
  868. typedconstsym :
  869. begin
  870. if ttypedconstsym(p).is_writable and
  871. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  872. finalize_sym(taasmoutput(arg),tsym(p));
  873. end;
  874. procsym :
  875. begin
  876. for i:=1 to tprocsym(p).procdef_count do
  877. begin
  878. pd:=tprocsym(p).procdef[i];
  879. if assigned(pd.localst) and
  880. (pd.procsym=tprocsym(p)) and
  881. (pd.localst.symtabletype<>staticsymtable) then
  882. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  883. end;
  884. end;
  885. end;
  886. end;
  887. { generates the code for incrementing the reference count of parameters and
  888. initialize out parameters }
  889. procedure init_paras(p : tnamedindexitem;arg:pointer);
  890. var
  891. href : treference;
  892. tmpreg : tregister;
  893. list : TAAsmoutput;
  894. begin
  895. list:=taasmoutput(arg);
  896. if (tsym(p).typ=paravarsym) and
  897. not is_class_or_interface(tparavarsym(p).vartype.def) and
  898. tparavarsym(p).vartype.def.needs_inittable then
  899. begin
  900. case tparavarsym(p).varspez of
  901. vs_value :
  902. begin
  903. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  904. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  905. end;
  906. vs_out :
  907. begin
  908. tmpreg:=cg.getaddressregister(list);
  909. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  910. reference_reset_base(href,tmpreg,0);
  911. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  912. end;
  913. end;
  914. end;
  915. end;
  916. { generates the code for decrementing the reference count of parameters }
  917. procedure final_paras(p : tnamedindexitem;arg:pointer);
  918. var
  919. list : TAAsmoutput;
  920. href : treference;
  921. begin
  922. if not(tsym(p).typ=paravarsym) then
  923. exit;
  924. list:=taasmoutput(arg);
  925. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  926. tparavarsym(p).vartype.def.needs_inittable then
  927. begin
  928. if (tparavarsym(p).varspez=vs_value) then
  929. begin
  930. include(current_procinfo.flags,pi_needs_implicit_finally);
  931. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  932. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  933. end;
  934. end
  935. else
  936. if (tparavarsym(p).varspez=vs_value) and
  937. (is_open_array(tparavarsym(p).vartype.def) or
  938. is_array_of_const(tparavarsym(p).vartype.def)) then
  939. begin
  940. { cdecl functions don't have a high pointer so it is not possible to generate
  941. a local copy }
  942. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  943. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  944. end;
  945. end;
  946. { Initialize temp ansi/widestrings,interfaces }
  947. procedure inittempvariables(list:taasmoutput);
  948. var
  949. hp : ptemprecord;
  950. href : treference;
  951. begin
  952. hp:=tg.templist;
  953. while assigned(hp) do
  954. begin
  955. if assigned(hp^.def) and
  956. hp^.def.needs_inittable then
  957. begin
  958. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  959. cg.g_initialize(list,hp^.def,href);
  960. end;
  961. hp:=hp^.next;
  962. end;
  963. end;
  964. procedure finalizetempvariables(list:taasmoutput);
  965. var
  966. hp : ptemprecord;
  967. href : treference;
  968. begin
  969. hp:=tg.templist;
  970. while assigned(hp) do
  971. begin
  972. if assigned(hp^.def) and
  973. hp^.def.needs_inittable then
  974. begin
  975. include(current_procinfo.flags,pi_needs_implicit_finally);
  976. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  977. cg.g_finalize(list,hp^.def,href);
  978. end;
  979. hp:=hp^.next;
  980. end;
  981. end;
  982. procedure gen_load_return_value(list:TAAsmoutput);
  983. var
  984. {$ifndef cpu64bit}
  985. href : treference;
  986. {$endif cpu64bit}
  987. ressym : tabstractnormalvarsym;
  988. resloc,
  989. restmploc : tlocation;
  990. hreg : tregister;
  991. funcretloc : tlocation;
  992. begin
  993. { Is the loading needed? }
  994. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  995. (
  996. (po_assembler in current_procinfo.procdef.procoptions) and
  997. (not(assigned(current_procinfo.procdef.funcretsym)) or
  998. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  999. ) then
  1000. exit;
  1001. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  1002. { constructors return self }
  1003. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1004. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  1005. else
  1006. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1007. if (ressym.refs>0) then
  1008. begin
  1009. {$ifdef OLDREGVARS}
  1010. case ressym.localloc.loc of
  1011. LOC_CFPUREGISTER,
  1012. LOC_FPUREGISTER:
  1013. begin
  1014. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1015. restmploc.register:=ressym.localloc.register;
  1016. end;
  1017. LOC_CREGISTER,
  1018. LOC_REGISTER:
  1019. begin
  1020. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1021. restmploc.register:=ressym.localloc.register;
  1022. end;
  1023. LOC_MMREGISTER:
  1024. begin
  1025. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1026. restmploc.register:=ressym.localloc.register;
  1027. end;
  1028. LOC_REFERENCE:
  1029. begin
  1030. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1031. restmploc.reference:=ressym.localloc.reference;
  1032. end;
  1033. else
  1034. internalerror(200309184);
  1035. end;
  1036. {$else}
  1037. restmploc:=ressym.localloc;
  1038. {$endif}
  1039. { Here, we return the function result. In most architectures, the value is
  1040. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1041. function returns in a register and the caller receives it in an other one }
  1042. case funcretloc.loc of
  1043. LOC_REGISTER:
  1044. begin
  1045. {$ifndef cpu64bit}
  1046. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1047. begin
  1048. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1049. if resloc.loc<>LOC_REGISTER then
  1050. internalerror(200409141);
  1051. { Load low and high register separate to generate better register
  1052. allocation info }
  1053. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1054. begin
  1055. cg.getcpuregister(list,resloc.register64.reglo);
  1056. cg.ungetcpuregister(list,resloc.register64.reglo);
  1057. { for the optimizer }
  1058. cg.a_reg_alloc(list,resloc.register64.reglo);
  1059. end;
  1060. case restmploc.loc of
  1061. LOC_REFERENCE :
  1062. begin
  1063. href:=restmploc.reference;
  1064. if target_info.endian=ENDIAN_BIG then
  1065. inc(href.offset,4);
  1066. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1067. end;
  1068. LOC_CREGISTER :
  1069. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1070. else
  1071. internalerror(200409203);
  1072. end;
  1073. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1074. begin
  1075. cg.getcpuregister(list,resloc.register64.reghi);
  1076. cg.ungetcpuregister(list,resloc.register64.reghi);
  1077. { for the optimizer }
  1078. cg.a_reg_alloc(list,resloc.register64.reghi);
  1079. end;
  1080. case restmploc.loc of
  1081. LOC_REFERENCE :
  1082. begin
  1083. href:=restmploc.reference;
  1084. if target_info.endian=ENDIAN_LITTLE then
  1085. inc(href.offset,4);
  1086. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1087. end;
  1088. LOC_CREGISTER :
  1089. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1090. else
  1091. internalerror(200409204);
  1092. end;
  1093. end
  1094. else
  1095. {$endif cpu64bit}
  1096. begin
  1097. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1098. if getsupreg(funcretloc.register)<first_int_imreg then
  1099. begin
  1100. cg.getcpuregister(list,funcretloc.register);
  1101. cg.ungetcpuregister(list,hreg);
  1102. { for the optimizer }
  1103. cg.a_reg_alloc(list,funcretloc.register);
  1104. end;
  1105. { it could be that a structure is passed in memory but the function is expected to
  1106. return a pointer to this memory }
  1107. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1108. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1109. else
  1110. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1111. end;
  1112. end;
  1113. LOC_FPUREGISTER:
  1114. begin
  1115. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1116. begin
  1117. cg.getcpuregister(list,funcretloc.register);
  1118. cg.ungetcpuregister(list,funcretloc.register);
  1119. end;
  1120. { we can't do direct moves between fpu and mm registers }
  1121. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1122. location_force_fpureg(list,restmploc,false);
  1123. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1124. end;
  1125. LOC_MMREGISTER:
  1126. begin
  1127. if getsupreg(funcretloc.register)<first_mm_imreg then
  1128. begin
  1129. cg.getcpuregister(list,funcretloc.register);
  1130. cg.ungetcpuregister(list,funcretloc.register);
  1131. end;
  1132. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1133. end;
  1134. LOC_INVALID,
  1135. LOC_REFERENCE:
  1136. ;
  1137. else
  1138. internalerror(200405025);
  1139. end;
  1140. end;
  1141. end;
  1142. procedure gen_alloc_regvar(list:TAAsmoutput;sym: tabstractnormalvarsym);
  1143. begin
  1144. case sym.localloc.loc of
  1145. LOC_CREGISTER:
  1146. begin
  1147. {$ifndef cpu64bit}
  1148. if sym.localloc.size in [OS_64,OS_S64] then
  1149. begin
  1150. sym.localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1151. sym.localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1152. end
  1153. else
  1154. {$endif cpu64bit}
  1155. sym.localloc.register:=cg.getintregister(list,sym.localloc.size);
  1156. end;
  1157. LOC_CFPUREGISTER:
  1158. begin
  1159. sym.localloc.register:=cg.getfpuregister(list,sym.localloc.size);
  1160. end;
  1161. LOC_CMMREGISTER:
  1162. begin
  1163. sym. localloc.register:=cg.getmmregister(list,sym.localloc.size);
  1164. end;
  1165. end;
  1166. { Allocate register already, to prevent first allocation to be
  1167. inside a loop }
  1168. {$ifndef cpu64bit}
  1169. if sym.localloc.size in [OS_64,OS_S64] then
  1170. begin
  1171. cg.a_reg_sync(list,sym.localloc.register64.reglo);
  1172. cg.a_reg_sync(list,sym.localloc.register64.reghi);
  1173. end
  1174. else
  1175. {$endif cpu64bit}
  1176. cg.a_reg_sync(list,sym.localloc.register);
  1177. if cs_asm_source in aktglobalswitches then
  1178. begin
  1179. case sym.localloc.loc of
  1180. LOC_REGISTER,
  1181. LOC_CREGISTER :
  1182. begin
  1183. if (cs_no_regalloc in aktglobalswitches) then
  1184. list.concat(Tai_comment.Create(strpnew('Var '+sym.realname+' located in register '+
  1185. std_regname(sym.localloc.register))))
  1186. else
  1187. list.concat(Tai_comment.Create(strpnew('Var '+sym.realname+' located in register')));
  1188. end;
  1189. end
  1190. end;
  1191. end;
  1192. procedure gen_load_para_value(list:TAAsmoutput);
  1193. procedure get_para(const paraloc:TCGParaLocation);
  1194. begin
  1195. case paraloc.loc of
  1196. LOC_REGISTER :
  1197. begin
  1198. if getsupreg(paraloc.register)<first_int_imreg then
  1199. cg.getcpuregister(list,paraloc.register);
  1200. end;
  1201. LOC_MMREGISTER :
  1202. begin
  1203. if getsupreg(paraloc.register)<first_mm_imreg then
  1204. cg.getcpuregister(list,paraloc.register);
  1205. end;
  1206. LOC_FPUREGISTER :
  1207. begin
  1208. if getsupreg(paraloc.register)<first_fpu_imreg then
  1209. cg.getcpuregister(list,paraloc.register);
  1210. end;
  1211. end;
  1212. end;
  1213. procedure unget_para(const paraloc:TCGParaLocation);
  1214. begin
  1215. case paraloc.loc of
  1216. LOC_REGISTER :
  1217. begin
  1218. if getsupreg(paraloc.register)<first_int_imreg then
  1219. cg.ungetcpuregister(list,paraloc.register);
  1220. end;
  1221. LOC_MMREGISTER :
  1222. begin
  1223. if getsupreg(paraloc.register)<first_mm_imreg then
  1224. cg.ungetcpuregister(list,paraloc.register);
  1225. end;
  1226. LOC_FPUREGISTER :
  1227. begin
  1228. if getsupreg(paraloc.register)<first_fpu_imreg then
  1229. cg.ungetcpuregister(list,paraloc.register);
  1230. end;
  1231. end;
  1232. end;
  1233. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1234. var
  1235. href : treference;
  1236. begin
  1237. case paraloc.loc of
  1238. LOC_REGISTER :
  1239. begin
  1240. {$IFDEF POWERPC64}
  1241. if (paraloc.shiftval <> 0) then
  1242. cg.a_op_const_reg_reg(list, OP_SHL, OS_INT, paraloc.shiftval, paraloc.register, paraloc.register);
  1243. {$ENDIF POWERPC64}
  1244. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1245. end;
  1246. LOC_MMREGISTER :
  1247. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1248. LOC_FPUREGISTER :
  1249. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1250. LOC_REFERENCE :
  1251. begin
  1252. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1253. { use concatcopy, because it can also be a float which fails when
  1254. load_ref_ref is used. Don't copy data when the references are equal }
  1255. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1256. cg.g_concatcopy(list,href,ref,sizeleft);
  1257. end;
  1258. else
  1259. internalerror(2002081302);
  1260. end;
  1261. end;
  1262. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1263. var
  1264. href : treference;
  1265. begin
  1266. case paraloc.loc of
  1267. LOC_REGISTER :
  1268. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1269. LOC_MMREGISTER :
  1270. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1271. LOC_FPUREGISTER :
  1272. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1273. LOC_REFERENCE :
  1274. begin
  1275. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1276. case getregtype(reg) of
  1277. R_INTREGISTER :
  1278. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1279. R_FPUREGISTER :
  1280. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1281. R_MMREGISTER :
  1282. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1283. else
  1284. internalerror(2004101012);
  1285. end;
  1286. end;
  1287. else
  1288. internalerror(2002081302);
  1289. end;
  1290. end;
  1291. var
  1292. i : longint;
  1293. currpara : tparavarsym;
  1294. paraloc : pcgparalocation;
  1295. href : treference;
  1296. sizeleft : aint;
  1297. {$ifdef sparc}
  1298. tempref : treference;
  1299. {$endif sparc}
  1300. begin
  1301. if (po_assembler in current_procinfo.procdef.procoptions) then
  1302. exit;
  1303. { Allocate registers used by parameters }
  1304. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1305. begin
  1306. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1307. paraloc:=currpara.paraloc[calleeside].location;
  1308. while assigned(paraloc) do
  1309. begin
  1310. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1311. get_para(paraloc^);
  1312. paraloc:=paraloc^.next;
  1313. end;
  1314. end;
  1315. { Copy parameters to local references/registers }
  1316. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1317. begin
  1318. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1319. paraloc:=currpara.paraloc[calleeside].location;
  1320. { skip e.g. empty records }
  1321. if not assigned(paraloc) then
  1322. internalerror(200408203);
  1323. if (paraloc^.loc = LOC_VOID) then
  1324. continue;
  1325. case currpara.localloc.loc of
  1326. LOC_REFERENCE :
  1327. begin
  1328. { If the parameter location is reused we don't need to copy
  1329. anything }
  1330. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1331. begin
  1332. href:=currpara.localloc.reference;
  1333. sizeleft:=currpara.paraloc[calleeside].intsize;
  1334. while assigned(paraloc) do
  1335. begin
  1336. unget_para(paraloc^);
  1337. if (paraloc^.size=OS_NO) then
  1338. begin
  1339. { Can only be a reference that contains the rest
  1340. of the parameter }
  1341. if (paraloc^.loc<>LOC_REFERENCE) or
  1342. assigned(paraloc^.next) then
  1343. internalerror(2005013010);
  1344. gen_load_ref(paraloc^,href,sizeleft);
  1345. inc(href.offset,sizeleft);
  1346. sizeleft:=0;
  1347. end
  1348. else
  1349. begin
  1350. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1351. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1352. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1353. end;
  1354. paraloc:=paraloc^.next;
  1355. end;
  1356. end;
  1357. end;
  1358. LOC_CREGISTER :
  1359. begin
  1360. {$ifndef cpu64bit}
  1361. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1362. is_64bit(currpara.vartype.def) then
  1363. begin
  1364. case paraloc^.loc of
  1365. LOC_REGISTER:
  1366. begin
  1367. if not assigned(paraloc^.next) then
  1368. internalerror(200410104);
  1369. if (target_info.endian=ENDIAN_BIG) then
  1370. begin
  1371. { paraloc^ -> high
  1372. paraloc^.next -> low }
  1373. unget_para(paraloc^);
  1374. gen_alloc_regvar(list,currpara);
  1375. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1376. unget_para(paraloc^.next^);
  1377. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1378. end
  1379. else
  1380. begin
  1381. { paraloc^ -> low
  1382. paraloc^.next -> high }
  1383. unget_para(paraloc^);
  1384. gen_alloc_regvar(list,currpara);
  1385. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1386. unget_para(paraloc^.next^);
  1387. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1388. end;
  1389. end;
  1390. LOC_REFERENCE:
  1391. begin
  1392. gen_alloc_regvar(list,currpara);
  1393. reference_reset_base(href,paraloc^.reference.index,paraloc^.reference.offset);
  1394. cg64.a_load64_ref_reg(list,href,currpara.localloc.register64);
  1395. unget_para(paraloc^);
  1396. end;
  1397. else
  1398. internalerror(2005101501);
  1399. end
  1400. end
  1401. else
  1402. {$endif cpu64bit}
  1403. begin
  1404. if assigned(paraloc^.next) then
  1405. internalerror(200410105);
  1406. unget_para(paraloc^);
  1407. gen_alloc_regvar(list,currpara);
  1408. gen_load_reg(paraloc^,currpara.localloc.register);
  1409. end;
  1410. end;
  1411. LOC_CFPUREGISTER :
  1412. begin
  1413. {$ifdef sparc}
  1414. { Sparc passes floats in int registers, when loading to fpu register
  1415. we need a temp }
  1416. sizeleft := TCGSize2Size[currpara.localloc.size];
  1417. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1418. href:=tempref;
  1419. while assigned(paraloc) do
  1420. begin
  1421. unget_para(paraloc^);
  1422. gen_load_ref(paraloc^,href,sizeleft);
  1423. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1424. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1425. paraloc:=paraloc^.next;
  1426. end;
  1427. gen_alloc_regvar(list,currpara);
  1428. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1429. tg.UnGetTemp(list,tempref);
  1430. {$else sparc}
  1431. unget_para(paraloc^);
  1432. gen_alloc_regvar(list,currpara);
  1433. gen_load_reg(paraloc^,currpara.localloc.register);
  1434. if assigned(paraloc^.next) then
  1435. internalerror(200410109);
  1436. {$endif sparc}
  1437. end;
  1438. LOC_CMMREGISTER :
  1439. begin
  1440. unget_para(paraloc^);
  1441. gen_alloc_regvar(list,currpara);
  1442. gen_load_reg(paraloc^,currpara.localloc.register);
  1443. { data could come in two memory locations, for now
  1444. we simply ignore the sanity check (FK)
  1445. if assigned(paraloc^.next) then
  1446. internalerror(200410108);
  1447. }
  1448. end;
  1449. end;
  1450. end;
  1451. { generate copies of call by value parameters, must be done before
  1452. the initialization and body is parsed because the refcounts are
  1453. incremented using the local copies }
  1454. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1455. {$ifdef powerpc}
  1456. { unget the register that contains the stack pointer before the procedure entry, }
  1457. { which is used to access the parameters in their original callee-side location }
  1458. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1459. cg.a_reg_dealloc(list,NR_R12);
  1460. {$endif powerpc}
  1461. {$ifdef powerpc64}
  1462. { unget the register that contains the stack pointer before the procedure entry, }
  1463. { which is used to access the parameters in their original callee-side location }
  1464. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1465. cg.a_reg_dealloc(list, NR_OLD_STACK_POINTER_REG);
  1466. {$endif powerpc64}
  1467. end;
  1468. procedure gen_initialize_code(list:TAAsmoutput);
  1469. begin
  1470. { initialize local data like ansistrings }
  1471. case current_procinfo.procdef.proctypeoption of
  1472. potype_unitinit:
  1473. begin
  1474. { this is also used for initialization of variables in a
  1475. program which does not have a globalsymtable }
  1476. if assigned(current_module.globalsymtable) then
  1477. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1478. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1479. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1480. end;
  1481. { units have seperate code for initilization and finalization }
  1482. potype_unitfinalize: ;
  1483. { program init/final is generated in separate procedure }
  1484. potype_proginit:
  1485. begin
  1486. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1487. end;
  1488. else
  1489. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1490. end;
  1491. { initialisizes temp. ansi/wide string data }
  1492. inittempvariables(list);
  1493. { initialize ansi/widesstring para's }
  1494. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1495. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1496. {$ifdef OLDREGVARS}
  1497. load_regvars(list,nil);
  1498. {$endif OLDREGVARS}
  1499. end;
  1500. procedure gen_finalize_code(list:TAAsmoutput);
  1501. begin
  1502. {$ifdef OLDREGVARS}
  1503. cleanup_regvars(list);
  1504. {$endif OLDREGVARS}
  1505. { finalize temporary data }
  1506. finalizetempvariables(list);
  1507. { finalize local data like ansistrings}
  1508. case current_procinfo.procdef.proctypeoption of
  1509. potype_unitfinalize:
  1510. begin
  1511. { this is also used for initialization of variables in a
  1512. program which does not have a globalsymtable }
  1513. if assigned(current_module.globalsymtable) then
  1514. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1515. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1516. end;
  1517. { units/progs have separate code for initialization and finalization }
  1518. potype_unitinit: ;
  1519. { program init/final is generated in separate procedure }
  1520. potype_proginit: ;
  1521. else
  1522. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1523. end;
  1524. { finalize paras data }
  1525. if assigned(current_procinfo.procdef.parast) and
  1526. not(po_assembler in current_procinfo.procdef.procoptions) then
  1527. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1528. end;
  1529. procedure gen_entry_code(list:TAAsmoutput);
  1530. var
  1531. paraloc1,
  1532. paraloc2 : tcgpara;
  1533. begin
  1534. paraloc1.init;
  1535. paraloc2.init;
  1536. { the actual profile code can clobber some registers,
  1537. therefore if the context must be saved, do it before
  1538. the actual call to the profile code
  1539. }
  1540. if (cs_profile in aktmoduleswitches) and
  1541. not(po_assembler in current_procinfo.procdef.procoptions) then
  1542. begin
  1543. { non-win32 can call mcout even in main }
  1544. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1545. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1546. begin
  1547. cg.allocallcpuregisters(list);
  1548. cg.g_profilecode(list);
  1549. cg.deallocallcpuregisters(list);
  1550. end;
  1551. end;
  1552. { call startup helpers from main program }
  1553. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1554. begin
  1555. if (target_info.system = system_powerpc_darwin) or
  1556. (target_info.system = system_powerpc_macos) then
  1557. begin
  1558. { the parameters are already in the right registers }
  1559. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1560. end;
  1561. { initialize units }
  1562. cg.allocallcpuregisters(list);
  1563. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1564. cg.deallocallcpuregisters(list);
  1565. end;
  1566. list.concat(Tai_force_line.Create);
  1567. {$ifdef OLDREGVARS}
  1568. load_regvars(list,nil);
  1569. {$endif OLDREGVARS}
  1570. paraloc1.done;
  1571. paraloc2.done;
  1572. end;
  1573. procedure gen_exit_code(list:TAAsmoutput);
  1574. begin
  1575. { call __EXIT for main program }
  1576. if (not DLLsource) and
  1577. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1578. cg.a_call_name(list,'FPC_DO_EXIT');
  1579. end;
  1580. {****************************************************************************
  1581. Entry/Exit
  1582. ****************************************************************************}
  1583. procedure alloc_proc_symbol(pd: tprocdef);
  1584. var
  1585. item: tstringlistitem;
  1586. begin
  1587. item := tstringlistitem(pd.aliasnames.first);
  1588. while assigned(item) do
  1589. begin
  1590. if (cs_profile in aktmoduleswitches) or
  1591. (po_global in current_procinfo.procdef.procoptions) then
  1592. objectlibrary.newasmsymbol(item.str,AB_GLOBAL,AT_FUNCTION)
  1593. else
  1594. objectlibrary.newasmsymbol(item.str,AB_GLOBAL,AT_FUNCTION);
  1595. item := tstringlistitem(item.next);
  1596. end;
  1597. end;
  1598. procedure gen_proc_symbol(list:Taasmoutput);
  1599. var
  1600. hs : string;
  1601. begin
  1602. repeat
  1603. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1604. if hs='' then
  1605. break;
  1606. if (cs_profile in aktmoduleswitches) or
  1607. (po_global in current_procinfo.procdef.procoptions) then
  1608. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1609. else
  1610. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1611. if target_info.use_function_relative_addresses then
  1612. list.concat(Tai_function_name.create(hs));
  1613. until false;
  1614. current_procinfo.procdef.procstarttai:=tai(list.last);
  1615. end;
  1616. procedure gen_proc_symbol_end(list:Taasmoutput);
  1617. begin
  1618. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1619. begin
  1620. { Insert Ident of the compiler in the main .text section }
  1621. if (not (cs_create_smart in aktmoduleswitches)) then
  1622. begin
  1623. list.insert(Tai_align.Create(const_align(32)));
  1624. list.insert(Tai_string.Create('FPC '+full_version_string+
  1625. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  1626. end;
  1627. { Reference all DEBUGINFO sections from the main .text section }
  1628. if (cs_debuginfo in aktmoduleswitches) then
  1629. debuginfo.referencesections(list);
  1630. end;
  1631. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1632. current_procinfo.procdef.procendtai:=tai(list.last);
  1633. end;
  1634. procedure gen_proc_entry_code(list:Taasmoutput);
  1635. var
  1636. hitemp,
  1637. lotemp : longint;
  1638. begin
  1639. { generate call frame marker for dwarf call frame info }
  1640. dwarfcfi.start_frame(list);
  1641. { All temps are know, write offsets used for information }
  1642. if (cs_asm_source in aktglobalswitches) then
  1643. begin
  1644. if tg.direction>0 then
  1645. begin
  1646. lotemp:=current_procinfo.tempstart;
  1647. hitemp:=tg.lasttemp;
  1648. end
  1649. else
  1650. begin
  1651. lotemp:=tg.lasttemp;
  1652. hitemp:=current_procinfo.tempstart;
  1653. end;
  1654. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1655. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1656. end;
  1657. { generate target specific proc entry code }
  1658. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1659. end;
  1660. procedure gen_proc_exit_code(list:Taasmoutput);
  1661. var
  1662. parasize : longint;
  1663. begin
  1664. { c style clearstack does not need to remove parameters from the stack, only the
  1665. return value when it was pushed by arguments }
  1666. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1667. begin
  1668. parasize:=0;
  1669. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1670. inc(parasize,sizeof(aint));
  1671. end
  1672. else
  1673. parasize:=current_procinfo.para_stack_size;
  1674. { generate target specific proc exit code }
  1675. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1676. { release return registers, needed for optimizer }
  1677. if not is_void(current_procinfo.procdef.rettype.def) then
  1678. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1679. { end of frame marker for call frame info }
  1680. dwarfcfi.end_frame(list);
  1681. end;
  1682. procedure gen_stack_check_size_para(list:Taasmoutput);
  1683. var
  1684. paraloc1 : tcgpara;
  1685. begin
  1686. paraloc1.init;
  1687. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1688. paramanager.allocparaloc(list,paraloc1);
  1689. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1690. paramanager.freeparaloc(list,paraloc1);
  1691. paraloc1.done;
  1692. end;
  1693. procedure gen_stack_check_call(list:Taasmoutput);
  1694. var
  1695. paraloc1 : tcgpara;
  1696. begin
  1697. paraloc1.init;
  1698. { Also alloc the register needed for the parameter }
  1699. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1700. paramanager.allocparaloc(list,paraloc1);
  1701. paramanager.freeparaloc(list,paraloc1);
  1702. { Call the helper }
  1703. cg.allocallcpuregisters(list);
  1704. cg.a_call_name(list,'FPC_STACKCHECK');
  1705. cg.deallocallcpuregisters(list);
  1706. paraloc1.done;
  1707. end;
  1708. procedure gen_save_used_regs(list:TAAsmoutput);
  1709. begin
  1710. { Pure assembler routines need to save the registers themselves }
  1711. if (po_assembler in current_procinfo.procdef.procoptions) then
  1712. exit;
  1713. { oldfpccall expects all registers to be destroyed }
  1714. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1715. cg.g_save_standard_registers(list);
  1716. end;
  1717. procedure gen_restore_used_regs(list:TAAsmoutput);
  1718. begin
  1719. { Pure assembler routines need to save the registers themselves }
  1720. if (po_assembler in current_procinfo.procdef.procoptions) then
  1721. exit;
  1722. { oldfpccall expects all registers to be destroyed }
  1723. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1724. cg.g_restore_standard_registers(list);
  1725. end;
  1726. procedure gen_got_load(list : taasmoutput);
  1727. begin
  1728. { if loading got is necessary for more cpus, it can be moved
  1729. to the cg }
  1730. {$ifdef i386}
  1731. { allocate PIC register }
  1732. if (cs_create_pic in aktmoduleswitches) and
  1733. (tf_pic_uses_got in target_info.flags) and
  1734. (pi_needs_got in current_procinfo.flags) then
  1735. begin
  1736. current_module.requires_ebx_pic_helper:=true;
  1737. cg.a_call_name_static(list,'fpc_geteipasebx');
  1738. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_L,objectlibrary.newasmsymbol('_GLOBAL_OFFSET_TABLE_',AB_EXTERNAL,AT_DATA),0,NR_PIC_OFFSET_REG));
  1739. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1740. { ecx could be used in leave procedures }
  1741. current_procinfo.got:=NR_EBX;
  1742. end;
  1743. {$endif i386}
  1744. end;
  1745. {****************************************************************************
  1746. External handling
  1747. ****************************************************************************}
  1748. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1749. begin
  1750. { add the procedure to the al_procedures }
  1751. maybe_new_object_file(list);
  1752. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1753. list.concat(Tai_align.create(aktalignment.procalign));
  1754. if (po_global in pd.procoptions) then
  1755. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1756. else
  1757. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1758. cg.a_jmp_name(list,externalname);
  1759. end;
  1760. {****************************************************************************
  1761. Const Data
  1762. ****************************************************************************}
  1763. procedure insertbssdata(sym : tglobalvarsym);
  1764. var
  1765. l,varalign : longint;
  1766. storefilepos : tfileposinfo;
  1767. list : Taasmoutput;
  1768. sectype : Tasmsectiontype;
  1769. begin
  1770. storefilepos:=aktfilepos;
  1771. aktfilepos:=sym.fileinfo;
  1772. l:=sym.getsize;
  1773. if tf_section_threadvars in target_info.flags then
  1774. begin
  1775. if (vo_is_thread_var in sym.varoptions) then
  1776. begin
  1777. list:=asmlist[al_threadvars];
  1778. sectype:=sec_threadvar;
  1779. end
  1780. else
  1781. begin
  1782. list:=asmlist[al_globals];
  1783. sectype:=sec_bss;
  1784. end;
  1785. end
  1786. else
  1787. begin
  1788. if (vo_is_thread_var in sym.varoptions) then
  1789. inc(l,sizeof(aint));
  1790. list:=asmlist[al_globals];
  1791. sectype:=sec_bss;
  1792. end;
  1793. varalign:=var_align(l);
  1794. maybe_new_object_file(list);
  1795. new_section(list,sectype,lower(sym.mangledname),varalign);
  1796. if (sym.owner.symtabletype=globalsymtable) or
  1797. maybe_smartlink_symbol or
  1798. DLLSource or
  1799. (assigned(current_procinfo) and
  1800. (po_inline in current_procinfo.procdef.procoptions)) or
  1801. (vo_is_exported in sym.varoptions) or
  1802. (vo_is_C_var in sym.varoptions) then
  1803. list.concat(Tai_datablock.create_global(sym.mangledname,l))
  1804. else
  1805. list.concat(Tai_datablock.create(sym.mangledname,l));
  1806. aktfilepos:=storefilepos;
  1807. end;
  1808. procedure init_regvar_loc(sym:tabstractnormalvarsym;cgsize:tcgsize);
  1809. begin
  1810. with sym do
  1811. begin
  1812. localloc.size:=cgsize;
  1813. case varregable of
  1814. vr_intreg :
  1815. begin
  1816. localloc.loc:=LOC_CREGISTER;
  1817. end;
  1818. vr_fpureg :
  1819. begin
  1820. localloc.loc:=LOC_CFPUREGISTER;
  1821. end;
  1822. vr_mmreg :
  1823. begin
  1824. localloc.loc:=LOC_CMMREGISTER;
  1825. end;
  1826. else
  1827. internalerror(2004101010);
  1828. end;
  1829. end;
  1830. end;
  1831. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1832. var
  1833. sym : tsym;
  1834. isaddr : boolean;
  1835. cgsize : tcgsize;
  1836. begin
  1837. sym:=tsym(st.symindex.first);
  1838. while assigned(sym) do
  1839. begin
  1840. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1841. begin
  1842. with tabstractnormalvarsym(sym) do
  1843. begin
  1844. { Parameters passed to assembler procedures need to be kept
  1845. in the original location }
  1846. if (sym.typ=paravarsym) and
  1847. (po_assembler in current_procinfo.procdef.procoptions) then
  1848. begin
  1849. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1850. end
  1851. else
  1852. begin
  1853. isaddr:=(st.symtabletype=parasymtable) and
  1854. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1855. if isaddr then
  1856. cgsize:=OS_ADDR
  1857. else
  1858. cgsize:=def_cgsize(vartype.def);
  1859. {$ifndef OLDREGVARS}
  1860. { When there is assembler code we can't use regvars }
  1861. if is_regvar then
  1862. begin
  1863. init_regvar_loc(tabstractnormalvarsym(sym),cgsize);
  1864. if (st.symtabletype <> parasymtable) then
  1865. gen_alloc_regvar(list,tabstractnormalvarsym(sym));
  1866. end
  1867. else
  1868. {$endif NOT OLDREGVARS}
  1869. begin
  1870. localloc.loc:=LOC_REFERENCE;
  1871. localloc.size:=cgsize;
  1872. case st.symtabletype of
  1873. parasymtable :
  1874. begin
  1875. { Reuse the parameter location for values to are at a single location on the stack }
  1876. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1877. begin
  1878. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1879. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1880. end
  1881. else
  1882. begin
  1883. if isaddr then
  1884. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1885. else
  1886. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1887. end;
  1888. end;
  1889. localsymtable,
  1890. stt_exceptsymtable :
  1891. begin
  1892. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1893. end;
  1894. staticsymtable :
  1895. begin
  1896. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1897. if not(vo_is_dll_var in varoptions) and ((tf_section_threadvars in target_info.flags) or
  1898. not(vo_is_thread_var in varoptions)) then
  1899. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1900. end;
  1901. else
  1902. internalerror(200410103);
  1903. end;
  1904. end;
  1905. end;
  1906. if cs_asm_source in aktglobalswitches then
  1907. begin
  1908. case localloc.loc of
  1909. LOC_REFERENCE :
  1910. begin
  1911. if not assigned(localloc.reference.symbol) then
  1912. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1913. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1914. end;
  1915. end;
  1916. end;
  1917. end;
  1918. end;
  1919. sym:=tsym(sym.indexnext);
  1920. end;
  1921. end;
  1922. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  1923. var
  1924. sym : tsym;
  1925. begin
  1926. sym:=tsym(st.symindex.first);
  1927. while assigned(sym) do
  1928. begin
  1929. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1930. begin
  1931. with tabstractnormalvarsym(sym) do
  1932. begin
  1933. { Note: We need to keep the data available in memory
  1934. for the sub procedures that can access local data
  1935. in the parent procedures }
  1936. case localloc.loc of
  1937. LOC_CREGISTER :
  1938. {$ifndef cpu64bit}
  1939. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  1940. begin
  1941. cg.a_reg_sync(list,localloc.register64.reglo);
  1942. cg.a_reg_sync(list,localloc.register64.reghi);
  1943. end
  1944. else
  1945. {$endif cpu64bit}
  1946. cg.a_reg_sync(list,localloc.register);
  1947. LOC_CFPUREGISTER,
  1948. LOC_CMMREGISTER:
  1949. cg.a_reg_sync(list,localloc.register);
  1950. LOC_REFERENCE :
  1951. begin
  1952. case st.symtabletype of
  1953. localsymtable,
  1954. parasymtable,
  1955. stt_exceptsymtable :
  1956. tg.Ungetlocal(list,localloc.reference);
  1957. end;
  1958. end;
  1959. end;
  1960. end;
  1961. end;
  1962. sym:=tsym(sym.indexnext);
  1963. end;
  1964. end;
  1965. { persistent rtti generation }
  1966. procedure generate_rtti(p:Ttypesym);
  1967. var
  1968. rsym : trttisym;
  1969. def : tstoreddef;
  1970. begin
  1971. { rtti can only be generated for classes that are always typesyms }
  1972. def:=tstoreddef(ttypesym(p).restype.def);
  1973. { there is an error, skip rtti info }
  1974. if (def.deftype=errordef) or (Errorcount>0) then
  1975. exit;
  1976. { only create rtti once for each definition }
  1977. if not(df_has_rttitable in def.defoptions) then
  1978. begin
  1979. { definition should be in the same symtable as the symbol }
  1980. if p.owner<>def.owner then
  1981. internalerror(200108262);
  1982. { create rttisym }
  1983. rsym:=trttisym.create(p.name,fullrtti);
  1984. p.owner.insert(rsym);
  1985. { register rttisym in definition }
  1986. include(def.defoptions,df_has_rttitable);
  1987. def.rttitablesym:=rsym;
  1988. { write rtti data }
  1989. def.write_child_rtti_data(fullrtti);
  1990. maybe_new_object_file(asmlist[al_rtti]);
  1991. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  1992. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  1993. def.write_rtti_data(fullrtti);
  1994. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  1995. end;
  1996. end;
  1997. { persistent init table generation }
  1998. procedure generate_inittable(p:tsym);
  1999. var
  2000. rsym : trttisym;
  2001. def : tstoreddef;
  2002. begin
  2003. { anonymous types are also allowed for records that can be varsym }
  2004. case p.typ of
  2005. typesym :
  2006. def:=tstoreddef(ttypesym(p).restype.def);
  2007. globalvarsym,
  2008. localvarsym,
  2009. paravarsym :
  2010. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2011. else
  2012. internalerror(200108263);
  2013. end;
  2014. { only create inittable once for each definition }
  2015. if not(df_has_inittable in def.defoptions) then
  2016. begin
  2017. { definition should be in the same symtable as the symbol }
  2018. if p.owner<>def.owner then
  2019. internalerror(200108264);
  2020. { create rttisym }
  2021. rsym:=trttisym.create(p.name,initrtti);
  2022. p.owner.insert(rsym);
  2023. { register rttisym in definition }
  2024. include(def.defoptions,df_has_inittable);
  2025. def.inittablesym:=rsym;
  2026. { write inittable data }
  2027. def.write_child_rtti_data(initrtti);
  2028. maybe_new_object_file(asmlist[al_rtti]);
  2029. new_section(asmlist[al_rtti],sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2030. asmlist[al_rtti].concat(Tai_symbol.Create_global(rsym.get_label,0));
  2031. def.write_rtti_data(initrtti);
  2032. asmlist[al_rtti].concat(Tai_symbol_end.Create(rsym.get_label));
  2033. end;
  2034. end;
  2035. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  2036. var
  2037. i,j,
  2038. proccount : longint;
  2039. tmps : string;
  2040. begin
  2041. for i:=1 to _class.implementedinterfaces.count do
  2042. begin
  2043. { only if implemented by this class }
  2044. if _class.implementedinterfaces.implindex(i)=i then
  2045. begin
  2046. proccount:=_class.implementedinterfaces.implproccount(i);
  2047. for j:=1 to proccount do
  2048. begin
  2049. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2050. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2051. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2052. { create wrapper code }
  2053. new_section(list,sec_code,lower(tmps),0);
  2054. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2055. end;
  2056. end;
  2057. end;
  2058. end;
  2059. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  2060. var
  2061. def : tstoreddef;
  2062. begin
  2063. def:=tstoreddef(st.defindex.first);
  2064. while assigned(def) do
  2065. begin
  2066. if is_class(def) then
  2067. gen_intf_wrapper(list,tobjectdef(def));
  2068. def:=tstoreddef(def.indexnext);
  2069. end;
  2070. end;
  2071. procedure gen_load_vmt_register(list:taasmoutput;objdef:tobjectdef;selfloc:tlocation;var vmtreg:tregister);
  2072. var
  2073. href : treference;
  2074. begin
  2075. if is_object(objdef) then
  2076. begin
  2077. case selfloc.loc of
  2078. LOC_CREFERENCE,
  2079. LOC_REFERENCE:
  2080. begin
  2081. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2082. cg.a_loadaddr_ref_reg(list,selfloc.reference,href.base);
  2083. end;
  2084. else
  2085. internalerror(200305056);
  2086. end;
  2087. end
  2088. else
  2089. begin
  2090. case selfloc.loc of
  2091. LOC_REGISTER:
  2092. begin
  2093. {$ifdef cpu_uses_separate_address_registers}
  2094. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  2095. begin
  2096. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2097. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,selfloc.register,href.base);
  2098. end
  2099. else
  2100. {$endif cpu_uses_separate_address_registers}
  2101. reference_reset_base(href,selfloc.register,objdef.vmt_offset);
  2102. end;
  2103. LOC_CREGISTER,
  2104. LOC_CREFERENCE,
  2105. LOC_REFERENCE:
  2106. begin
  2107. reference_reset_base(href,cg.getaddressregister(list),objdef.vmt_offset);
  2108. cg.a_load_loc_reg(list,OS_ADDR,selfloc,href.base);
  2109. end;
  2110. else
  2111. internalerror(200305057);
  2112. end;
  2113. end;
  2114. vmtreg:=cg.getaddressregister(list);
  2115. cg.g_maybe_testself(list,href.base);
  2116. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,vmtreg);
  2117. cg.g_maybe_testvmt(list,vmtreg,objdef);
  2118. end;
  2119. function getprocalign : longint;
  2120. begin
  2121. { gprof uses 16 byte granularity }
  2122. if (cs_profile in aktmoduleswitches) then
  2123. result:=16
  2124. else
  2125. result:=aktalignment.procalign;
  2126. end;
  2127. procedure gen_pic_helpers(list : taasmoutput);
  2128. var
  2129. href : treference;
  2130. begin
  2131. { if other cpus require such helpers as well, it can be solved more cleaner }
  2132. {$ifdef i386}
  2133. if current_module.requires_ebx_pic_helper then
  2134. begin
  2135. new_section(list,sec_code,'fpc_geteipasebx',0);
  2136. list.concat(tai_symbol.Createname('fpc_geteipasebx',AT_FUNCTION,getprocalign));
  2137. reference_reset(href);
  2138. href.base:=NR_ESP;
  2139. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_EBX));
  2140. list.concat(taicpu.op_none(A_RET,S_NO));
  2141. end;
  2142. if current_module.requires_ecx_pic_helper then
  2143. begin
  2144. new_section(list,sec_code,'fpc_geteipasecx',0);
  2145. list.concat(tai_symbol.Createname('fpc_geteipasecx',AT_FUNCTION,getprocalign));
  2146. reference_reset(href);
  2147. href.base:=NR_ESP;
  2148. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,NR_ECX));
  2149. list.concat(taicpu.op_none(A_RET,S_NO));
  2150. end;
  2151. {$endif i386}
  2152. end;
  2153. end.