ncgutil.pas 95 KB

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