ncgutil.pas 86 KB

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