ncgutil.pas 96 KB

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