ncgutil.pas 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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. {#
  60. Allocate the buffers for exception management and setjmp environment.
  61. Return a pointer to these buffers, send them to the utility routine
  62. so they are registered, and then call setjmp.
  63. Then compare the result of setjmp with 0, and if not equal
  64. to zero, then jump to exceptlabel.
  65. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  66. It is to note that this routine may be called *after* the stackframe of a
  67. routine has been called, therefore on machines where the stack cannot
  68. be modified, all temps should be allocated on the heap instead of the
  69. stack.
  70. }
  71. const
  72. EXCEPT_BUF_SIZE = 3*sizeof(aint);
  73. type
  74. texceptiontemps=record
  75. jmpbuf,
  76. envbuf,
  77. reasonbuf : treference;
  78. end;
  79. procedure get_exception_temps(list:taasmoutput;var t:texceptiontemps);
  80. procedure unget_exception_temps(list:taasmoutput;const t:texceptiontemps);
  81. procedure new_exception(list:TAAsmoutput;const t:texceptiontemps;exceptlabel:tasmlabel);
  82. procedure free_exception(list:TAAsmoutput;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  83. procedure insertconstdata(sym : ttypedconstsym);
  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.allocallcpuregisters(list);
  314. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  315. cg.deallocallcpuregisters(list);
  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.allocallcpuregisters(list);
  321. cg.a_call_name(list,'FPC_SETJMP');
  322. cg.deallocallcpuregisters(list);
  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.allocallcpuregisters(list);
  332. cg.a_call_name(list,'FPC_POPADDRSTACK');
  333. cg.deallocallcpuregisters(list);
  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. LOC_CMMREGISTER :
  766. { clear the whole register }
  767. cg.a_opmm_reg_reg(taasmoutput(arg),OP_XOR,reg_cgsize(tglobalvarsym(p).localloc.register),
  768. tglobalvarsym(p).localloc.register,
  769. tglobalvarsym(p).localloc.register,
  770. nil);
  771. LOC_CFPUREGISTER :
  772. ;
  773. else
  774. internalerror(200410124);
  775. end;
  776. end;
  777. end;
  778. { generates the code for initialisation of local data }
  779. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  780. var
  781. oldexprasmlist : TAAsmoutput;
  782. hp : tnode;
  783. begin
  784. if (tsym(p).typ in [globalvarsym,localvarsym]) and
  785. (tabstractvarsym(p).refs>0) and
  786. not(is_class(tabstractvarsym(p).vartype.def)) and
  787. tabstractvarsym(p).vartype.def.needs_inittable then
  788. begin
  789. oldexprasmlist:=exprasmlist;
  790. exprasmlist:=taasmoutput(arg);
  791. hp:=initialize_data_node(cloadnode.create(tsym(p),tsym(p).owner));
  792. firstpass(hp);
  793. secondpass(hp);
  794. hp.free;
  795. exprasmlist:=oldexprasmlist;
  796. end;
  797. end;
  798. procedure finalize_sym(asmlist:taasmoutput;sym:tsym);
  799. var
  800. hp : tnode;
  801. oldexprasmlist : TAAsmoutput;
  802. begin
  803. include(current_procinfo.flags,pi_needs_implicit_finally);
  804. oldexprasmlist:=exprasmlist;
  805. exprasmlist:=asmlist;
  806. hp:=finalize_data_node(cloadnode.create(sym,sym.owner));
  807. firstpass(hp);
  808. secondpass(hp);
  809. hp.free;
  810. exprasmlist:=oldexprasmlist;
  811. end;
  812. { generates the code for finalisation of local variables }
  813. procedure finalize_local_vars(p : tnamedindexitem;arg:pointer);
  814. begin
  815. if (tsym(p).typ=localvarsym) and
  816. (tlocalvarsym(p).refs>0) and
  817. not(vo_is_funcret in tlocalvarsym(p).varoptions) and
  818. not(is_class(tlocalvarsym(p).vartype.def)) and
  819. tlocalvarsym(p).vartype.def.needs_inittable then
  820. finalize_sym(taasmoutput(arg),tsym(p));
  821. end;
  822. { generates the code for finalisation of local typedconsts }
  823. procedure finalize_local_typedconst(p : tnamedindexitem;arg:pointer);
  824. var
  825. i : longint;
  826. pd : tprocdef;
  827. begin
  828. case tsym(p).typ of
  829. typedconstsym :
  830. begin
  831. if ttypedconstsym(p).is_writable and
  832. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  833. finalize_sym(taasmoutput(arg),tsym(p));
  834. end;
  835. procsym :
  836. begin
  837. for i:=1 to tprocsym(p).procdef_count do
  838. begin
  839. pd:=tprocsym(p).procdef[i];
  840. if assigned(pd.localst) and
  841. (pd.procsym=tprocsym(p)) and
  842. (pd.localst.symtabletype<>staticsymtable) then
  843. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  844. end;
  845. end;
  846. end;
  847. end;
  848. { generates the code for finalization of static symtable and
  849. all local (static) typedconsts }
  850. procedure finalize_static_data(p : tnamedindexitem;arg:pointer);
  851. var
  852. i : longint;
  853. pd : tprocdef;
  854. begin
  855. case tsym(p).typ of
  856. globalvarsym :
  857. begin
  858. if (tglobalvarsym(p).refs>0) and
  859. not(vo_is_funcret in tglobalvarsym(p).varoptions) and
  860. not(is_class(tglobalvarsym(p).vartype.def)) and
  861. tglobalvarsym(p).vartype.def.needs_inittable then
  862. finalize_sym(taasmoutput(arg),tsym(p));
  863. end;
  864. typedconstsym :
  865. begin
  866. if ttypedconstsym(p).is_writable and
  867. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  868. finalize_sym(taasmoutput(arg),tsym(p));
  869. end;
  870. procsym :
  871. begin
  872. for i:=1 to tprocsym(p).procdef_count do
  873. begin
  874. pd:=tprocsym(p).procdef[i];
  875. if assigned(pd.localst) and
  876. (pd.procsym=tprocsym(p)) and
  877. (pd.localst.symtabletype<>staticsymtable) then
  878. pd.localst.foreach_static(@finalize_local_typedconst,arg);
  879. end;
  880. end;
  881. end;
  882. end;
  883. { generates the code for incrementing the reference count of parameters and
  884. initialize out parameters }
  885. procedure init_paras(p : tnamedindexitem;arg:pointer);
  886. var
  887. href : treference;
  888. tmpreg : tregister;
  889. list : TAAsmoutput;
  890. begin
  891. list:=taasmoutput(arg);
  892. if (tsym(p).typ=paravarsym) and
  893. not is_class_or_interface(tparavarsym(p).vartype.def) and
  894. tparavarsym(p).vartype.def.needs_inittable then
  895. begin
  896. case tparavarsym(p).varspez of
  897. vs_value :
  898. begin
  899. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  900. cg.g_incrrefcount(list,tparavarsym(p).vartype.def,href);
  901. end;
  902. vs_out :
  903. begin
  904. tmpreg:=cg.getaddressregister(list);
  905. cg.a_load_loc_reg(list,OS_ADDR,tparavarsym(p).localloc,tmpreg);
  906. reference_reset_base(href,tmpreg,0);
  907. cg.g_initialize(list,tparavarsym(p).vartype.def,href);
  908. end;
  909. end;
  910. end;
  911. end;
  912. { generates the code for decrementing the reference count of parameters }
  913. procedure final_paras(p : tnamedindexitem;arg:pointer);
  914. var
  915. list : TAAsmoutput;
  916. href : treference;
  917. begin
  918. if not(tsym(p).typ=paravarsym) then
  919. exit;
  920. list:=taasmoutput(arg);
  921. if not is_class_or_interface(tparavarsym(p).vartype.def) and
  922. tparavarsym(p).vartype.def.needs_inittable then
  923. begin
  924. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  925. if (tparavarsym(p).varspez=vs_value) then
  926. begin
  927. include(current_procinfo.flags,pi_needs_implicit_finally);
  928. location_get_data_ref(list,tparavarsym(p).localloc,href,is_open_array(tparavarsym(p).vartype.def));
  929. cg.g_decrrefcount(list,tparavarsym(p).vartype.def,href);
  930. end;
  931. end
  932. else
  933. if (tparavarsym(p).varspez=vs_value) and
  934. (is_open_array(tparavarsym(p).vartype.def) or
  935. is_array_of_const(tparavarsym(p).vartype.def)) then
  936. begin
  937. { cdecl functions don't have a high pointer so it is not possible to generate
  938. a local copy }
  939. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  940. cg.g_releasevaluepara_openarray(list,tparavarsym(p).localloc);
  941. end;
  942. end;
  943. { Initialize temp ansi/widestrings,interfaces }
  944. procedure inittempvariables(list:taasmoutput);
  945. var
  946. hp : ptemprecord;
  947. href : treference;
  948. begin
  949. hp:=tg.templist;
  950. while assigned(hp) do
  951. begin
  952. if assigned(hp^.def) and
  953. hp^.def.needs_inittable then
  954. begin
  955. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  956. cg.g_initialize(list,hp^.def,href);
  957. end;
  958. hp:=hp^.next;
  959. end;
  960. end;
  961. procedure finalizetempvariables(list:taasmoutput);
  962. var
  963. hp : ptemprecord;
  964. href : treference;
  965. begin
  966. hp:=tg.templist;
  967. while assigned(hp) do
  968. begin
  969. if assigned(hp^.def) and
  970. hp^.def.needs_inittable then
  971. begin
  972. include(current_procinfo.flags,pi_needs_implicit_finally);
  973. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  974. cg.g_finalize(list,hp^.def,href);
  975. end;
  976. hp:=hp^.next;
  977. end;
  978. end;
  979. procedure gen_load_return_value(list:TAAsmoutput);
  980. var
  981. {$ifndef cpu64bit}
  982. href : treference;
  983. {$endif cpu64bit}
  984. ressym : tabstractnormalvarsym;
  985. resloc,
  986. restmploc : tlocation;
  987. hreg : tregister;
  988. funcretloc : tlocation;
  989. begin
  990. { Is the loading needed? }
  991. if (current_procinfo.procdef.funcretloc[calleeside].loc=LOC_VOID) or
  992. (
  993. (po_assembler in current_procinfo.procdef.procoptions) and
  994. (not(assigned(current_procinfo.procdef.funcretsym)) or
  995. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs=0))
  996. ) then
  997. exit;
  998. funcretloc:=current_procinfo.procdef.funcretloc[calleeside];
  999. { constructors return self }
  1000. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1001. ressym:=tabstractnormalvarsym(current_procinfo.procdef.parast.search('self'))
  1002. else
  1003. ressym:=tabstractnormalvarsym(current_procinfo.procdef.funcretsym);
  1004. if (ressym.refs>0) then
  1005. begin
  1006. {$ifdef OLDREGVARS}
  1007. case ressym.localloc.loc of
  1008. LOC_CFPUREGISTER,
  1009. LOC_FPUREGISTER:
  1010. begin
  1011. location_reset(restmploc,LOC_CFPUREGISTER,funcretloc^.size);
  1012. restmploc.register:=ressym.localloc.register;
  1013. end;
  1014. LOC_CREGISTER,
  1015. LOC_REGISTER:
  1016. begin
  1017. location_reset(restmploc,LOC_CREGISTER,funcretloc^.size);
  1018. restmploc.register:=ressym.localloc.register;
  1019. end;
  1020. LOC_MMREGISTER:
  1021. begin
  1022. location_reset(restmploc,LOC_CMMREGISTER,funcretloc^.size);
  1023. restmploc.register:=ressym.localloc.register;
  1024. end;
  1025. LOC_REFERENCE:
  1026. begin
  1027. location_reset(restmploc,LOC_REFERENCE,funcretloc^.size);
  1028. restmploc.reference:=ressym.localloc.reference;
  1029. end;
  1030. else
  1031. internalerror(200309184);
  1032. end;
  1033. {$else}
  1034. restmploc:=ressym.localloc;
  1035. {$endif}
  1036. { Here, we return the function result. In most architectures, the value is
  1037. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1038. function returns in a register and the caller receives it in an other one }
  1039. case funcretloc.loc of
  1040. LOC_REGISTER:
  1041. begin
  1042. {$ifndef cpu64bit}
  1043. if current_procinfo.procdef.funcretloc[calleeside].size in [OS_64,OS_S64] then
  1044. begin
  1045. resloc:=current_procinfo.procdef.funcretloc[calleeside];
  1046. if resloc.loc<>LOC_REGISTER then
  1047. internalerror(200409141);
  1048. { Load low and high register separate to generate better register
  1049. allocation info }
  1050. if getsupreg(resloc.register64.reglo)<first_int_imreg then
  1051. begin
  1052. cg.getcpuregister(list,resloc.register64.reglo);
  1053. cg.ungetcpuregister(list,resloc.register64.reglo);
  1054. { for the optimizer }
  1055. cg.a_reg_alloc(list,resloc.register64.reglo);
  1056. end;
  1057. case restmploc.loc of
  1058. LOC_REFERENCE :
  1059. begin
  1060. href:=restmploc.reference;
  1061. if target_info.endian=ENDIAN_BIG then
  1062. inc(href.offset,4);
  1063. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reglo);
  1064. end;
  1065. LOC_CREGISTER :
  1066. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reglo,resloc.register64.reglo);
  1067. else
  1068. internalerror(200409203);
  1069. end;
  1070. if getsupreg(resloc.register64.reghi)<first_int_imreg then
  1071. begin
  1072. cg.getcpuregister(list,resloc.register64.reghi);
  1073. cg.ungetcpuregister(list,resloc.register64.reghi);
  1074. { for the optimizer }
  1075. cg.a_reg_alloc(list,resloc.register64.reghi);
  1076. end;
  1077. case restmploc.loc of
  1078. LOC_REFERENCE :
  1079. begin
  1080. href:=restmploc.reference;
  1081. if target_info.endian=ENDIAN_LITTLE then
  1082. inc(href.offset,4);
  1083. cg.a_load_ref_reg(list,OS_32,OS_32,href,resloc.register64.reghi);
  1084. end;
  1085. LOC_CREGISTER :
  1086. cg.a_load_reg_reg(list,OS_32,OS_32,restmploc.register64.reghi,resloc.register64.reghi);
  1087. else
  1088. internalerror(200409204);
  1089. end;
  1090. end
  1091. else
  1092. {$endif cpu64bit}
  1093. begin
  1094. hreg:=cg.makeregsize(list,funcretloc.register,funcretloc.size);
  1095. if getsupreg(funcretloc.register)<first_int_imreg then
  1096. begin
  1097. cg.getcpuregister(list,funcretloc.register);
  1098. cg.ungetcpuregister(list,hreg);
  1099. { for the optimizer }
  1100. cg.a_reg_alloc(list,funcretloc.register);
  1101. end;
  1102. { it could be that a structure is passed in memory but the function is expected to
  1103. return a pointer to this memory }
  1104. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1105. cg.a_load_loc_reg(list,OS_ADDR,restmploc,hreg)
  1106. else
  1107. cg.a_load_loc_reg(list,restmploc.size,restmploc,hreg);
  1108. end;
  1109. end;
  1110. LOC_FPUREGISTER:
  1111. begin
  1112. if getsupreg(funcretloc.register)<first_fpu_imreg then
  1113. begin
  1114. cg.getcpuregister(list,funcretloc.register);
  1115. cg.ungetcpuregister(list,funcretloc.register);
  1116. end;
  1117. { we can't do direct moves between fpu and mm registers }
  1118. if restmploc.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  1119. location_force_fpureg(list,restmploc,false);
  1120. cg.a_loadfpu_loc_reg(list,restmploc,funcretloc.register);
  1121. end;
  1122. LOC_MMREGISTER:
  1123. begin
  1124. if getsupreg(funcretloc.register)<first_mm_imreg then
  1125. begin
  1126. cg.getcpuregister(list,funcretloc.register);
  1127. cg.ungetcpuregister(list,funcretloc.register);
  1128. end;
  1129. cg.a_loadmm_loc_reg(list,restmploc.size,restmploc,funcretloc.register,mms_movescalar);
  1130. end;
  1131. LOC_INVALID,
  1132. LOC_REFERENCE:
  1133. ;
  1134. else
  1135. internalerror(200405025);
  1136. end;
  1137. end;
  1138. end;
  1139. procedure gen_load_para_value(list:TAAsmoutput);
  1140. procedure get_para(const paraloc:TCGParaLocation);
  1141. begin
  1142. case paraloc.loc of
  1143. LOC_REGISTER :
  1144. begin
  1145. if getsupreg(paraloc.register)<first_int_imreg then
  1146. cg.getcpuregister(list,paraloc.register);
  1147. end;
  1148. LOC_MMREGISTER :
  1149. begin
  1150. if getsupreg(paraloc.register)<first_mm_imreg then
  1151. cg.getcpuregister(list,paraloc.register);
  1152. end;
  1153. LOC_FPUREGISTER :
  1154. begin
  1155. if getsupreg(paraloc.register)<first_fpu_imreg then
  1156. cg.getcpuregister(list,paraloc.register);
  1157. end;
  1158. end;
  1159. end;
  1160. procedure unget_para(const paraloc:TCGParaLocation);
  1161. begin
  1162. case paraloc.loc of
  1163. LOC_REGISTER :
  1164. begin
  1165. if getsupreg(paraloc.register)<first_int_imreg then
  1166. cg.ungetcpuregister(list,paraloc.register);
  1167. end;
  1168. LOC_MMREGISTER :
  1169. begin
  1170. if getsupreg(paraloc.register)<first_mm_imreg then
  1171. cg.ungetcpuregister(list,paraloc.register);
  1172. end;
  1173. LOC_FPUREGISTER :
  1174. begin
  1175. if getsupreg(paraloc.register)<first_fpu_imreg then
  1176. cg.ungetcpuregister(list,paraloc.register);
  1177. end;
  1178. end;
  1179. end;
  1180. procedure gen_load_ref(const paraloc:TCGParaLocation;const ref:treference;sizeleft:aint);
  1181. var
  1182. href : treference;
  1183. begin
  1184. case paraloc.loc of
  1185. LOC_REGISTER :
  1186. cg.a_load_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref);
  1187. LOC_MMREGISTER :
  1188. cg.a_loadmm_reg_ref(list,paraloc.size,paraloc.size,paraloc.register,ref,mms_movescalar);
  1189. LOC_FPUREGISTER :
  1190. cg.a_loadfpu_reg_ref(list,paraloc.size,paraloc.register,ref);
  1191. LOC_REFERENCE :
  1192. begin
  1193. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1194. { use concatcopy, because it can also be a float which fails when
  1195. load_ref_ref is used. Don't copy data when the references are equal }
  1196. if not((href.base=ref.base) and (href.offset=ref.offset)) then
  1197. cg.g_concatcopy(list,href,ref,sizeleft);
  1198. end;
  1199. else
  1200. internalerror(2002081302);
  1201. end;
  1202. end;
  1203. procedure gen_load_reg(const paraloc:TCGParaLocation;reg:tregister);
  1204. var
  1205. href : treference;
  1206. begin
  1207. case paraloc.loc of
  1208. LOC_REGISTER :
  1209. cg.a_load_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg);
  1210. LOC_MMREGISTER :
  1211. cg.a_loadmm_reg_reg(list,paraloc.size,paraloc.size,paraloc.register,reg,mms_movescalar);
  1212. LOC_FPUREGISTER :
  1213. cg.a_loadfpu_reg_reg(list,paraloc.size,paraloc.register,reg);
  1214. LOC_REFERENCE :
  1215. begin
  1216. reference_reset_base(href,paraloc.reference.index,paraloc.reference.offset);
  1217. case getregtype(reg) of
  1218. R_INTREGISTER :
  1219. cg.a_load_ref_reg(list,paraloc.size,paraloc.size,href,reg);
  1220. R_FPUREGISTER :
  1221. cg.a_loadfpu_ref_reg(list,paraloc.size,href,reg);
  1222. R_MMREGISTER :
  1223. cg.a_loadmm_ref_reg(list,paraloc.size,paraloc.size,href,reg,mms_movescalar);
  1224. else
  1225. internalerror(2004101012);
  1226. end;
  1227. end;
  1228. else
  1229. internalerror(2002081302);
  1230. end;
  1231. end;
  1232. var
  1233. i : longint;
  1234. currpara : tparavarsym;
  1235. paraloc : pcgparalocation;
  1236. href : treference;
  1237. sizeleft : aint;
  1238. {$ifdef sparc}
  1239. tempref : treference;
  1240. {$endif sparc}
  1241. begin
  1242. if (po_assembler in current_procinfo.procdef.procoptions) then
  1243. exit;
  1244. { Allocate registers used by parameters }
  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. while assigned(paraloc) do
  1250. begin
  1251. if paraloc^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_MMREGISTER] then
  1252. get_para(paraloc^);
  1253. paraloc:=paraloc^.next;
  1254. end;
  1255. end;
  1256. { Copy parameters to local references/registers }
  1257. for i:=0 to current_procinfo.procdef.paras.count-1 do
  1258. begin
  1259. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  1260. paraloc:=currpara.paraloc[calleeside].location;
  1261. { skip e.g. empty records }
  1262. if (paraloc^.loc = LOC_VOID) then
  1263. continue;
  1264. if not assigned(paraloc) then
  1265. internalerror(200408203);
  1266. case currpara.localloc.loc of
  1267. LOC_REFERENCE :
  1268. begin
  1269. { If the parameter location is reused we don't need to copy
  1270. anything }
  1271. if not paramanager.param_use_paraloc(currpara.paraloc[calleeside]) then
  1272. begin
  1273. href:=currpara.localloc.reference;
  1274. sizeleft:=currpara.paraloc[calleeside].intsize;
  1275. while assigned(paraloc) do
  1276. begin
  1277. unget_para(paraloc^);
  1278. if (paraloc^.size=OS_NO) then
  1279. begin
  1280. { Can only be a reference that contains the rest
  1281. of the parameter }
  1282. if (paraloc^.loc<>LOC_REFERENCE) or
  1283. assigned(paraloc^.next) then
  1284. internalerror(2005013010);
  1285. gen_load_ref(paraloc^,href,sizeleft);
  1286. inc(href.offset,sizeleft);
  1287. sizeleft:=0;
  1288. end
  1289. else
  1290. begin
  1291. gen_load_ref(paraloc^,href,tcgsize2size[paraloc^.size]);
  1292. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1293. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1294. end;
  1295. paraloc:=paraloc^.next;
  1296. end;
  1297. end;
  1298. end;
  1299. LOC_CREGISTER :
  1300. begin
  1301. {$ifndef cpu64bit}
  1302. if (currpara.paraloc[calleeside].size in [OS_64,OS_S64]) and
  1303. is_64bit(currpara.vartype.def) then
  1304. begin
  1305. if not assigned(paraloc^.next) then
  1306. internalerror(200410104);
  1307. if (target_info.endian=ENDIAN_BIG) then
  1308. begin
  1309. { paraloc^ -> high
  1310. paraloc^.next -> low }
  1311. unget_para(paraloc^);
  1312. gen_load_reg(paraloc^,currpara.localloc.register64.reghi);
  1313. unget_para(paraloc^.next^);
  1314. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reglo);
  1315. end
  1316. else
  1317. begin
  1318. { paraloc^ -> low
  1319. paraloc^.next -> high }
  1320. unget_para(paraloc^);
  1321. gen_load_reg(paraloc^,currpara.localloc.register64.reglo);
  1322. unget_para(paraloc^.next^);
  1323. gen_load_reg(paraloc^.next^,currpara.localloc.register64.reghi);
  1324. end;
  1325. end
  1326. else
  1327. {$endif cpu64bit}
  1328. begin
  1329. if assigned(paraloc^.next) then
  1330. internalerror(200410105);
  1331. unget_para(paraloc^);
  1332. gen_load_reg(paraloc^,currpara.localloc.register);
  1333. end;
  1334. end;
  1335. LOC_CFPUREGISTER :
  1336. begin
  1337. {$ifdef sparc}
  1338. { Sparc passes floats in int registers, when loading to fpu register
  1339. we need a temp }
  1340. sizeleft := TCGSize2Size[currpara.localloc.size];
  1341. tg.GetTemp(list,sizeleft,tt_normal,tempref);
  1342. href:=tempref;
  1343. while assigned(paraloc) do
  1344. begin
  1345. unget_para(paraloc^);
  1346. gen_load_ref(paraloc^,href,sizeleft);
  1347. inc(href.offset,TCGSize2Size[paraloc^.size]);
  1348. dec(sizeleft,TCGSize2Size[paraloc^.size]);
  1349. paraloc:=paraloc^.next;
  1350. end;
  1351. cg.a_loadfpu_ref_reg(list,currpara.localloc.size,tempref,currpara.localloc.register);
  1352. tg.UnGetTemp(list,tempref);
  1353. {$else sparc}
  1354. unget_para(paraloc^);
  1355. gen_load_reg(paraloc^,currpara.localloc.register);
  1356. if assigned(paraloc^.next) then
  1357. internalerror(200410109);
  1358. {$endif sparc}
  1359. end;
  1360. LOC_CMMREGISTER :
  1361. begin
  1362. unget_para(paraloc^);
  1363. gen_load_reg(paraloc^,currpara.localloc.register);
  1364. { data could come in two memory locations, for now
  1365. we simply ignore the sanity check (FK)
  1366. if assigned(paraloc^.next) then
  1367. internalerror(200410108);
  1368. }
  1369. end;
  1370. end;
  1371. end;
  1372. { generate copies of call by value parameters, must be done before
  1373. the initialization and body is parsed because the refcounts are
  1374. incremented using the local copies }
  1375. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1376. {$ifdef powerpc}
  1377. { unget the register that contains the stack pointer before the procedure entry, }
  1378. { which is used to access the parameters in their original callee-side location }
  1379. cg.a_reg_dealloc(list,NR_R12);
  1380. {$endif powerpc}
  1381. end;
  1382. procedure gen_initialize_code(list:TAAsmoutput);
  1383. begin
  1384. { initialize local data like ansistrings }
  1385. case current_procinfo.procdef.proctypeoption of
  1386. potype_unitinit:
  1387. begin
  1388. { this is also used for initialization of variables in a
  1389. program which does not have a globalsymtable }
  1390. if assigned(current_module.globalsymtable) then
  1391. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1392. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1393. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1394. end;
  1395. { units have seperate code for initilization and finalization }
  1396. potype_unitfinalize: ;
  1397. { program init/final is generated in separate procedure }
  1398. potype_proginit:
  1399. begin
  1400. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_regvars,list);
  1401. end;
  1402. else
  1403. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1404. end;
  1405. { initialisizes temp. ansi/wide string data }
  1406. inittempvariables(list);
  1407. { initialize ansi/widesstring para's }
  1408. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1409. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1410. {$ifdef OLDREGVARS}
  1411. load_regvars(list,nil);
  1412. {$endif OLDREGVARS}
  1413. end;
  1414. procedure gen_finalize_code(list:TAAsmoutput);
  1415. begin
  1416. {$ifdef OLDREGVARS}
  1417. cleanup_regvars(list);
  1418. {$endif OLDREGVARS}
  1419. { finalize temporary data }
  1420. finalizetempvariables(list);
  1421. { finalize local data like ansistrings}
  1422. case current_procinfo.procdef.proctypeoption of
  1423. potype_unitfinalize:
  1424. begin
  1425. { this is also used for initialization of variables in a
  1426. program which does not have a globalsymtable }
  1427. if assigned(current_module.globalsymtable) then
  1428. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1429. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_static_data,list);
  1430. end;
  1431. { units/progs have separate code for initialization and finalization }
  1432. potype_unitinit: ;
  1433. { program init/final is generated in separate procedure }
  1434. potype_proginit: ;
  1435. else
  1436. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_local_vars,list);
  1437. end;
  1438. { finalize paras data }
  1439. if assigned(current_procinfo.procdef.parast) and
  1440. not(po_assembler in current_procinfo.procdef.procoptions) then
  1441. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1442. end;
  1443. procedure gen_entry_code(list:TAAsmoutput);
  1444. var
  1445. href : treference;
  1446. paraloc1,
  1447. paraloc2,
  1448. paraloc3 : tcgpara;
  1449. hp : tused_unit;
  1450. begin
  1451. paraloc1.init;
  1452. paraloc2.init;
  1453. { the actual profile code can clobber some registers,
  1454. therefore if the context must be saved, do it before
  1455. the actual call to the profile code
  1456. }
  1457. if (cs_profile in aktmoduleswitches) and
  1458. not(po_assembler in current_procinfo.procdef.procoptions) then
  1459. begin
  1460. { non-win32 can call mcout even in main }
  1461. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) or
  1462. not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1463. begin
  1464. cg.allocallcpuregisters(list);
  1465. cg.g_profilecode(list);
  1466. cg.deallocallcpuregisters(list);
  1467. end;
  1468. end;
  1469. { call startup helpers from main program }
  1470. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1471. begin
  1472. { initialize profiling for win32 }
  1473. if (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1474. (cs_profile in aktmoduleswitches) then
  1475. begin
  1476. reference_reset_symbol(href,objectlibrary.newasmsymbol('etext',AB_EXTERNAL,AT_DATA),0);
  1477. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1478. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1479. paramanager.allocparaloc(list,paraloc2);
  1480. cg.a_paramaddr_ref(list,href,paraloc2);
  1481. reference_reset_symbol(href,objectlibrary.newasmsymbol('__image_base__',AB_EXTERNAL,AT_DATA),0);
  1482. paramanager.allocparaloc(list,paraloc1);
  1483. cg.a_paramaddr_ref(list,href,paraloc1);
  1484. paramanager.freeparaloc(list,paraloc2);
  1485. paramanager.freeparaloc(list,paraloc1);
  1486. cg.allocallcpuregisters(list);
  1487. cg.a_call_name(list,'_monstartup');
  1488. cg.deallocallcpuregisters(list);
  1489. end;
  1490. if (target_info.system = system_powerpc_darwin) then
  1491. begin
  1492. { the parameters are already in the right registers }
  1493. cg.a_call_name(list,target_info.cprefix+'FPC_SYSTEMMAIN');
  1494. end;
  1495. { initialize units }
  1496. cg.allocallcpuregisters(list);
  1497. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1498. cg.deallocallcpuregisters(list);
  1499. {$ifdef GDB}
  1500. if (cs_debuginfo in aktmoduleswitches) then
  1501. if target_info.system <> system_powerpc_macos then
  1502. begin
  1503. { include reference to all debuginfo sections of used units }
  1504. hp:=tused_unit(usedunits.first);
  1505. while assigned(hp) do
  1506. begin
  1507. If (hp.u.flags and uf_has_debuginfo)=uf_has_debuginfo then
  1508. current_procinfo.aktlocaldata.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',hp.u.globalsymtable,''),AT_DATA,0));
  1509. hp:=tused_unit(hp.next);
  1510. end;
  1511. { include reference to debuginfo for this program }
  1512. current_procinfo.aktlocaldata.concat(Tai_const.Createname(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  1513. end;
  1514. {$endif GDB}
  1515. end;
  1516. {$ifdef GDB}
  1517. if (cs_debuginfo in aktmoduleswitches) then
  1518. list.concat(Tai_force_line.Create);
  1519. {$endif GDB}
  1520. {$ifdef OLDREGVARS}
  1521. load_regvars(list,nil);
  1522. {$endif OLDREGVARS}
  1523. paraloc1.done;
  1524. paraloc2.done;
  1525. end;
  1526. procedure gen_exit_code(list:TAAsmoutput);
  1527. begin
  1528. { call __EXIT for main program }
  1529. if (not DLLsource) and
  1530. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1531. cg.a_call_name(list,'FPC_DO_EXIT');
  1532. end;
  1533. {****************************************************************************
  1534. Entry/Exit
  1535. ****************************************************************************}
  1536. procedure gen_proc_symbol(list:Taasmoutput);
  1537. var
  1538. hs : string;
  1539. begin
  1540. { add symbol entry point as well as debug information }
  1541. { will be inserted in front of the rest of this list. }
  1542. { Insert alignment and assembler names }
  1543. { Align, gprof uses 16 byte granularity }
  1544. if (cs_profile in aktmoduleswitches) then
  1545. list.concat(Tai_align.create(16))
  1546. else
  1547. list.concat(Tai_align.create(aktalignment.procalign));
  1548. {$ifdef GDB}
  1549. if (cs_debuginfo in aktmoduleswitches) then
  1550. begin
  1551. if (po_global in current_procinfo.procdef.procoptions) then
  1552. Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
  1553. current_procinfo.procdef.concatstabto(list);
  1554. Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
  1555. end;
  1556. {$endif GDB}
  1557. repeat
  1558. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1559. if hs='' then
  1560. break;
  1561. {$ifdef GDB}
  1562. if (cs_debuginfo in aktmoduleswitches) and
  1563. target_info.use_function_relative_addresses then
  1564. list.concat(Tai_stab_function_name.create(strpnew(hs)));
  1565. {$endif GDB}
  1566. if (cs_profile in aktmoduleswitches) or
  1567. (po_global in current_procinfo.procdef.procoptions) then
  1568. list.concat(Tai_symbol.createname_global(hs,AT_FUNCTION,0))
  1569. else
  1570. list.concat(Tai_symbol.createname(hs,AT_FUNCTION,0));
  1571. until false;
  1572. end;
  1573. procedure gen_proc_symbol_end(list:Taasmoutput);
  1574. {$ifdef GDB}
  1575. var
  1576. stabsendlabel : tasmlabel;
  1577. mangled_length : longint;
  1578. p : pchar;
  1579. {$endif GDB}
  1580. begin
  1581. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1582. {$ifdef GDB}
  1583. if (cs_debuginfo in aktmoduleswitches) then
  1584. begin
  1585. objectlibrary.getlabel(stabsendlabel);
  1586. cg.a_label(list,stabsendlabel);
  1587. { define calling EBP as pseudo local var PM }
  1588. { this enables test if the function is a local one !! }
  1589. {if assigned(current_procinfo.parent) and
  1590. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  1591. list.concat(Tai_stabs.Create(strpnew(
  1592. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1593. tostr(N_LSYM)+',0,0,'+tostr(current_procinfo.parent_framepointer_offset)))); }
  1594. if assigned(current_procinfo.procdef.funcretsym) and
  1595. (tabstractnormalvarsym(current_procinfo.procdef.funcretsym).refs>0) then
  1596. begin
  1597. if tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.loc=LOC_REFERENCE then
  1598. begin
  1599. {$warning Need to add gdb support for ret in param register calling}
  1600. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1601. begin
  1602. list.concat(Tai_stabs.Create(strpnew(
  1603. '"'+current_procinfo.procdef.procsym.name+':X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1604. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1605. if (m_result in aktmodeswitches) then
  1606. list.concat(Tai_stabs.Create(strpnew(
  1607. '"RESULT:X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1608. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))))
  1609. end
  1610. else
  1611. begin
  1612. list.concat(Tai_stabs.Create(strpnew(
  1613. '"'+current_procinfo.procdef.procsym.name+':X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1614. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1615. if (m_result in aktmodeswitches) then
  1616. list.concat(Tai_stabs.Create(strpnew(
  1617. '"RESULT:X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1618. tostr(N_tsym)+',0,0,'+tostr(tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1619. end;
  1620. end;
  1621. end;
  1622. mangled_length:=length(current_procinfo.procdef.mangledname);
  1623. getmem(p,2*mangled_length+50);
  1624. strpcopy(p,'192,0,0,');
  1625. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1626. if (target_info.use_function_relative_addresses) then
  1627. begin
  1628. strpcopy(strend(p),'-');
  1629. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1630. end;
  1631. list.concat(Tai_stabn.Create(strnew(p)));
  1632. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1633. +current_procinfo.procdef.mangledname))));
  1634. p[0]:='2';p[1]:='2';p[2]:='4';
  1635. strpcopy(strend(p),'_end');}
  1636. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1637. if (target_info.use_function_relative_addresses) then
  1638. begin
  1639. strpcopy(strend(p),'-');
  1640. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1641. end;
  1642. list.concatlist(withdebuglist);
  1643. list.concat(Tai_stabn.Create(strnew(p)));
  1644. { strpnew('224,0,0,'
  1645. +current_procinfo.procdef.mangledname+'_end'))));}
  1646. freemem(p,2*mangled_length+50);
  1647. end;
  1648. {$endif GDB}
  1649. end;
  1650. procedure gen_proc_entry_code(list:Taasmoutput);
  1651. var
  1652. hitemp,
  1653. lotemp : longint;
  1654. begin
  1655. { generate call frame marker for dwarf call frame info }
  1656. dwarfcfi.start_frame(list);
  1657. { All temps are know, write offsets used for information }
  1658. if (cs_asm_source in aktglobalswitches) then
  1659. begin
  1660. if tg.direction>0 then
  1661. begin
  1662. lotemp:=current_procinfo.tempstart;
  1663. hitemp:=tg.lasttemp;
  1664. end
  1665. else
  1666. begin
  1667. lotemp:=tg.lasttemp;
  1668. hitemp:=current_procinfo.tempstart;
  1669. end;
  1670. list.concat(Tai_comment.Create(strpnew('Temps allocated between '+std_regname(current_procinfo.framepointer)+
  1671. tostr_with_plus(lotemp)+' and '+std_regname(current_procinfo.framepointer)+tostr_with_plus(hitemp))));
  1672. end;
  1673. { generate target specific proc entry code }
  1674. cg.g_proc_entry(list,current_procinfo.calc_stackframe_size,(po_nostackframe in current_procinfo.procdef.procoptions));
  1675. end;
  1676. procedure gen_proc_exit_code(list:Taasmoutput);
  1677. var
  1678. parasize : longint;
  1679. begin
  1680. { c style clearstack does not need to remove parameters from the stack, only the
  1681. return value when it was pushed by arguments }
  1682. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1683. begin
  1684. parasize:=0;
  1685. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1686. inc(parasize,sizeof(aint));
  1687. end
  1688. else
  1689. parasize:=current_procinfo.para_stack_size;
  1690. { generate target specific proc exit code }
  1691. cg.g_proc_exit(list,parasize,(po_nostackframe in current_procinfo.procdef.procoptions));
  1692. { release return registers, needed for optimizer }
  1693. if not is_void(current_procinfo.procdef.rettype.def) then
  1694. location_free(list,current_procinfo.procdef.funcretloc[calleeside]);
  1695. { end of frame marker for call frame info }
  1696. dwarfcfi.end_frame(list);
  1697. end;
  1698. procedure gen_stack_check_size_para(list:Taasmoutput);
  1699. var
  1700. paraloc1 : tcgpara;
  1701. begin
  1702. paraloc1.init;
  1703. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1704. paramanager.allocparaloc(list,paraloc1);
  1705. cg.a_param_const(list,OS_INT,current_procinfo.calc_stackframe_size,paraloc1);
  1706. paramanager.freeparaloc(list,paraloc1);
  1707. paraloc1.done;
  1708. end;
  1709. procedure gen_stack_check_call(list:Taasmoutput);
  1710. var
  1711. paraloc1 : tcgpara;
  1712. begin
  1713. paraloc1.init;
  1714. { Also alloc the register needed for the parameter }
  1715. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1716. paramanager.allocparaloc(list,paraloc1);
  1717. paramanager.freeparaloc(list,paraloc1);
  1718. { Call the helper }
  1719. cg.allocallcpuregisters(list);
  1720. cg.a_call_name(list,'FPC_STACKCHECK');
  1721. cg.deallocallcpuregisters(list);
  1722. paraloc1.done;
  1723. end;
  1724. procedure gen_save_used_regs(list:TAAsmoutput);
  1725. begin
  1726. { Pure assembler routines need to save the registers themselves }
  1727. if (po_assembler in current_procinfo.procdef.procoptions) then
  1728. exit;
  1729. { oldfpccall expects all registers to be destroyed }
  1730. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1731. cg.g_save_standard_registers(list);
  1732. end;
  1733. procedure gen_restore_used_regs(list:TAAsmoutput);
  1734. begin
  1735. { Pure assembler routines need to save the registers themselves }
  1736. if (po_assembler in current_procinfo.procdef.procoptions) then
  1737. exit;
  1738. { oldfpccall expects all registers to be destroyed }
  1739. if current_procinfo.procdef.proccalloption<>pocall_oldfpccall then
  1740. cg.g_restore_standard_registers(list);
  1741. end;
  1742. {****************************************************************************
  1743. External handling
  1744. ****************************************************************************}
  1745. procedure gen_external_stub(list:taasmoutput;pd:tprocdef;const externalname:string);
  1746. begin
  1747. { add the procedure to the codesegment }
  1748. maybe_new_object_file(list);
  1749. new_section(list,sec_code,lower(pd.mangledname),aktalignment.procalign);
  1750. list.concat(Tai_align.create(aktalignment.procalign));
  1751. if (po_global in pd.procoptions) then
  1752. list.concat(Tai_symbol.createname_global(pd.mangledname,AT_FUNCTION,0))
  1753. else
  1754. list.concat(Tai_symbol.createname(pd.mangledname,AT_FUNCTION,0));
  1755. cg.a_jmp_name(list,externalname);
  1756. end;
  1757. {****************************************************************************
  1758. Const Data
  1759. ****************************************************************************}
  1760. procedure insertconstdata(sym : ttypedconstsym);
  1761. { this does not affect the local stack space, since all
  1762. typed constansts and initialized variables are always
  1763. put in the .data / .rodata section
  1764. }
  1765. var
  1766. storefilepos : tfileposinfo;
  1767. curconstsegment : taasmoutput;
  1768. l : longint;
  1769. begin
  1770. storefilepos:=aktfilepos;
  1771. aktfilepos:=sym.fileinfo;
  1772. if sym.is_writable then
  1773. curconstsegment:=datasegment
  1774. else
  1775. curconstsegment:=consts;
  1776. l:=sym.getsize;
  1777. { insert cut for smartlinking or alignment }
  1778. maybe_new_object_file(curconstSegment);
  1779. new_section(curconstSegment,sec_rodata,lower(sym.mangledname),const_align(l));
  1780. {$ifdef GDB}
  1781. if (cs_debuginfo in aktmoduleswitches) then
  1782. sym.concatstabto(curconstSegment);
  1783. {$endif GDB}
  1784. if (sym.owner.symtabletype=globalsymtable) or
  1785. maybe_smartlink_symbol or
  1786. (assigned(current_procinfo) and
  1787. (po_inline in current_procinfo.procdef.procoptions)) or
  1788. DLLSource then
  1789. curconstSegment.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,l))
  1790. else
  1791. curconstSegment.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,l));
  1792. aktfilepos:=storefilepos;
  1793. end;
  1794. procedure insertbssdata(sym : tglobalvarsym);
  1795. var
  1796. l,varalign : longint;
  1797. storefilepos : tfileposinfo;
  1798. begin
  1799. storefilepos:=aktfilepos;
  1800. aktfilepos:=sym.fileinfo;
  1801. l:=sym.getsize;
  1802. if (vo_is_thread_var in sym.varoptions) then
  1803. inc(l,sizeof(aint));
  1804. varalign:=var_align(l);
  1805. maybe_new_object_file(bssSegment);
  1806. new_section(bssSegment,sec_bss,lower(sym.mangledname),varalign);
  1807. {$ifdef GDB}
  1808. if (cs_debuginfo in aktmoduleswitches) then
  1809. sym.concatstabto(bssSegment);
  1810. {$endif GDB}
  1811. if (sym.owner.symtabletype=globalsymtable) or
  1812. maybe_smartlink_symbol or
  1813. DLLSource or
  1814. (assigned(current_procinfo) and
  1815. (po_inline in current_procinfo.procdef.procoptions)) or
  1816. (vo_is_exported in sym.varoptions) or
  1817. (vo_is_C_var in sym.varoptions) then
  1818. bssSegment.concat(Tai_datablock.Create_global(sym.mangledname,l))
  1819. else
  1820. bssSegment.concat(Tai_datablock.Create(sym.mangledname,l));
  1821. aktfilepos:=storefilepos;
  1822. end;
  1823. procedure gen_alloc_symtable(list:TAAsmoutput;st:tsymtable);
  1824. var
  1825. sym : tsym;
  1826. isaddr : boolean;
  1827. cgsize : tcgsize;
  1828. begin
  1829. sym:=tsym(st.symindex.first);
  1830. while assigned(sym) do
  1831. begin
  1832. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1833. begin
  1834. with tabstractnormalvarsym(sym) do
  1835. begin
  1836. { Parameters passed to assembler procedures need to be kept
  1837. in the original location }
  1838. if (sym.typ=paravarsym) and
  1839. (po_assembler in current_procinfo.procdef.procoptions) then
  1840. begin
  1841. tparavarsym(sym).paraloc[calleeside].get_location(localloc);
  1842. end
  1843. else
  1844. begin
  1845. isaddr:=(st.symtabletype=parasymtable) and
  1846. paramanager.push_addr_param(varspez,vartype.def,current_procinfo.procdef.proccalloption);
  1847. if isaddr then
  1848. cgsize:=OS_ADDR
  1849. else
  1850. cgsize:=def_cgsize(vartype.def);
  1851. {$ifndef OLDREGVARS}
  1852. { When there is assembler code we can't use regvars }
  1853. if is_regvar then
  1854. begin
  1855. localloc.size:=cgsize;
  1856. case varregable of
  1857. vr_intreg :
  1858. begin
  1859. localloc.loc:=LOC_CREGISTER;
  1860. {$ifndef cpu64bit}
  1861. if cgsize in [OS_64,OS_S64] then
  1862. begin
  1863. localloc.register64.reglo:=cg.getintregister(list,OS_32);
  1864. localloc.register64.reghi:=cg.getintregister(list,OS_32);
  1865. end
  1866. else
  1867. {$endif cpu64bit}
  1868. localloc.register:=cg.getintregister(list,cgsize);
  1869. end;
  1870. vr_fpureg :
  1871. begin
  1872. localloc.loc:=LOC_CFPUREGISTER;
  1873. localloc.register:=cg.getfpuregister(list,cgsize);
  1874. end;
  1875. vr_mmreg :
  1876. begin
  1877. localloc.loc:=LOC_CMMREGISTER;
  1878. localloc.register:=cg.getmmregister(list,cgsize);
  1879. end;
  1880. else
  1881. internalerror(2004101010);
  1882. end;
  1883. { Allocate register already, to prevent first allocation to be
  1884. inside a loop }
  1885. {$ifndef cpu64bit}
  1886. if cgsize in [OS_64,OS_S64] then
  1887. begin
  1888. cg.a_reg_sync(list,localloc.register64.reglo);
  1889. cg.a_reg_sync(list,localloc.register64.reghi);
  1890. end
  1891. else
  1892. {$endif cpu64bit}
  1893. cg.a_reg_sync(list,localloc.register);
  1894. end
  1895. else
  1896. {$endif NOT OLDREGVARS}
  1897. begin
  1898. localloc.loc:=LOC_REFERENCE;
  1899. localloc.size:=cgsize;
  1900. case st.symtabletype of
  1901. parasymtable :
  1902. begin
  1903. { Reuse the parameter location for values to are at a single location on the stack }
  1904. if paramanager.param_use_paraloc(tparavarsym(sym).paraloc[calleeside]) then
  1905. begin
  1906. reference_reset_base(localloc.reference,tparavarsym(sym).paraloc[calleeside].location^.reference.index,
  1907. tparavarsym(sym).paraloc[calleeside].location^.reference.offset);
  1908. end
  1909. else
  1910. begin
  1911. if isaddr then
  1912. tg.GetLocal(list,sizeof(aint),voidpointertype.def,localloc.reference)
  1913. else
  1914. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1915. end;
  1916. end;
  1917. localsymtable,
  1918. stt_exceptsymtable :
  1919. begin
  1920. tg.GetLocal(list,getsize,vartype.def,localloc.reference);
  1921. end;
  1922. staticsymtable :
  1923. begin
  1924. { PIC, DLL and Threadvar need extra code and are handled in ncgld }
  1925. if not((target_info.system=system_powerpc_darwin) and
  1926. (cs_create_pic in aktmoduleswitches)) and
  1927. not(vo_is_dll_var in varoptions) and
  1928. not(vo_is_thread_var in varoptions) then
  1929. reference_reset_symbol(localloc.reference,objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA),0);
  1930. end;
  1931. else
  1932. internalerror(200410103);
  1933. end;
  1934. end;
  1935. end;
  1936. if cs_asm_source in aktglobalswitches then
  1937. begin
  1938. case localloc.loc of
  1939. LOC_REGISTER,
  1940. LOC_CREGISTER :
  1941. begin
  1942. if (cs_no_regalloc in aktglobalswitches) then
  1943. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register '+
  1944. std_regname(localloc.register))))
  1945. else
  1946. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located in register')));
  1947. end;
  1948. LOC_REFERENCE :
  1949. begin
  1950. if not assigned(localloc.reference.symbol) then
  1951. list.concat(Tai_comment.Create(strpnew('Var '+realname+' located at '+
  1952. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  1953. end;
  1954. end;
  1955. end;
  1956. end;
  1957. end;
  1958. sym:=tsym(sym.indexnext);
  1959. end;
  1960. end;
  1961. procedure gen_free_symtable(list:TAAsmoutput;st:tsymtable);
  1962. var
  1963. sym : tsym;
  1964. begin
  1965. sym:=tsym(st.symindex.first);
  1966. while assigned(sym) do
  1967. begin
  1968. if (sym.typ in [globalvarsym,localvarsym,paravarsym]) then
  1969. begin
  1970. with tabstractnormalvarsym(sym) do
  1971. begin
  1972. { Note: We need to keep the data available in memory
  1973. for the sub procedures that can access local data
  1974. in the parent procedures }
  1975. case localloc.loc of
  1976. LOC_CREGISTER :
  1977. {$ifndef cpu64bit}
  1978. if def_cgsize(vartype.def) in [OS_64,OS_S64] then
  1979. begin
  1980. cg.a_reg_sync(list,localloc.register64.reglo);
  1981. cg.a_reg_sync(list,localloc.register64.reghi);
  1982. end
  1983. else
  1984. {$endif cpu64bit}
  1985. cg.a_reg_sync(list,localloc.register);
  1986. LOC_REFERENCE :
  1987. begin
  1988. case st.symtabletype of
  1989. localsymtable,
  1990. parasymtable,
  1991. stt_exceptsymtable :
  1992. tg.Ungetlocal(list,localloc.reference);
  1993. end;
  1994. end;
  1995. end;
  1996. end;
  1997. end;
  1998. sym:=tsym(sym.indexnext);
  1999. end;
  2000. end;
  2001. {$ifdef PASS2INLINE}
  2002. procedure gen_alloc_inline_parast(list:TAAsmoutput;pd:tprocdef);
  2003. var
  2004. sym : tsym;
  2005. calleeparaloc,
  2006. callerparaloc : pcgparalocation;
  2007. begin
  2008. if (po_assembler in pd.procoptions) then
  2009. exit;
  2010. sym:=tsym(pd.parast.symindex.first);
  2011. while assigned(sym) do
  2012. begin
  2013. if sym.typ=paravarsym then
  2014. begin
  2015. with tparavarsym(sym) do
  2016. begin
  2017. { for localloc <> LOC_REFERENCE, we need regvar support inside inlined procedures }
  2018. localloc.loc:=LOC_REFERENCE;
  2019. localloc.size:=int_cgsize(paramanager.push_size(varspez,vartype.def,pd.proccalloption));
  2020. tg.GetLocal(list,tcgsize2size[localloc.size],vartype.def,localloc.reference);
  2021. calleeparaloc:=paraloc[calleeside].location;
  2022. callerparaloc:=paraloc[callerside].location;
  2023. while assigned(calleeparaloc) do
  2024. begin
  2025. if not assigned(callerparaloc) then
  2026. internalerror(200408281);
  2027. if calleeparaloc^.loc<>callerparaloc^.loc then
  2028. internalerror(200408282);
  2029. case calleeparaloc^.loc of
  2030. LOC_FPUREGISTER:
  2031. begin
  2032. calleeparaloc^.register:=cg.getfpuregister(list,calleeparaloc^.size);
  2033. callerparaloc^.register:=calleeparaloc^.register;
  2034. end;
  2035. LOC_REGISTER:
  2036. begin
  2037. calleeparaloc^.register:=cg.getintregister(list,calleeparaloc^.size);
  2038. callerparaloc^.register:=calleeparaloc^.register;
  2039. end;
  2040. LOC_MMREGISTER:
  2041. begin
  2042. calleeparaloc^.register:=cg.getmmregister(list,calleeparaloc^.size);
  2043. callerparaloc^.register:=calleeparaloc^.register;
  2044. end;
  2045. LOC_REFERENCE:
  2046. begin
  2047. calleeparaloc^.reference.offset := localloc.reference.offset;
  2048. calleeparaloc^.reference.index := localloc.reference.base;
  2049. callerparaloc^.reference.offset := localloc.reference.offset;
  2050. callerparaloc^.reference.index := localloc.reference.base;
  2051. end;
  2052. end;
  2053. calleeparaloc:=calleeparaloc^.next;
  2054. callerparaloc:=callerparaloc^.next;
  2055. end;
  2056. if cs_asm_source in aktglobalswitches then
  2057. begin
  2058. case localloc.loc of
  2059. LOC_REFERENCE :
  2060. list.concat(Tai_comment.Create(strpnew('Para '+realname+' allocated at '+
  2061. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  2062. end;
  2063. end;
  2064. end;
  2065. end;
  2066. sym:=tsym(sym.indexnext);
  2067. end;
  2068. end;
  2069. procedure gen_alloc_inline_funcret(list:TAAsmoutput;pd:tprocdef);
  2070. var
  2071. callerparaloc : tlocation;
  2072. begin
  2073. if not assigned(pd.funcretsym) or
  2074. (po_assembler in pd.procoptions) then
  2075. exit;
  2076. { for localloc <> LOC_REFERENCE, we need regvar support inside inlined procedures }
  2077. with tabstractnormalvarsym(pd.funcretsym) do
  2078. begin
  2079. localloc.loc:=LOC_REFERENCE;
  2080. localloc.size:=int_cgsize(paramanager.push_size(varspez,vartype.def,pd.proccalloption));
  2081. tg.GetLocal(list,tcgsize2size[localloc.size],vartype.def,localloc.reference);
  2082. callerparaloc:=pd.funcretloc[callerside];
  2083. case pd.funcretloc[calleeside].loc of
  2084. LOC_FPUREGISTER:
  2085. begin
  2086. pd.funcretloc[calleeside].register:=cg.getfpuregister(list,pd.funcretloc[calleeside].size);
  2087. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2088. end;
  2089. LOC_REGISTER:
  2090. begin
  2091. {$ifndef cpu64bit}
  2092. if callerparaloc.size in [OS_64,OS_S64] then
  2093. begin
  2094. end
  2095. else
  2096. {$endif cpu64bit}
  2097. begin
  2098. pd.funcretloc[calleeside].register:=cg.getintregister(list,pd.funcretloc[calleeside].size);
  2099. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2100. end;
  2101. end;
  2102. LOC_MMREGISTER:
  2103. begin
  2104. pd.funcretloc[calleeside].register:=cg.getmmregister(list,pd.funcretloc[calleeside].size);
  2105. pd.funcretloc[callerside].register:=pd.funcretloc[calleeside].register;
  2106. end;
  2107. LOC_REFERENCE:
  2108. begin
  2109. pd.funcretloc[calleeside].reference.offset := localloc.reference.offset;
  2110. pd.funcretloc[calleeside].reference.index := localloc.reference.base;
  2111. pd.funcretloc[callerside].reference.offset := localloc.reference.offset;
  2112. pd.funcretloc[callerside].reference.index := localloc.reference.base;
  2113. end;
  2114. LOC_VOID:
  2115. ;
  2116. else
  2117. internalerror(200411191);
  2118. end;
  2119. if cs_asm_source in aktglobalswitches then
  2120. begin
  2121. case localloc.loc of
  2122. LOC_REFERENCE :
  2123. list.concat(Tai_comment.Create(strpnew('Funcret '+realname+' allocated at '+
  2124. std_regname(localloc.reference.base)+tostr_with_plus(localloc.reference.offset))));
  2125. end;
  2126. end;
  2127. end;
  2128. end;
  2129. {$endif PASS2INLINE}
  2130. { persistent rtti generation }
  2131. procedure generate_rtti(p:Ttypesym);
  2132. var
  2133. rsym : trttisym;
  2134. def : tstoreddef;
  2135. begin
  2136. { rtti can only be generated for classes that are always typesyms }
  2137. def:=tstoreddef(ttypesym(p).restype.def);
  2138. { there is an error, skip rtti info }
  2139. if (def.deftype=errordef) or (Errorcount>0) then
  2140. exit;
  2141. { only create rtti once for each definition }
  2142. if not(df_has_rttitable in def.defoptions) then
  2143. begin
  2144. { definition should be in the same symtable as the symbol }
  2145. if p.owner<>def.owner then
  2146. internalerror(200108262);
  2147. { create rttisym }
  2148. rsym:=trttisym.create(p.name,fullrtti);
  2149. p.owner.insert(rsym);
  2150. { register rttisym in definition }
  2151. include(def.defoptions,df_has_rttitable);
  2152. def.rttitablesym:=rsym;
  2153. { write rtti data }
  2154. def.write_child_rtti_data(fullrtti);
  2155. maybe_new_object_file(rttilist);
  2156. new_section(rttilist,sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2157. rttiList.concat(Tai_symbol.Create_global(rsym.get_label,0));
  2158. def.write_rtti_data(fullrtti);
  2159. rttiList.concat(Tai_symbol_end.Create(rsym.get_label));
  2160. end;
  2161. end;
  2162. { persistent init table generation }
  2163. procedure generate_inittable(p:tsym);
  2164. var
  2165. rsym : trttisym;
  2166. def : tstoreddef;
  2167. begin
  2168. { anonymous types are also allowed for records that can be varsym }
  2169. case p.typ of
  2170. typesym :
  2171. def:=tstoreddef(ttypesym(p).restype.def);
  2172. globalvarsym,
  2173. localvarsym,
  2174. paravarsym :
  2175. def:=tstoreddef(tabstractvarsym(p).vartype.def);
  2176. else
  2177. internalerror(200108263);
  2178. end;
  2179. { only create inittable once for each definition }
  2180. if not(df_has_inittable in def.defoptions) then
  2181. begin
  2182. { definition should be in the same symtable as the symbol }
  2183. if p.owner<>def.owner then
  2184. internalerror(200108264);
  2185. { create rttisym }
  2186. rsym:=trttisym.create(p.name,initrtti);
  2187. p.owner.insert(rsym);
  2188. { register rttisym in definition }
  2189. include(def.defoptions,df_has_inittable);
  2190. def.inittablesym:=rsym;
  2191. { write inittable data }
  2192. def.write_child_rtti_data(initrtti);
  2193. maybe_new_object_file(rttilist);
  2194. new_section(rttilist,sec_rodata,rsym.get_label.name,const_align(sizeof(aint)));
  2195. rttiList.concat(Tai_symbol.Create_global(rsym.get_label,0));
  2196. def.write_rtti_data(initrtti);
  2197. rttiList.concat(Tai_symbol_end.Create(rsym.get_label));
  2198. end;
  2199. end;
  2200. procedure gen_intf_wrapper(list:taasmoutput;_class:tobjectdef);
  2201. var
  2202. i,j,
  2203. proccount : longint;
  2204. tmps : string;
  2205. begin
  2206. for i:=1 to _class.implementedinterfaces.count do
  2207. begin
  2208. { only if implemented by this class }
  2209. if _class.implementedinterfaces.implindex(i)=i then
  2210. begin
  2211. proccount:=_class.implementedinterfaces.implproccount(i);
  2212. for j:=1 to proccount do
  2213. begin
  2214. tmps:=make_mangledname('WRPR',_class.owner,_class.objname^+'_$_'+
  2215. _class.implementedinterfaces.interfaces(i).objname^+'_$_'+
  2216. tostr(j)+'_$_'+_class.implementedinterfaces.implprocs(i,j).mangledname);
  2217. { create wrapper code }
  2218. new_section(list,sec_code,lower(tmps),0);
  2219. cg.g_intf_wrapper(list,_class.implementedinterfaces.implprocs(i,j),tmps,_class.implementedinterfaces.ioffsets(i));
  2220. end;
  2221. end;
  2222. end;
  2223. end;
  2224. procedure gen_intf_wrappers(list:taasmoutput;st:tsymtable);
  2225. var
  2226. def : tstoreddef;
  2227. begin
  2228. def:=tstoreddef(st.defindex.first);
  2229. while assigned(def) do
  2230. begin
  2231. if is_class(def) then
  2232. gen_intf_wrapper(list,tobjectdef(def));
  2233. def:=tstoreddef(def.indexnext);
  2234. end;
  2235. end;
  2236. end.