ncgutil.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Helper routines for all code generators
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,cpuinfo,
  23. globtype,
  24. cpubase,cgbase,
  25. aasmbase,aasmtai,aasmcpu,
  26. symconst,symbase,symdef,symsym,symtype,symtable,
  27. {$ifndef cpu64bit}
  28. cg64f32
  29. {$endif cpu64bit}
  30. ;
  31. type
  32. tloadregvars = (lr_dont_load_regvars, lr_load_regvars);
  33. procedure firstcomplex(p : tbinarynode);
  34. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  35. // procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  36. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  37. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  38. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  39. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  40. procedure gen_proc_symbol(list:Taasmoutput);
  41. procedure gen_proc_symbol_end(list:Taasmoutput);
  42. procedure gen_stackalloc_code(list:Taasmoutput);
  43. procedure gen_stackfree_code(list:Taasmoutput;usesacc,usesacchi:boolean);
  44. procedure gen_save_used_regs(list:TAAsmoutput);
  45. procedure gen_restore_used_regs(list:TAAsmoutput;usesacc,usesacchi,usesfpu:boolean);
  46. procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
  47. procedure gen_finalize_code(list:TAAsmoutput;inlined:boolean);
  48. procedure gen_entry_code(list:TAAsmoutput);
  49. procedure gen_exit_code(list:TAAsmoutput);
  50. procedure gen_load_para_value(list:TAAsmoutput);
  51. procedure gen_load_return_value(list:TAAsmoutput; var uses_acc,uses_acchi,uses_fpu : boolean);
  52. (*
  53. procedure geninlineentrycode(list:TAAsmoutput;stackframe:longint);
  54. procedure geninlineexitcode(list:TAAsmoutput;inlined:boolean);
  55. *)
  56. {#
  57. Allocate the buffers for exception management and setjmp environment.
  58. Return a pointer to these buffers, send them to the utility routine
  59. so they are registered, and then call setjmp.
  60. Then compare the result of setjmp with 0, and if not equal
  61. to zero, then jump to exceptlabel.
  62. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  63. It is to note that this routine may be called *after* the stackframe of a
  64. routine has been called, therefore on machines where the stack cannot
  65. be modified, all temps should be allocated on the heap instead of the
  66. stack.
  67. }
  68. procedure new_exception(list:TAAsmoutput;const jmpbuf,envbuf, href : treference;
  69. a : aword; exceptlabel : tasmlabel);
  70. procedure free_exception(list:TAAsmoutput;const jmpbuf, envbuf, href : treference;
  71. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  72. procedure insertconstdata(sym : ttypedconstsym);
  73. procedure insertbssdata(sym : tvarsym);
  74. procedure gen_alloc_localst(list:TAAsmoutput;st:tlocalsymtable);
  75. procedure gen_free_localst(list:TAAsmoutput;st:tlocalsymtable);
  76. procedure gen_alloc_parast(list:TAAsmoutput;st:tparasymtable);
  77. procedure gen_alloc_inline_parast(list:TAAsmoutput;st:tparasymtable);
  78. procedure gen_free_parast(list:TAAsmoutput;st:tparasymtable);
  79. implementation
  80. uses
  81. {$ifdef Delphi}
  82. Sysutils,
  83. {$else}
  84. strings,
  85. {$endif}
  86. cutils,cclasses,
  87. globals,systems,verbose,
  88. defutil,
  89. procinfo,paramgr,fmodule,
  90. regvars,
  91. {$ifdef GDB}
  92. gdb,
  93. {$endif GDB}
  94. ncon,
  95. tgobj,cgobj;
  96. const
  97. { Please leave this here, this module should NOT use
  98. exprasmlist, the lists are always passed as arguments.
  99. Declaring it as string here results in an error when compiling (PFV) }
  100. exprasmlist = 'error';
  101. {*****************************************************************************
  102. Misc Helpers
  103. *****************************************************************************}
  104. { DO NOT RELY on the fact that the tnode is not yet swaped
  105. because of inlining code PM }
  106. procedure firstcomplex(p : tbinarynode);
  107. var
  108. hp : tnode;
  109. begin
  110. { always calculate boolean AND and OR from left to right }
  111. if (p.nodetype in [orn,andn]) and
  112. is_boolean(p.left.resulttype.def) then
  113. begin
  114. if nf_swaped in p.flags then
  115. internalerror(234234);
  116. end
  117. else
  118. if (
  119. (p.location.loc=LOC_FPUREGISTER) and
  120. (p.right.registersfpu > p.left.registersfpu)
  121. ) or
  122. (
  123. (
  124. (
  125. ((p.left.registersfpu = 0) and (p.right.registersfpu = 0)) or
  126. (p.location.loc<>LOC_FPUREGISTER)
  127. ) and
  128. (p.left.registers32<p.right.registers32)
  129. )
  130. ) then
  131. begin
  132. hp:=p.left;
  133. p.left:=p.right;
  134. p.right:=hp;
  135. if nf_swaped in p.flags then
  136. exclude(p.flags,nf_swaped)
  137. else
  138. include(p.flags,nf_swaped);
  139. end;
  140. end;
  141. procedure maketojumpbool(list:TAAsmoutput; p : tnode; loadregvars: tloadregvars);
  142. {
  143. produces jumps to true respectively false labels using boolean expressions
  144. depending on whether the loading of regvars is currently being
  145. synchronized manually (such as in an if-node) or automatically (most of
  146. the other cases where this procedure is called), loadregvars can be
  147. "lr_load_regvars" or "lr_dont_load_regvars"
  148. }
  149. var
  150. opsize : tcgsize;
  151. storepos : tfileposinfo;
  152. begin
  153. if nf_error in p.flags then
  154. exit;
  155. storepos:=aktfilepos;
  156. aktfilepos:=p.fileinfo;
  157. if is_boolean(p.resulttype.def) then
  158. begin
  159. {$ifdef OLDREGVARS}
  160. if loadregvars = lr_load_regvars then
  161. load_all_regvars(list);
  162. {$endif OLDREGVARS}
  163. if is_constboolnode(p) then
  164. begin
  165. if tordconstnode(p).value<>0 then
  166. cg.a_jmp_always(list,truelabel)
  167. else
  168. cg.a_jmp_always(list,falselabel)
  169. end
  170. else
  171. begin
  172. opsize:=def_cgsize(p.resulttype.def);
  173. case p.location.loc of
  174. LOC_CREGISTER,LOC_REGISTER,LOC_CREFERENCE,LOC_REFERENCE :
  175. begin
  176. {$ifdef OLDREGVARS}
  177. if (p.location.loc = LOC_CREGISTER) then
  178. load_regvar_reg(list,p.location.register);
  179. {$endif OLDREGVARS}
  180. cg.a_cmp_const_loc_label(list,opsize,OC_NE,
  181. 0,p.location,truelabel);
  182. { !!! should happen right after cmp (JM) }
  183. location_release(list,p.location);
  184. cg.a_jmp_always(list,falselabel);
  185. end;
  186. LOC_JUMP:
  187. ;
  188. {$ifdef cpuflags}
  189. LOC_FLAGS :
  190. begin
  191. cg.a_jmp_flags(list,p.location.resflags,
  192. truelabel);
  193. cg.a_jmp_always(list,falselabel);
  194. end;
  195. {$endif cpuflags}
  196. else
  197. internalerror(200308241);
  198. end;
  199. end;
  200. end
  201. else
  202. internalerror(200112305);
  203. aktfilepos:=storepos;
  204. end;
  205. (*
  206. This code needs fixing. It is not safe to use rgint; on the m68000 it
  207. would be rgaddr.
  208. procedure remove_non_regvars_from_loc(const t: tlocation; var regs:Tsuperregisterset);
  209. begin
  210. case t.loc of
  211. LOC_REGISTER:
  212. begin
  213. { can't be a regvar, since it would be LOC_CREGISTER then }
  214. exclude(regs,getsupreg(t.register));
  215. if t.registerhigh<>NR_NO then
  216. exclude(regs,getsupreg(t.registerhigh));
  217. end;
  218. LOC_CREFERENCE,LOC_REFERENCE:
  219. begin
  220. if not(cs_regvars in aktglobalswitches) or
  221. (getsupreg(t.reference.base) in cg.rgint.usableregs) then
  222. exclude(regs,getsupreg(t.reference.base));
  223. if not(cs_regvars in aktglobalswitches) or
  224. (getsupreg(t.reference.index) in cg.rgint.usableregs) then
  225. exclude(regs,getsupreg(t.reference.index));
  226. end;
  227. end;
  228. end;
  229. *)
  230. {*****************************************************************************
  231. EXCEPTION MANAGEMENT
  232. *****************************************************************************}
  233. procedure new_exception(list:TAAsmoutput;const jmpbuf,envbuf, href : treference;
  234. a : aword; exceptlabel : tasmlabel);
  235. var
  236. paraloc1,paraloc2,paraloc3 : tparalocation;
  237. begin
  238. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  239. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  240. paraloc3:=paramanager.getintparaloc(pocall_default,3);
  241. paramanager.allocparaloc(list,paraloc3);
  242. cg.a_paramaddr_ref(list,envbuf,paraloc3);
  243. paramanager.allocparaloc(list,paraloc2);
  244. cg.a_paramaddr_ref(list,jmpbuf,paraloc2);
  245. { push type of exceptionframe }
  246. paramanager.allocparaloc(list,paraloc1);
  247. cg.a_param_const(list,OS_S32,1,paraloc1);
  248. paramanager.freeparaloc(list,paraloc3);
  249. paramanager.freeparaloc(list,paraloc2);
  250. paramanager.freeparaloc(list,paraloc1);
  251. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  252. cg.a_call_name(list,'FPC_PUSHEXCEPTADDR');
  253. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  254. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  255. paramanager.allocparaloc(list,paraloc1);
  256. cg.a_param_reg(list,OS_ADDR,NR_FUNCTION_RESULT_REG,paraloc1);
  257. paramanager.freeparaloc(list,paraloc1);
  258. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  259. cg.a_call_name(list,'FPC_SETJMP');
  260. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  261. cg.g_exception_reason_save(list, href);
  262. cg.a_cmp_const_reg_label(list,OS_S32,OC_NE,0,NR_FUNCTION_RESULT_REG,exceptlabel);
  263. end;
  264. procedure free_exception(list:TAAsmoutput;const jmpbuf, envbuf, href : treference;
  265. a : aword ; endexceptlabel : tasmlabel; onlyfree : boolean);
  266. begin
  267. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  268. cg.a_call_name(list,'FPC_POPADDRSTACK');
  269. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  270. if not onlyfree then
  271. begin
  272. cg.g_exception_reason_load(list, href);
  273. cg.a_cmp_const_reg_label(list,OS_S32,OC_EQ,a,NR_FUNCTION_RESULT_REG,endexceptlabel);
  274. end;
  275. end;
  276. {*****************************************************************************
  277. TLocation
  278. *****************************************************************************}
  279. {$ifndef cpu64bit}
  280. { 32-bit version }
  281. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  282. var
  283. hregister,
  284. hregisterhi : tregister;
  285. hreg64 : tregister64;
  286. hl : tasmlabel;
  287. oldloc : tlocation;
  288. begin
  289. oldloc:=l;
  290. if dst_size=OS_NO then
  291. internalerror(200309144);
  292. { handle transformations to 64bit separate }
  293. if dst_size in [OS_64,OS_S64] then
  294. begin
  295. if not (l.size in [OS_64,OS_S64]) then
  296. begin
  297. { load a smaller size to OS_64 }
  298. if l.loc=LOC_REGISTER then
  299. begin
  300. hregister:=cg.makeregsize(l.registerlow,OS_32);
  301. cg.a_load_reg_reg(list,l.size,OS_32,l.registerlow,hregister);
  302. end
  303. else
  304. begin
  305. location_release(list,l);
  306. hregister:=cg.getintregister(list,OS_INT);
  307. end;
  308. { load value in low register }
  309. case l.loc of
  310. LOC_FLAGS :
  311. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  312. LOC_JUMP :
  313. begin
  314. cg.a_label(list,truelabel);
  315. cg.a_load_const_reg(list,OS_INT,1,hregister);
  316. objectlibrary.getlabel(hl);
  317. cg.a_jmp_always(list,hl);
  318. cg.a_label(list,falselabel);
  319. cg.a_load_const_reg(list,OS_INT,0,hregister);
  320. cg.a_label(list,hl);
  321. end;
  322. else
  323. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  324. end;
  325. { reset hi part, take care of the signed bit of the current value }
  326. hregisterhi:=cg.getintregister(list,OS_INT);
  327. if (dst_size=OS_S64) and
  328. (l.size in [OS_S8,OS_S16,OS_S32]) then
  329. begin
  330. if l.loc=LOC_CONSTANT then
  331. begin
  332. if (longint(l.value)<0) then
  333. cg.a_load_const_reg(list,OS_32,$ffffffff,hregisterhi)
  334. else
  335. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  336. end
  337. else
  338. begin
  339. cg.a_op_const_reg_reg(list,OP_SAR,OS_32,31,hregister,
  340. hregisterhi);
  341. end;
  342. end
  343. else
  344. cg.a_load_const_reg(list,OS_32,0,hregisterhi);
  345. location_reset(l,LOC_REGISTER,dst_size);
  346. l.registerlow:=hregister;
  347. l.registerhigh:=hregisterhi;
  348. end
  349. else
  350. begin
  351. { 64bit to 64bit }
  352. if (l.loc=LOC_REGISTER) or
  353. ((l.loc=LOC_CREGISTER) and maybeconst) then
  354. begin
  355. hregister:=l.registerlow;
  356. hregisterhi:=l.registerhigh;
  357. end
  358. else
  359. begin
  360. hregister:=cg.getintregister(list,OS_INT);
  361. hregisterhi:=cg.getintregister(list,OS_INT);
  362. location_release(list,l);
  363. end;
  364. hreg64.reglo:=hregister;
  365. hreg64.reghi:=hregisterhi;
  366. { load value in new register }
  367. cg64.a_load64_loc_reg(list,l,hreg64,false);
  368. location_reset(l,LOC_REGISTER,dst_size);
  369. l.registerlow:=hregister;
  370. l.registerhigh:=hregisterhi;
  371. end;
  372. end
  373. else
  374. begin
  375. {Do not bother to recycle the existing register. The register
  376. allocator eliminates unnecessary moves, so it's not needed
  377. and trying to recycle registers can cause problems because
  378. the registers changes size and may need aditional constraints.}
  379. location_release(list,l);
  380. hregister:=cg.getintregister(list,dst_size);
  381. { load value in new register }
  382. case l.loc of
  383. LOC_FLAGS :
  384. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  385. LOC_JUMP :
  386. begin
  387. cg.a_label(list,truelabel);
  388. cg.a_load_const_reg(list,dst_size,1,hregister);
  389. objectlibrary.getlabel(hl);
  390. cg.a_jmp_always(list,hl);
  391. cg.a_label(list,falselabel);
  392. cg.a_load_const_reg(list,dst_size,0,hregister);
  393. cg.a_label(list,hl);
  394. end;
  395. else
  396. begin
  397. { load_loc_reg can only handle size >= l.size, when the
  398. new size is smaller then we need to adjust the size
  399. of the orignal and maybe recalculate l.register for i386 }
  400. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  401. begin
  402. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  403. l.register:=cg.makeregsize(l.register,dst_size);
  404. { for big endian systems, the reference's offset must }
  405. { be increased in this case, since they have the }
  406. { MSB first in memory and e.g. byte(word_var) should }
  407. { return the second byte in this case (JM) }
  408. if (target_info.endian = ENDIAN_BIG) and
  409. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  410. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  411. {$ifdef i386}
  412. l.size:=dst_size;
  413. {$endif i386}
  414. end;
  415. cg.a_load_loc_reg(list,dst_size,l,hregister);
  416. {$ifndef i386}
  417. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  418. l.size:=dst_size;
  419. {$endif not i386}
  420. end;
  421. end;
  422. if (l.loc <> LOC_CREGISTER) or
  423. not maybeconst then
  424. location_reset(l,LOC_REGISTER,dst_size)
  425. else
  426. location_reset(l,LOC_CREGISTER,dst_size);
  427. l.register:=hregister;
  428. end;
  429. { Release temp when it was a reference }
  430. if oldloc.loc=LOC_REFERENCE then
  431. location_freetemp(list,oldloc);
  432. end;
  433. {$else cpu64bit}
  434. { 64-bit version }
  435. procedure location_force_reg(list:TAAsmoutput;var l:tlocation;dst_size:TCGSize;maybeconst:boolean);
  436. var
  437. hregister : tregister;
  438. hl : tasmlabel;
  439. oldloc : tlocation;
  440. begin
  441. oldloc:=l;
  442. if dst_size=OS_NO then
  443. internalerror(200309144);
  444. { handle transformations to 64bit separate }
  445. if dst_size in [OS_64,OS_S64] then
  446. begin
  447. { load a smaller size to OS_64 }
  448. if l.loc=LOC_REGISTER then
  449. hregister:=cg.makeregsize(l.register,OS_INT)
  450. else
  451. begin
  452. location_release(list,l);
  453. hregister:=cg.getregisterint(list,OS_INT);
  454. end;
  455. { load value in low register }
  456. case l.loc of
  457. {$ifdef cpuflags}
  458. LOC_FLAGS :
  459. cg.g_flags2reg(list,OS_INT,l.resflags,hregister);
  460. {$endif cpuflags}
  461. LOC_JUMP :
  462. begin
  463. cg.a_label(list,truelabel);
  464. cg.a_load_const_reg(list,OS_INT,1,hregister);
  465. objectlibrary.getlabel(hl);
  466. cg.a_jmp_always(list,hl);
  467. cg.a_label(list,falselabel);
  468. cg.a_load_const_reg(list,OS_INT,0,hregister);
  469. cg.a_label(list,hl);
  470. end;
  471. else
  472. cg.a_load_loc_reg(list,OS_INT,l,hregister);
  473. end;
  474. location_reset(l,LOC_REGISTER,dst_size);
  475. l.register:=hregister;
  476. end
  477. else
  478. begin
  479. { transformations to 32bit or smaller }
  480. if l.loc=LOC_REGISTER then
  481. begin
  482. hregister:=l.register;
  483. end
  484. else
  485. begin
  486. { get new register }
  487. if (l.loc=LOC_CREGISTER) and
  488. maybeconst and
  489. (TCGSize2Size[dst_size]=TCGSize2Size[l.size]) then
  490. hregister:=l.register
  491. else
  492. begin
  493. location_release(list,l);
  494. hregister:=cg.getregisterint(list,OS_INT);
  495. end;
  496. end;
  497. hregister:=cg.makeregsize(hregister,dst_size);
  498. { load value in new register }
  499. case l.loc of
  500. {$ifdef cpuflags}
  501. LOC_FLAGS :
  502. cg.g_flags2reg(list,dst_size,l.resflags,hregister);
  503. {$endif cpuflags}
  504. LOC_JUMP :
  505. begin
  506. cg.a_label(list,truelabel);
  507. cg.a_load_const_reg(list,dst_size,1,hregister);
  508. objectlibrary.getlabel(hl);
  509. cg.a_jmp_always(list,hl);
  510. cg.a_label(list,falselabel);
  511. cg.a_load_const_reg(list,dst_size,0,hregister);
  512. cg.a_label(list,hl);
  513. end;
  514. else
  515. begin
  516. { load_loc_reg can only handle size >= l.size, when the
  517. new size is smaller then we need to adjust the size
  518. of the orignal and maybe recalculate l.register for i386 }
  519. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  520. begin
  521. if (l.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  522. l.register:=cg.makeregsize(l.register,dst_size);
  523. { for big endian systems, the reference's offset must }
  524. { be increased in this case, since they have the }
  525. { MSB first in memory and e.g. byte(word_var) should }
  526. { return the second byte in this case (JM) }
  527. if (target_info.endian = ENDIAN_BIG) and
  528. (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  529. inc(l.reference.offset,TCGSize2Size[l.size]-TCGSize2Size[dst_size]);
  530. {$ifdef i386}
  531. l.size:=dst_size;
  532. {$endif i386}
  533. end;
  534. cg.a_load_loc_reg(list,dst_size,l,hregister);
  535. {$ifndef i386}
  536. if (TCGSize2Size[dst_size]<TCGSize2Size[l.size]) then
  537. l.size:=dst_size;
  538. {$endif not i386}
  539. end;
  540. end;
  541. location_reset(l,LOC_REGISTER,dst_size);
  542. l.register:=hregister;
  543. end;
  544. { Release temp when it was a reference }
  545. if oldloc.loc=LOC_REFERENCE then
  546. location_freetemp(list,oldloc);
  547. end;
  548. {$endif cpu64bit}
  549. procedure location_force_fpureg(list:TAAsmoutput;var l: tlocation;maybeconst:boolean);
  550. var
  551. reg : tregister;
  552. begin
  553. if (l.loc<>LOC_FPUREGISTER) and
  554. ((l.loc<>LOC_CFPUREGISTER) or (not maybeconst)) then
  555. begin
  556. reg:=cg.getfpuregister(list,l.size);
  557. cg.a_loadfpu_loc_reg(list,l,reg);
  558. location_freetemp(list,l);
  559. location_release(list,l);
  560. location_reset(l,LOC_FPUREGISTER,l.size);
  561. l.register:=reg;
  562. end;
  563. end;
  564. procedure location_force_mem(list:TAAsmoutput;var l:tlocation);
  565. var
  566. r : treference;
  567. begin
  568. case l.loc of
  569. LOC_FPUREGISTER,
  570. LOC_CFPUREGISTER :
  571. begin
  572. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  573. cg.a_loadfpu_reg_ref(list,l.size,l.register,r);
  574. location_release(list,l);
  575. location_reset(l,LOC_REFERENCE,l.size);
  576. l.reference:=r;
  577. end;
  578. LOC_CONSTANT,
  579. LOC_REGISTER,
  580. LOC_CREGISTER :
  581. begin
  582. tg.GetTemp(list,TCGSize2Size[l.size],tt_normal,r);
  583. if l.size in [OS_64,OS_S64] then
  584. begin
  585. cg64.a_load64_loc_ref(list,l,r);
  586. location_release(list,l);
  587. end
  588. else
  589. begin
  590. location_release(list,l);
  591. cg.a_load_loc_ref(list,l.size,l,r);
  592. end;
  593. location_reset(l,LOC_REFERENCE,l.size);
  594. l.reference:=r;
  595. end;
  596. LOC_CREFERENCE,
  597. LOC_REFERENCE : ;
  598. else
  599. internalerror(200203219);
  600. end;
  601. end;
  602. {*****************************************************************************
  603. Maybe_Save
  604. *****************************************************************************}
  605. function maybe_pushfpu(list:taasmoutput;needed : byte;var l:tlocation) : boolean;
  606. begin
  607. {$ifdef i386}
  608. if (needed>=maxfpuregs) and
  609. (l.loc = LOC_FPUREGISTER) then
  610. begin
  611. location_force_mem(list,l);
  612. maybe_pushfpu:=true;
  613. end
  614. else
  615. maybe_pushfpu:=false;
  616. {$endif i386}
  617. end;
  618. {****************************************************************************
  619. Init/Finalize Code
  620. ****************************************************************************}
  621. procedure copyvalueparas(p : tnamedindexitem;arg:pointer);
  622. var
  623. href1,href2 : treference;
  624. list:TAAsmoutput;
  625. hsym : tvarsym;
  626. l : longint;
  627. loadref : boolean;
  628. localcopyloc : tparalocation;
  629. begin
  630. list:=taasmoutput(arg);
  631. if (tsym(p).typ=varsym) and
  632. (tvarsym(p).varspez=vs_value) and
  633. (paramanager.push_addr_param(tvarsym(p).varspez,tvarsym(p).vartype.def,current_procinfo.procdef.proccalloption)) then
  634. begin
  635. loadref:=true;
  636. case tvarsym(p).localloc.loc of
  637. LOC_REGISTER :
  638. begin
  639. reference_reset_base(href1,tvarsym(p).localloc.register,0);
  640. loadref:=false;
  641. end;
  642. LOC_REFERENCE :
  643. reference_reset_base(href1,tvarsym(p).localloc.reference.index,
  644. tvarsym(p).localloc.reference.offset);
  645. else
  646. internalerror(200309181);
  647. end;
  648. if is_open_array(tvarsym(p).vartype.def) or
  649. is_array_of_const(tvarsym(p).vartype.def) then
  650. begin
  651. { cdecl functions don't have a high pointer so it is not possible to generate
  652. a local copy }
  653. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  654. begin
  655. hsym:=tvarsym(tsym(p).owner.search('high'+p.name));
  656. if not assigned(hsym) then
  657. internalerror(200306061);
  658. case hsym.localloc.loc of
  659. LOC_REFERENCE :
  660. begin
  661. { this code is only executed before the code for the body and the entry/exit code is generated
  662. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  663. }
  664. include(current_procinfo.flags,pi_do_call);
  665. reference_reset_base(href2,hsym.localloc.reference.index,hsym.localloc.reference.offset);
  666. cg.g_copyvaluepara_openarray(list,href1,href2,tarraydef(tvarsym(p).vartype.def).elesize)
  667. end
  668. else
  669. internalerror(200309182);
  670. end;
  671. end;
  672. end
  673. else
  674. begin
  675. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  676. internalerror(200309183);
  677. { Allocate space for the local copy }
  678. l:=tvarsym(p).getvaluesize;
  679. localcopyloc.loc:=LOC_REFERENCE;
  680. localcopyloc.size:=int_cgsize(l);
  681. tg.GetLocal(list,l,localcopyloc.reference);
  682. { Copy data }
  683. reference_reset_base(href2,localcopyloc.reference.index,localcopyloc.reference.offset);
  684. if is_shortstring(tvarsym(p).vartype.def) then
  685. begin
  686. { this code is only executed before the code for the body and the entry/exit code is generated
  687. so we're allowed to include pi_do_call here; after pass1 is run, this isn't allowed anymore
  688. }
  689. include(current_procinfo.flags,pi_do_call);
  690. cg.g_copyshortstring(list,href1,href2,tstringdef(tvarsym(p).vartype.def).len,false,loadref)
  691. end
  692. else
  693. cg.g_concatcopy(list,href1,href2,tvarsym(p).vartype.def.size,true,loadref);
  694. { update localloc of varsym }
  695. tg.Ungetlocal(list,tvarsym(p).localloc.reference);
  696. tvarsym(p).localloc:=localcopyloc;
  697. end;
  698. end;
  699. end;
  700. { generates the code for initialisation of local data }
  701. procedure initialize_data(p : tnamedindexitem;arg:pointer);
  702. var
  703. href : treference;
  704. list:TAAsmoutput;
  705. begin
  706. list:=taasmoutput(arg);
  707. if (tsym(p).typ=varsym) and
  708. (tvarsym(p).refs>0) and
  709. assigned(tvarsym(p).vartype.def) and
  710. not(is_class(tvarsym(p).vartype.def)) and
  711. tvarsym(p).vartype.def.needs_inittable then
  712. begin
  713. if (cs_implicit_exceptions in aktmoduleswitches) then
  714. include(current_procinfo.flags,pi_needs_implicit_finally);
  715. if tvarsym(p).owner.symtabletype=localsymtable then
  716. begin
  717. case tvarsym(p).localloc.loc of
  718. LOC_REFERENCE :
  719. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  720. else
  721. internalerror(2003091810);
  722. end;
  723. end
  724. else
  725. reference_reset_symbol(href,objectlibrary.newasmsymboldata(tvarsym(p).mangledname),0);
  726. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  727. end;
  728. end;
  729. { generates the code for finalisation of local data }
  730. procedure finalize_data(p : tnamedindexitem;arg:pointer);
  731. var
  732. href : treference;
  733. list:TAAsmoutput;
  734. begin
  735. list:=taasmoutput(arg);
  736. case tsym(p).typ of
  737. varsym :
  738. begin
  739. if (tvarsym(p).refs>0) and
  740. not(vo_is_funcret in tvarsym(p).varoptions) and
  741. assigned(tvarsym(p).vartype.def) and
  742. not(is_class(tvarsym(p).vartype.def)) and
  743. tvarsym(p).vartype.def.needs_inittable then
  744. begin
  745. if tvarsym(p).owner.symtabletype=localsymtable then
  746. begin
  747. case tvarsym(p).localloc.loc of
  748. LOC_REFERENCE :
  749. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  750. else
  751. internalerror(2003091811);
  752. end;
  753. end
  754. else
  755. reference_reset_symbol(href,objectlibrary.newasmsymboldata(tvarsym(p).mangledname),0);
  756. cg.g_finalize(list,tvarsym(p).vartype.def,href,false);
  757. end;
  758. end;
  759. typedconstsym :
  760. begin
  761. if ttypedconstsym(p).is_writable and
  762. ttypedconstsym(p).typedconsttype.def.needs_inittable then
  763. begin
  764. reference_reset_symbol(href,objectlibrary.newasmsymboldata(ttypedconstsym(p).mangledname),0);
  765. cg.g_finalize(list,ttypedconstsym(p).typedconsttype.def,href,false);
  766. end;
  767. end;
  768. end;
  769. end;
  770. { generates the code for incrementing the reference count of parameters and
  771. initialize out parameters }
  772. procedure init_paras(p : tnamedindexitem;arg:pointer);
  773. var
  774. href : treference;
  775. tmpreg : tregister;
  776. list:TAAsmoutput;
  777. begin
  778. list:=taasmoutput(arg);
  779. if (tsym(p).typ=varsym) and
  780. not is_class(tvarsym(p).vartype.def) and
  781. tvarsym(p).vartype.def.needs_inittable then
  782. begin
  783. case tvarsym(p).varspez of
  784. vs_value :
  785. begin
  786. if (cs_implicit_exceptions in aktmoduleswitches) then
  787. include(current_procinfo.flags,pi_needs_implicit_finally);
  788. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  789. internalerror(200309187);
  790. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  791. cg.g_incrrefcount(list,tvarsym(p).vartype.def,href,is_open_array(tvarsym(p).vartype.def));
  792. end;
  793. vs_out :
  794. begin
  795. case tvarsym(p).localloc.loc of
  796. LOC_REFERENCE :
  797. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  798. else
  799. internalerror(2003091810);
  800. end;
  801. tmpreg:=cg.getaddressregister(list);
  802. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,tmpreg);
  803. reference_reset_base(href,tmpreg,0);
  804. cg.g_initialize(list,tvarsym(p).vartype.def,href,false);
  805. cg.ungetregister(list,tmpreg);
  806. end;
  807. end;
  808. end;
  809. end;
  810. { generates the code for decrementing the reference count of parameters }
  811. procedure final_paras(p : tnamedindexitem;arg:pointer);
  812. var
  813. href : treference;
  814. list:TAAsmoutput;
  815. begin
  816. list:=taasmoutput(arg);
  817. if (tsym(p).typ=varsym) and
  818. not is_class(tvarsym(p).vartype.def) and
  819. tvarsym(p).vartype.def.needs_inittable then
  820. begin
  821. if (tvarsym(p).varspez=vs_value) then
  822. begin
  823. if tvarsym(p).localloc.loc<>LOC_REFERENCE then
  824. internalerror(200309188);
  825. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  826. cg.g_decrrefcount(list,tvarsym(p).vartype.def,href,is_open_array(tvarsym(p).vartype.def));
  827. end;
  828. end
  829. else if (tsym(p).typ=varsym) and
  830. (is_open_array(tvarsym(p).vartype.def) or
  831. is_array_of_const(tvarsym(p).vartype.def)) then
  832. begin
  833. { cdecl functions don't have a high pointer so it is not possible to generate
  834. a local copy }
  835. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  836. begin
  837. reference_reset_base(href,tvarsym(p).localloc.reference.index,tvarsym(p).localloc.reference.offset);
  838. cg.g_releasevaluepara_openarray(list,href);
  839. end;
  840. end;
  841. end;
  842. { Initialize temp ansi/widestrings,interfaces }
  843. procedure inittempvariables(list:taasmoutput);
  844. var
  845. hp : ptemprecord;
  846. href : treference;
  847. begin
  848. hp:=tg.templist;
  849. while assigned(hp) do
  850. begin
  851. if assigned(hp^.def) and
  852. hp^.def.needs_inittable then
  853. begin
  854. if (cs_implicit_exceptions in aktmoduleswitches) then
  855. include(current_procinfo.flags,pi_needs_implicit_finally);
  856. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  857. cg.g_initialize(list,hp^.def,href,false);
  858. end;
  859. hp:=hp^.next;
  860. end;
  861. end;
  862. procedure finalizetempvariables(list:taasmoutput);
  863. var
  864. hp : ptemprecord;
  865. href : treference;
  866. paraloc1 : tparalocation;
  867. begin
  868. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  869. hp:=tg.templist;
  870. while assigned(hp) do
  871. begin
  872. if assigned(hp^.def) and
  873. hp^.def.needs_inittable then
  874. begin
  875. reference_reset_base(href,current_procinfo.framepointer,hp^.pos);
  876. cg.g_finalize(list,hp^.def,href,false);
  877. end;
  878. hp:=hp^.next;
  879. end;
  880. end;
  881. procedure gen_load_return_value(list:TAAsmoutput; var uses_acc,uses_acchi,uses_fpu : boolean);
  882. var
  883. ressym : tvarsym;
  884. resloc : tlocation;
  885. href : treference;
  886. hreg : tregister;
  887. begin
  888. { Is the loading needed? }
  889. if is_void(current_procinfo.procdef.rettype.def) or
  890. (
  891. (po_assembler in current_procinfo.procdef.procoptions) and
  892. (not(assigned(current_procinfo.procdef.funcretsym)) or
  893. (tvarsym(current_procinfo.procdef.funcretsym).refs=0))
  894. ) then
  895. exit;
  896. { Constructors need to return self }
  897. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  898. begin
  899. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  900. { return the self pointer }
  901. ressym:=tvarsym(current_procinfo.procdef.parast.search('self'));
  902. if not assigned(ressym) then
  903. internalerror(200305058);
  904. case ressym.localloc.loc of
  905. LOC_REFERENCE :
  906. reference_reset_base(href,ressym.localloc.reference.index,ressym.localloc.reference.offset);
  907. else
  908. internalerror(2003091810);
  909. end;
  910. cg.ungetregister(list,NR_FUNCTION_RETURN_REG);
  911. // for the optimizer
  912. cg.a_reg_alloc(list,NR_FUNCTION_RETURN_REG);
  913. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_FUNCTION_RETURN_REG);
  914. uses_acc:=true;
  915. exit;
  916. end;
  917. ressym := tvarsym(current_procinfo.procdef.funcretsym);
  918. if (ressym.refs>0) then
  919. begin
  920. case ressym.localloc.loc of
  921. LOC_FPUREGISTER,
  922. LOC_REGISTER :
  923. begin
  924. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  925. location_reset(resloc,LOC_CREGISTER,OS_ADDR)
  926. else
  927. if ressym.vartype.def.deftype = floatdef then
  928. location_reset(resloc,LOC_CFPUREGISTER,def_cgsize(current_procinfo.procdef.rettype.def))
  929. else
  930. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  931. resloc.register:=ressym.localloc.register;
  932. end;
  933. LOC_REFERENCE :
  934. begin
  935. location_reset(resloc,LOC_REFERENCE,def_cgsize(current_procinfo.procdef.rettype.def));
  936. reference_reset_base(resloc.reference,ressym.localloc.reference.index,ressym.localloc.reference.offset);
  937. end;
  938. else
  939. internalerror(200309184);
  940. end;
  941. { Here, we return the function result. In most architectures, the value is
  942. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  943. function returns in a register and the caller receives it in an other one }
  944. case current_procinfo.procdef.rettype.def.deftype of
  945. orddef,
  946. enumdef :
  947. begin
  948. uses_acc:=true;
  949. {$ifndef cpu64bit}
  950. if resloc.size in [OS_64,OS_S64] then
  951. begin
  952. uses_acchi:=true;
  953. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  954. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  955. cg.ungetregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  956. cg.ungetregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  957. // for the optimizer
  958. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_LOW_REG);
  959. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_HIGH_REG);
  960. cg64.a_load64_loc_reg(list,resloc,joinreg64(NR_FUNCTION_RETURN64_LOW_REG,
  961. NR_FUNCTION_RETURN64_HIGH_REG),false);
  962. end
  963. else
  964. {$endif cpu64bit}
  965. begin
  966. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  967. hreg:=cg.makeregsize(NR_FUNCTION_RETURN_REG,resloc.size);
  968. cg.ungetregister(list,hreg);
  969. // for the optimizer
  970. cg.a_reg_alloc(list,NR_FUNCTION_RETURN_REG);
  971. cg.a_load_loc_reg(list,resloc.size,resloc,hreg);
  972. end;
  973. end;
  974. floatdef :
  975. begin
  976. uses_fpu := true;
  977. {$ifdef cpufpemu}
  978. if cs_fp_emulation in aktmoduleswitches then
  979. cg.a_loadfpu_loc_reg(list,resloc,NR_FUNCTION_RETURN_REG);
  980. else
  981. {$endif cpufpemu}
  982. cg.a_loadfpu_loc_reg(list,resloc,NR_FPU_RESULT_REG);
  983. end;
  984. else
  985. begin
  986. if not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  987. begin
  988. uses_acc:=true;
  989. {$ifndef cpu64bit}
  990. { Win32 can return records in EAX:EDX }
  991. if resloc.size in [OS_64,OS_S64] then
  992. begin
  993. uses_acchi:=true;
  994. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  995. cg.getexplicitregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  996. cg.ungetregister(list,NR_FUNCTION_RETURN64_LOW_REG);
  997. cg.ungetregister(list,NR_FUNCTION_RETURN64_HIGH_REG);
  998. // for the optimizer
  999. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_LOW_REG);
  1000. cg.a_reg_alloc(list,NR_FUNCTION_RETURN64_HIGH_REG);
  1001. cg64.a_load64_loc_reg(list,resloc,joinreg64(NR_FUNCTION_RETURN64_LOW_REG,
  1002. NR_FUNCTION_RETURN64_HIGH_REG),false);
  1003. end
  1004. else
  1005. {$endif cpu64bit}
  1006. begin
  1007. cg.getexplicitregister(list,NR_FUNCTION_RETURN_REG);
  1008. hreg:=cg.makeregsize(NR_FUNCTION_RETURN_REG,resloc.size);
  1009. cg.ungetregister(list,hreg);
  1010. // for the optimizer
  1011. cg.a_reg_alloc(list,NR_FUNCTION_RETURN_REG);
  1012. cg.a_load_loc_reg(list,resloc.size,resloc,hreg);
  1013. end;
  1014. end
  1015. end;
  1016. end;
  1017. end;
  1018. end;
  1019. procedure gen_load_para_value(list:TAAsmoutput);
  1020. var
  1021. hp : tparaitem;
  1022. href : treference;
  1023. gotregvarparas : boolean;
  1024. begin
  1025. { Store register parameters in reference or in register variable }
  1026. if assigned(current_procinfo.procdef.parast) and
  1027. not (po_assembler in current_procinfo.procdef.procoptions) then
  1028. begin
  1029. { move register parameters which aren't regable into memory }
  1030. { we do this before init_paras because that one calls routines which may overwrite these }
  1031. { registers and it also expects the values to be in memory }
  1032. hp:=tparaitem(current_procinfo.procdef.para.first);
  1033. gotregvarparas := false;
  1034. while assigned(hp) do
  1035. begin
  1036. case tvarsym(hp.parasym).localloc.loc of
  1037. LOC_REGISTER :
  1038. begin
  1039. gotregvarparas := true;
  1040. { cg.a_load_param_reg will first allocate and then deallocate paraloc }
  1041. { register (if the parameter resides in a register) and then allocate }
  1042. { the regvar (which is currently not allocated) }
  1043. cg.a_loadany_param_reg(list,hp.paraloc[calleeside],tvarsym(hp.parasym).localloc.register,nil);
  1044. end;
  1045. LOC_FPUREGISTER:
  1046. begin
  1047. gotregvarparas := true;
  1048. { cg.a_load_param_reg will first allocate and then deallocate paraloc }
  1049. { register (if the parameter resides in a register) and then allocate }
  1050. { the regvar (which is currently not allocated) }
  1051. cg.a_loadany_param_reg(list,hp.paraloc[calleeside],tvarsym(hp.parasym).localloc.register,nil);
  1052. end;
  1053. LOC_REFERENCE :
  1054. begin
  1055. if hp.paraloc[calleeside].loc<>LOC_REFERENCE then
  1056. begin
  1057. if getsupreg(hp.paraloc[calleeside].register)<first_int_imreg then
  1058. begin
  1059. {$ifndef cpu64bit}
  1060. if (hp.paraloc[calleeside].size in [OS_S64,OS_64]) then
  1061. begin
  1062. cg.getexplicitregister(list,hp.paraloc[calleeside].registerlow);
  1063. cg.getexplicitregister(list,hp.paraloc[calleeside].registerhigh);
  1064. end
  1065. else
  1066. {$endif cpu64bit}
  1067. cg.getexplicitregister(list,hp.paraloc[calleeside].register);
  1068. end;
  1069. { Release parameter register }
  1070. {$ifndef cpu64bit}
  1071. if (hp.paraloc[calleeside].size in [OS_S64,OS_64]) then
  1072. begin
  1073. cg.ungetregister(list,hp.paraloc[calleeside].registerlow);
  1074. cg.ungetregister(list,hp.paraloc[calleeside].registerhigh);
  1075. end
  1076. else
  1077. {$endif cpu64bit}
  1078. cg.ungetregister(list,hp.paraloc[calleeside].register);
  1079. reference_reset_base(href,tvarsym(hp.parasym).localloc.reference.index,tvarsym(hp.parasym).localloc.reference.offset);
  1080. cg.a_loadany_param_ref(list,hp.paraloc[calleeside],href,nil);
  1081. end;
  1082. end;
  1083. else
  1084. internalerror(200309185);
  1085. end;
  1086. hp:=tparaitem(hp.next);
  1087. end;
  1088. if gotregvarparas then
  1089. begin
  1090. { deallocate all register variables again }
  1091. hp:=tparaitem(current_procinfo.procdef.para.first);
  1092. while assigned(hp) do
  1093. begin
  1094. if (tvarsym(hp.parasym).localloc.loc=LOC_REGISTER) then
  1095. cg.ungetregister(list,tvarsym(hp.parasym).localloc.register);
  1096. hp:=tparaitem(hp.next);
  1097. end;
  1098. end;
  1099. end;
  1100. { generate copies of call by value parameters, must be done before
  1101. the initialization and body is parsed because the refcounts are
  1102. incremented using the local copies }
  1103. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1104. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1105. end;
  1106. procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
  1107. begin
  1108. { initialize local data like ansistrings }
  1109. case current_procinfo.procdef.proctypeoption of
  1110. potype_unitinit:
  1111. begin
  1112. { this is also used for initialization of variables in a
  1113. program which does not have a globalsymtable }
  1114. if assigned(current_module.globalsymtable) then
  1115. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1116. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1117. end;
  1118. { units have seperate code for initilization and finalization }
  1119. potype_unitfinalize: ;
  1120. { program init/final is generated in separate procedure }
  1121. potype_proginit: ;
  1122. else
  1123. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1124. end;
  1125. { initialisizes temp. ansi/wide string data }
  1126. inittempvariables(list);
  1127. { initialize ansi/widesstring para's }
  1128. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1129. {$ifdef OLDREGVARS}
  1130. load_regvars(list,nil);
  1131. {$endif OLDREGVARS}
  1132. end;
  1133. procedure gen_finalize_code(list:TAAsmoutput;inlined:boolean);
  1134. begin
  1135. {$ifdef OLDREGVARS}
  1136. cleanup_regvars(list);
  1137. {$endif OLDREGVARS}
  1138. { finalize temporary data }
  1139. finalizetempvariables(list);
  1140. { finalize local data like ansistrings}
  1141. case current_procinfo.procdef.proctypeoption of
  1142. potype_unitfinalize:
  1143. begin
  1144. { this is also used for initialization of variables in a
  1145. program which does not have a globalsymtable }
  1146. if assigned(current_module.globalsymtable) then
  1147. tsymtable(current_module.globalsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1148. tsymtable(current_module.localsymtable).foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1149. end;
  1150. { units/progs have separate code for initialization and finalization }
  1151. potype_unitinit: ;
  1152. { program init/final is generated in separate procedure }
  1153. potype_proginit: ;
  1154. else
  1155. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1156. end;
  1157. { finalize paras data }
  1158. if assigned(current_procinfo.procdef.parast) then
  1159. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1160. end;
  1161. procedure gen_entry_code(list:TAAsmoutput);
  1162. var
  1163. href : treference;
  1164. paraloc1,
  1165. paraloc2 : tparalocation;
  1166. begin
  1167. { the actual profile code can clobber some registers,
  1168. therefore if the context must be saved, do it before
  1169. the actual call to the profile code
  1170. }
  1171. if (cs_profile in aktmoduleswitches) and
  1172. not(po_assembler in current_procinfo.procdef.procoptions) then
  1173. begin
  1174. { non-win32 can call mcout even in main }
  1175. if not (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1176. cg.g_profilecode(list)
  1177. else
  1178. { wdosx, and win32 should not call mcount before monstartup has been called }
  1179. if not (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1180. cg.g_profilecode(list);
  1181. end;
  1182. { call startup helpers from main program }
  1183. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1184. begin
  1185. { initialize profiling for win32 }
  1186. if (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1187. (cs_profile in aktmoduleswitches) then
  1188. begin
  1189. reference_reset_symbol(href,objectlibrary.newasmsymboldata('etext'),0);
  1190. paraloc1:=paramanager.getintparaloc(pocall_default,1);
  1191. paraloc2:=paramanager.getintparaloc(pocall_default,2);
  1192. paramanager.allocparaloc(list,paraloc2);
  1193. cg.a_paramaddr_ref(list,href,paraloc2);
  1194. reference_reset_symbol(href,objectlibrary.newasmsymboldata('__image_base__'),0);
  1195. paramanager.allocparaloc(list,paraloc1);
  1196. cg.a_paramaddr_ref(list,href,paraloc1);
  1197. paramanager.freeparaloc(list,paraloc2);
  1198. paramanager.freeparaloc(list,paraloc1);
  1199. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1200. cg.a_call_name(list,'_monstartup');
  1201. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_cdecl));
  1202. end;
  1203. { initialize units }
  1204. cg.allocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1205. cg.a_call_name(list,'FPC_INITIALIZEUNITS');
  1206. cg.deallocexplicitregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1207. end;
  1208. {$ifdef GDB}
  1209. if (cs_debuginfo in aktmoduleswitches) then
  1210. list.concat(Tai_force_line.Create);
  1211. {$endif GDB}
  1212. {$ifdef OLDREGVARS}
  1213. load_regvars(list,nil);
  1214. {$endif OLDREGVARS}
  1215. end;
  1216. procedure gen_exit_code(list:TAAsmoutput);
  1217. begin
  1218. { call __EXIT for main program }
  1219. if (not DLLsource) and
  1220. (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1221. cg.a_call_name(list,'FPC_DO_EXIT');
  1222. end;
  1223. {****************************************************************************
  1224. Entry/Exit
  1225. ****************************************************************************}
  1226. procedure gen_proc_symbol(list:Taasmoutput);
  1227. var
  1228. hs : string;
  1229. begin
  1230. { add symbol entry point as well as debug information }
  1231. { will be inserted in front of the rest of this list. }
  1232. { Insert alignment and assembler names }
  1233. { Align, gprof uses 16 byte granularity }
  1234. if (cs_profile in aktmoduleswitches) then
  1235. list.concat(Tai_align.create(16))
  1236. else
  1237. list.concat(Tai_align.create(aktalignment.procalign));
  1238. {$ifdef GDB}
  1239. if (cs_debuginfo in aktmoduleswitches) then
  1240. begin
  1241. if (po_public in current_procinfo.procdef.procoptions) then
  1242. Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
  1243. current_procinfo.procdef.concatstabto(list);
  1244. Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
  1245. end;
  1246. {$endif GDB}
  1247. repeat
  1248. hs:=current_procinfo.procdef.aliasnames.getfirst;
  1249. if hs='' then
  1250. break;
  1251. {$ifdef GDB}
  1252. if (cs_debuginfo in aktmoduleswitches) and
  1253. target_info.use_function_relative_addresses then
  1254. list.concat(Tai_stab_function_name.create(strpnew(hs)));
  1255. {$endif GDB}
  1256. if (cs_profile in aktmoduleswitches) or
  1257. (po_public in current_procinfo.procdef.procoptions) then
  1258. list.concat(Tai_symbol.createname_global(hs,0))
  1259. else
  1260. list.concat(Tai_symbol.createname(hs,0));
  1261. until false;
  1262. end;
  1263. procedure gen_proc_symbol_end(list:Taasmoutput);
  1264. {$ifdef GDB}
  1265. var
  1266. stabsendlabel : tasmlabel;
  1267. mangled_length : longint;
  1268. p : pchar;
  1269. {$endif GDB}
  1270. begin
  1271. list.concat(Tai_symbol_end.Createname(current_procinfo.procdef.mangledname));
  1272. {$ifdef GDB}
  1273. if (cs_debuginfo in aktmoduleswitches) then
  1274. begin
  1275. objectlibrary.getlabel(stabsendlabel);
  1276. cg.a_label(list,stabsendlabel);
  1277. { define calling EBP as pseudo local var PM }
  1278. { this enables test if the function is a local one !! }
  1279. {if assigned(current_procinfo.parent) and
  1280. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  1281. list.concat(Tai_stabs.Create(strpnew(
  1282. '"parent_ebp:'+tstoreddef(voidpointertype.def).numberstring+'",'+
  1283. tostr(N_LSYM)+',0,0,'+tostr(current_procinfo.parent_framepointer_offset)))); }
  1284. if (not is_void(current_procinfo.procdef.rettype.def)) and
  1285. (tvarsym(current_procinfo.procdef.funcretsym).refs>0) then
  1286. begin
  1287. if tvarsym(current_procinfo.procdef.funcretsym).localloc.loc<>LOC_REFERENCE then
  1288. internalerror(2003091812);
  1289. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1290. begin
  1291. list.concat(Tai_stabs.Create(strpnew(
  1292. '"'+current_procinfo.procdef.procsym.name+':X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1293. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1294. if (m_result in aktmodeswitches) then
  1295. list.concat(Tai_stabs.Create(strpnew(
  1296. '"RESULT:X*'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1297. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))))
  1298. end
  1299. else
  1300. begin
  1301. list.concat(Tai_stabs.Create(strpnew(
  1302. '"'+current_procinfo.procdef.procsym.name+':X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1303. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1304. if (m_result in aktmodeswitches) then
  1305. list.concat(Tai_stabs.Create(strpnew(
  1306. '"RESULT:X'+tstoreddef(current_procinfo.procdef.rettype.def).numberstring+'",'+
  1307. tostr(N_tsym)+',0,0,'+tostr(tvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset))));
  1308. end;
  1309. end;
  1310. mangled_length:=length(current_procinfo.procdef.mangledname);
  1311. getmem(p,2*mangled_length+50);
  1312. strpcopy(p,'192,0,0,');
  1313. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1314. if (target_info.use_function_relative_addresses) then
  1315. begin
  1316. strpcopy(strend(p),'-');
  1317. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1318. end;
  1319. list.concat(Tai_stabn.Create(strnew(p)));
  1320. {List.concat(Tai_stabn.Create(strpnew('192,0,0,'
  1321. +current_procinfo.procdef.mangledname))));
  1322. p[0]:='2';p[1]:='2';p[2]:='4';
  1323. strpcopy(strend(p),'_end');}
  1324. strpcopy(p,'224,0,0,'+stabsendlabel.name);
  1325. if (target_info.use_function_relative_addresses) then
  1326. begin
  1327. strpcopy(strend(p),'-');
  1328. strpcopy(strend(p),current_procinfo.procdef.mangledname);
  1329. end;
  1330. list.concatlist(withdebuglist);
  1331. list.concat(Tai_stabn.Create(strnew(p)));
  1332. { strpnew('224,0,0,'
  1333. +current_procinfo.procdef.mangledname+'_end'))));}
  1334. freemem(p,2*mangled_length+50);
  1335. end;
  1336. {$endif GDB}
  1337. end;
  1338. procedure gen_stackalloc_code(list:Taasmoutput);
  1339. var
  1340. stackframe : longint;
  1341. begin
  1342. { Calculate size of stackframe }
  1343. stackframe:=current_procinfo.calc_stackframe_size;
  1344. {$ifndef powerpc}
  1345. { at least for the ppc this applies always, so this code isn't usable (FK) }
  1346. { omit stack frame ? }
  1347. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1348. begin
  1349. CGmessage(cg_d_stackframe_omited);
  1350. if stackframe<>0 then
  1351. cg.g_stackpointer_alloc(list,stackframe);
  1352. end
  1353. else
  1354. {$endif powerpc}
  1355. begin
  1356. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1357. cg.g_interrupt_stackframe_entry(list);
  1358. cg.g_stackframe_entry(list,stackframe);
  1359. {Never call stack checking before the standard system unit
  1360. has been initialized.}
  1361. if (cs_check_stack in aktlocalswitches) and (current_procinfo.procdef.proctypeoption<>potype_proginit) then
  1362. cg.g_stackcheck(list,stackframe);
  1363. end;
  1364. end;
  1365. procedure gen_stackfree_code(list:Taasmoutput;usesacc,usesacchi:boolean);
  1366. var
  1367. stacksize,
  1368. retsize : longint;
  1369. begin
  1370. {$ifndef powerpc}
  1371. { remove stackframe }
  1372. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1373. begin
  1374. stacksize:=current_procinfo.calc_stackframe_size;
  1375. if (stacksize<>0) then
  1376. cg.a_op_const_reg(list,OP_ADD,OS_32,stacksize,current_procinfo.framepointer);
  1377. end
  1378. else
  1379. cg.g_restore_frame_pointer(list);
  1380. {$endif}
  1381. { at last, the return is generated }
  1382. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1383. cg.g_interrupt_stackframe_exit(list,usesacc,usesacchi)
  1384. else
  1385. begin
  1386. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1387. begin
  1388. retsize:=0;
  1389. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1390. inc(retsize,POINTER_SIZE);
  1391. end
  1392. else
  1393. retsize:=current_procinfo.para_stack_size;
  1394. cg.g_return_from_proc(list,retsize);
  1395. end;
  1396. end;
  1397. procedure gen_save_used_regs(list:TAAsmoutput);
  1398. begin
  1399. { Pure assembler routines need to save the registers themselves }
  1400. if (po_assembler in current_procinfo.procdef.procoptions) then
  1401. exit;
  1402. { for the save all registers we can simply use a pusha,popa which
  1403. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1404. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1405. cg.g_save_all_registers(list)
  1406. else
  1407. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1408. cg.g_save_standard_registers(list);
  1409. end;
  1410. procedure gen_restore_used_regs(list:TAAsmoutput;usesacc,usesacchi,usesfpu:boolean);
  1411. begin
  1412. { Pure assembler routines need to save the registers themselves }
  1413. if (po_assembler in current_procinfo.procdef.procoptions) then
  1414. exit;
  1415. { for the save all registers we can simply use a pusha,popa which
  1416. push edi,esi,ebp,esp(ignored),ebx,edx,ecx,eax }
  1417. if (po_saveregisters in current_procinfo.procdef.procoptions) then
  1418. cg.g_restore_all_registers(list,usesacc,usesacchi)
  1419. else
  1420. if current_procinfo.procdef.proccalloption in savestdregs_pocalls then
  1421. cg.g_restore_standard_registers(list);
  1422. end;
  1423. {****************************************************************************
  1424. Inlining
  1425. ****************************************************************************}
  1426. (*
  1427. procedure load_inlined_return_value(list:TAAsmoutput);
  1428. var
  1429. ressym: tvarsym;
  1430. resloc: tlocation;
  1431. r,r2 : tregister;
  1432. begin
  1433. if not is_void(current_procinfo.procdef.rettype.def) then
  1434. begin
  1435. ressym := tvarsym(current_procinfo.procdef.funcretsym);
  1436. if ressym.reg.enum <> R_NO then
  1437. begin
  1438. if paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1439. location_reset(resloc,LOC_CREGISTER,OS_ADDR)
  1440. else
  1441. if ressym.vartype.def.deftype = floatdef then
  1442. location_reset(resloc,LOC_CFPUREGISTER,def_cgsize(current_procinfo.procdef.rettype.def))
  1443. else
  1444. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1445. resloc.register := ressym.reg;
  1446. end
  1447. else
  1448. begin
  1449. location_reset(resloc,LOC_CREGISTER,def_cgsize(current_procinfo.procdef.rettype.def));
  1450. reference_reset_base(resloc.reference,current_procinfo.framepointer,tvarsym(current_procinfo.procdef.funcretsym).adjusted_address);
  1451. end;
  1452. { Here, we return the function result. In most architectures, the value is
  1453. passed into the FUNCTION_RETURN_REG, but in a windowed architecure like sparc a
  1454. function returns in a register and the caller receives it in an other one }
  1455. case current_procinfo.procdef.rettype.def.deftype of
  1456. orddef,
  1457. enumdef :
  1458. begin
  1459. {$ifndef cpu64bit}
  1460. if resloc.size in [OS_64,OS_S64] then
  1461. begin
  1462. r:=cg.getregisterint(list,OS_INT);
  1463. r2:=cg.getregisterint(list,OS_INT);
  1464. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1465. end
  1466. else
  1467. {$endif cpu64bit}
  1468. begin
  1469. r:=cg.getregisterint(list,resloc.size);
  1470. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1471. end;
  1472. end;
  1473. floatdef :
  1474. begin
  1475. {$ifdef cpufpemu}
  1476. if cs_fp_emulation in aktmoduleswitches then
  1477. r.enum := FUNCTION_RETURN_REG
  1478. else
  1479. {$endif cpufpemu}
  1480. r.enum:=FPU_RESULT_REG;
  1481. cg.a_loadfpu_loc_reg(list,resloc,r);
  1482. end;
  1483. else
  1484. begin
  1485. if not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption) then
  1486. begin
  1487. {$ifndef cpu64bit}
  1488. { Win32 can return records in EAX:EDX }
  1489. if resloc.size in [OS_64,OS_S64] then
  1490. begin
  1491. r:=cg.getregisterint(list,OS_INT);
  1492. r2:=cg.getregisterint(list,OS_INT);
  1493. cg64.a_load64_loc_reg(list,resloc,joinreg64(r,r2),false);
  1494. end
  1495. else
  1496. {$endif cpu64bit}
  1497. begin
  1498. r:=cg.getregisterint(list,resloc.size);
  1499. cg.a_load_loc_reg(list,resloc.size,resloc,r);
  1500. end;
  1501. end
  1502. end;
  1503. end;
  1504. end;
  1505. end;
  1506. procedure geninlineentrycode(list:TAAsmoutput;stackframe:longint);
  1507. begin
  1508. { initialize return value }
  1509. initretvalue(list);
  1510. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}initialize_data,list);
  1511. { initialisizes temp. ansi/wide string data }
  1512. inittempvariables(list);
  1513. { initialize ansi/widesstring para's }
  1514. if assigned(current_procinfo.procdef.parast) then
  1515. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}init_paras,list);
  1516. { generate copies of call by value parameters }
  1517. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1518. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}copyvalueparas,list);
  1519. load_regvars(list,nil);
  1520. end;
  1521. procedure geninlineexitcode(list:TAAsmoutput;inlined:boolean);
  1522. var
  1523. usesacc,
  1524. usesacchi,
  1525. usesfpu : boolean;
  1526. begin
  1527. if aktexitlabel.is_used then
  1528. cg.a_label(list,aktexitlabel);
  1529. cleanup_regvars(list);
  1530. { finalize temporary data }
  1531. finalizetempvariables(list);
  1532. current_procinfo.procdef.localst.foreach_static({$ifndef TP}@{$endif}finalize_data,list);
  1533. { finalize paras data }
  1534. if assigned(current_procinfo.procdef.parast) then
  1535. current_procinfo.procdef.parast.foreach_static({$ifndef TP}@{$endif}final_paras,list);
  1536. { handle return value, this is not done for assembler routines when
  1537. they didn't reference the result variable }
  1538. if not(po_assembler in current_procinfo.procdef.procoptions) or
  1539. (assigned(current_procinfo.procdef.funcretsym) and
  1540. (tvarsym(current_procinfo.procdef.funcretsym).refcount>1)) then
  1541. begin
  1542. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  1543. internalerror(200305263);
  1544. // load_inlined_return_value(list);
  1545. load_return_value(list,usesacc,usesacchi,usesfpu)
  1546. end;
  1547. cleanup_regvars(list);
  1548. end;
  1549. *)
  1550. {****************************************************************************
  1551. Const Data
  1552. ****************************************************************************}
  1553. procedure insertconstdata(sym : ttypedconstsym);
  1554. { this does not affect the local stack space, since all
  1555. typed constansts and initialized variables are always
  1556. put in the .data / .rodata section
  1557. }
  1558. var
  1559. storefilepos : tfileposinfo;
  1560. curconstsegment : taasmoutput;
  1561. l : longint;
  1562. begin
  1563. storefilepos:=aktfilepos;
  1564. aktfilepos:=sym.fileinfo;
  1565. if sym.is_writable then
  1566. curconstsegment:=datasegment
  1567. else
  1568. curconstsegment:=consts;
  1569. l:=sym.getsize;
  1570. { insert cut for smartlinking or alignment }
  1571. if (cs_create_smart in aktmoduleswitches) then
  1572. curconstSegment.concat(Tai_cut.Create);
  1573. curconstSegment.concat(Tai_align.create(const_align(l)));
  1574. {$ifdef GDB}
  1575. if cs_debuginfo in aktmoduleswitches then
  1576. sym.concatstabto(curconstsegment);
  1577. {$endif GDB}
  1578. if (sym.owner.symtabletype=globalsymtable) or
  1579. (cs_create_smart in aktmoduleswitches) or
  1580. (assigned(current_procinfo) and
  1581. (current_procinfo.procdef.proccalloption=pocall_inline)) or
  1582. DLLSource then
  1583. curconstSegment.concat(Tai_symbol.Createdataname_global(sym.mangledname,l))
  1584. else
  1585. curconstSegment.concat(Tai_symbol.Createdataname(sym.mangledname,l));
  1586. aktfilepos:=storefilepos;
  1587. end;
  1588. procedure insertbssdata(sym : tvarsym);
  1589. var
  1590. l,varalign : longint;
  1591. storefilepos : tfileposinfo;
  1592. begin
  1593. storefilepos:=aktfilepos;
  1594. aktfilepos:=sym.fileinfo;
  1595. l:=sym.getvaluesize;
  1596. if (vo_is_thread_var in sym.varoptions) then
  1597. inc(l,pointer_size);
  1598. varalign:=var_align(l);
  1599. {
  1600. sym.address:=align(datasize,varalign);
  1601. datasize:=tvarsym(sym).address+l;
  1602. }
  1603. { insert cut for smartlinking or alignment }
  1604. if (cs_create_smart in aktmoduleswitches) then
  1605. bssSegment.concat(Tai_cut.Create);
  1606. bssSegment.concat(Tai_align.create(varalign));
  1607. {$ifdef GDB}
  1608. if cs_debuginfo in aktmoduleswitches then
  1609. sym.concatstabto(bsssegment);
  1610. {$endif GDB}
  1611. if (sym.owner.symtabletype=globalsymtable) or
  1612. (cs_create_smart in aktmoduleswitches) or
  1613. DLLSource or
  1614. (assigned(current_procinfo) and
  1615. (current_procinfo.procdef.proccalloption=pocall_inline)) or
  1616. (vo_is_exported in sym.varoptions) or
  1617. (vo_is_C_var in sym.varoptions) then
  1618. bssSegment.concat(Tai_datablock.Create_global(sym.mangledname,l))
  1619. else
  1620. bssSegment.concat(Tai_datablock.Create(sym.mangledname,l));
  1621. aktfilepos:=storefilepos;
  1622. end;
  1623. procedure gen_alloc_localst(list:TAAsmoutput;st:tlocalsymtable);
  1624. var
  1625. sym : tsym;
  1626. begin
  1627. sym:=tsym(st.symindex.first);
  1628. while assigned(sym) do
  1629. begin
  1630. { Only allocate space for referenced locals }
  1631. if (sym.typ=varsym) and
  1632. (tvarsym(sym).refs>0) then
  1633. begin
  1634. with tvarsym(sym) do
  1635. begin
  1636. {$warning TODO Add support for register variables}
  1637. localloc.loc:=LOC_REFERENCE;
  1638. tg.GetLocal(list,getvaluesize,localloc.reference);
  1639. if cs_asm_source in aktglobalswitches then
  1640. list.concat(Tai_comment.Create(strpnew('Local '+realname+' located at '+
  1641. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1642. end;
  1643. end;
  1644. sym:=tsym(sym.indexnext);
  1645. end;
  1646. end;
  1647. procedure gen_free_localst(list:TAAsmoutput;st:tlocalsymtable);
  1648. var
  1649. sym : tsym;
  1650. begin
  1651. sym:=tsym(st.symindex.first);
  1652. while assigned(sym) do
  1653. begin
  1654. if (sym.typ=varsym) and
  1655. (tvarsym(sym).refs>0) then
  1656. begin
  1657. with tvarsym(sym) do
  1658. begin
  1659. { Note: We need to keep the data available in memory
  1660. for the sub procedures that can access local data
  1661. in the parent procedures }
  1662. case localloc.loc of
  1663. LOC_REFERENCE :
  1664. tg.Ungetlocal(list,localloc.reference);
  1665. LOC_REGISTER :
  1666. begin
  1667. {$ifndef cpu64bit}
  1668. if localloc.size in [OS_64,OS_S64] then
  1669. begin
  1670. cg.ungetregister(list,localloc.registerlow);
  1671. cg.ungetregister(list,localloc.registerhigh);
  1672. end
  1673. else
  1674. {$endif cpu64bit}
  1675. cg.ungetregister(list,localloc.register);
  1676. end;
  1677. end;
  1678. end;
  1679. end;
  1680. sym:=tsym(sym.indexnext);
  1681. end;
  1682. end;
  1683. procedure gen_alloc_parast(list:TAAsmoutput;st:tparasymtable);
  1684. var
  1685. sym : tsym;
  1686. begin
  1687. sym:=tsym(st.symindex.first);
  1688. while assigned(sym) do
  1689. begin
  1690. if sym.typ=varsym then
  1691. begin
  1692. with tvarsym(sym) do
  1693. begin
  1694. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1695. begin
  1696. case paraitem.paraloc[calleeside].loc of
  1697. LOC_FPUREGISTER,
  1698. LOC_REGISTER:
  1699. begin
  1700. (*
  1701. if paraitem.paraloc[calleeside].register=NR_NO then
  1702. begin
  1703. paraitem.paraloc[calleeside].loc:=LOC_REGISTER;
  1704. paraitem.paraloc[calleeside].size:=paraitem.paraloc[calleeside].size;
  1705. {$ifndef cpu64bit}
  1706. if paraitem.paraloc[calleeside].size in [OS_64,OS_S64] then
  1707. begin
  1708. paraitem.paraloc[calleeside].registerlow:=cg.getregisterint(list,OS_32);
  1709. paraitem.paraloc[calleeside].registerhigh:=cg.getregisterint(list,OS_32);
  1710. end
  1711. else
  1712. {$endif cpu64bit}
  1713. paraitem.paraloc[calleeside].register:=cg.getregisterint(list,localloc.size);
  1714. end;
  1715. *)
  1716. (*
  1717. {$warning TODO Allocate register paras}
  1718. localloc.loc:=LOC_REGISTER;
  1719. localloc.size:=paraitem.paraloc[calleeside].size;
  1720. {$ifndef cpu64bit}
  1721. if localloc.size in [OS_64,OS_S64] then
  1722. begin
  1723. localloc.registerlow:=cg.getregisterint(list,OS_32);
  1724. localloc.registerhigh:=cg.getregisterint(list,OS_32);
  1725. end
  1726. else
  1727. {$endif cpu64bit}
  1728. localloc.register:=cg.getregisterint(list,localloc.size);
  1729. *)
  1730. localloc.loc:=LOC_REFERENCE;
  1731. localloc.size:=paraitem.paraloc[calleeside].size;
  1732. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1733. end
  1734. else
  1735. localloc:=paraitem.paraloc[calleeside];
  1736. end;
  1737. end
  1738. else
  1739. localloc:=paraitem.paraloc[calleeside];
  1740. if cs_asm_source in aktglobalswitches then
  1741. begin
  1742. case localloc.loc of
  1743. LOC_REFERENCE :
  1744. list.concat(Tai_comment.Create(strpnew('Para '+realname+' located at '+
  1745. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1746. end;
  1747. end;
  1748. end;
  1749. end;
  1750. sym:=tsym(sym.indexnext);
  1751. end;
  1752. end;
  1753. procedure gen_alloc_inline_parast(list:TAAsmoutput;st:tparasymtable);
  1754. var
  1755. sym : tsym;
  1756. begin
  1757. sym:=tsym(st.symindex.first);
  1758. while assigned(sym) do
  1759. begin
  1760. if sym.typ=varsym then
  1761. begin
  1762. with tvarsym(sym) do
  1763. begin
  1764. {$warning TODO Allocate register paras}
  1765. localloc.loc:=LOC_REFERENCE;
  1766. localloc.size:=int_cgsize(paramanager.push_size(vs_value,vartype.def,pocall_inline));
  1767. tg.GetLocal(list,tcgsize2size[localloc.size],localloc.reference);
  1768. if cs_asm_source in aktglobalswitches then
  1769. begin
  1770. case localloc.loc of
  1771. LOC_REFERENCE :
  1772. list.concat(Tai_comment.Create(strpnew('Para '+realname+' allocated at '+
  1773. std_regname(localloc.reference.index)+tostr_with_plus(localloc.reference.offset))));
  1774. end;
  1775. end;
  1776. paraitem.paraloc[callerside]:=localloc;
  1777. paraitem.paraloc[calleeside]:=localloc;
  1778. end;
  1779. end;
  1780. sym:=tsym(sym.indexnext);
  1781. end;
  1782. end;
  1783. procedure gen_free_parast(list:TAAsmoutput;st:tparasymtable);
  1784. var
  1785. sym : tsym;
  1786. begin
  1787. sym:=tsym(st.symindex.first);
  1788. while assigned(sym) do
  1789. begin
  1790. if sym.typ=varsym then
  1791. begin
  1792. with tvarsym(sym) do
  1793. begin
  1794. { Note: We need to keep the data available in memory
  1795. for the sub procedures that can access local data
  1796. in the parent procedures }
  1797. case localloc.loc of
  1798. LOC_REFERENCE :
  1799. tg.UngetLocal(list,localloc.reference);
  1800. LOC_REGISTER :
  1801. begin
  1802. if localloc.register<>paraitem.paraloc[calleeside].register then
  1803. begin
  1804. {$ifndef cpu64bit}
  1805. if localloc.size in [OS_64,OS_S64] then
  1806. begin
  1807. cg.ungetregister(list,localloc.registerlow);
  1808. cg.ungetregister(list,localloc.registerhigh);
  1809. end
  1810. else
  1811. {$endif cpu64bit}
  1812. cg.ungetregister(list,localloc.register);
  1813. end;
  1814. end;
  1815. end;
  1816. end;
  1817. end;
  1818. sym:=tsym(sym.indexnext);
  1819. end;
  1820. end;
  1821. end.
  1822. {
  1823. $Log$
  1824. Revision 1.168 2003-11-22 00:31:25 jonas
  1825. + extra allocations of function result regs for the optimiser
  1826. Revision 1.167 2003/11/11 21:10:12 peter
  1827. * remove temporary stdcall hack
  1828. Revision 1.166 2003/11/10 22:02:52 peter
  1829. * cross unit inlining fixed
  1830. Revision 1.165 2003/11/07 15:58:32 florian
  1831. * Florian's culmutative nr. 1; contains:
  1832. - invalid calling conventions for a certain cpu are rejected
  1833. - arm softfloat calling conventions
  1834. - -Sp for cpu dependend code generation
  1835. - several arm fixes
  1836. - remaining code for value open array paras on heap
  1837. Revision 1.164 2003/11/04 19:03:50 peter
  1838. * fixes for temp type patch
  1839. Revision 1.163 2003/11/04 15:35:13 peter
  1840. * fix for referencecounted temps
  1841. Revision 1.162 2003/10/25 11:34:02 florian
  1842. * fixed compilation of ppc system unit
  1843. Revision 1.161 2003/10/19 01:34:30 florian
  1844. * some ppc stuff fixed
  1845. * memory leak fixed
  1846. Revision 1.160 2003/10/17 15:08:34 peter
  1847. * commented out more obsolete constants
  1848. Revision 1.159 2003/10/17 14:38:32 peter
  1849. * 64k registers supported
  1850. * fixed some memory leaks
  1851. Revision 1.158 2003/10/10 17:48:13 peter
  1852. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1853. * tregisteralloctor renamed to trgobj
  1854. * removed rgobj from a lot of units
  1855. * moved location_* and reference_* to cgobj
  1856. * first things for mmx register allocation
  1857. Revision 1.157 2003/10/09 21:31:37 daniel
  1858. * Register allocator splitted, ans abstract now
  1859. Revision 1.156 2003/10/07 18:18:16 peter
  1860. * fix register calling for assembler procedures
  1861. * fix result loading for assembler procedures
  1862. Revision 1.155 2003/10/07 15:17:07 peter
  1863. * inline supported again, LOC_REFERENCEs are used to pass the
  1864. parameters
  1865. * inlineparasymtable,inlinelocalsymtable removed
  1866. * exitlabel inserting fixed
  1867. Revision 1.154 2003/10/01 20:34:48 peter
  1868. * procinfo unit contains tprocinfo
  1869. * cginfo renamed to cgbase
  1870. * moved cgmessage to verbose
  1871. * fixed ppc and sparc compiles
  1872. Revision 1.153 2003/09/30 21:02:37 peter
  1873. * updates for inlining
  1874. Revision 1.152 2003/09/29 20:58:56 peter
  1875. * optimized releasing of registers
  1876. Revision 1.151 2003/09/28 21:47:18 peter
  1877. * register paras and local copies updates
  1878. Revision 1.150 2003/09/28 17:55:03 peter
  1879. * parent framepointer changed to hidden parameter
  1880. * tloadparentfpnode added
  1881. Revision 1.149 2003/09/28 13:39:38 peter
  1882. * optimized releasing of registers
  1883. Revision 1.148 2003/09/25 21:28:00 peter
  1884. * parameter fixes
  1885. Revision 1.147 2003/09/23 21:03:59 peter
  1886. * check for refs>0 in init/final local data
  1887. Revision 1.146 2003/09/23 17:56:05 peter
  1888. * locals and paras are allocated in the code generation
  1889. * tvarsym.localloc contains the location of para/local when
  1890. generating code for the current procedure
  1891. Revision 1.145 2003/09/16 16:17:01 peter
  1892. * varspez in calls to push_addr_param
  1893. Revision 1.144 2003/09/14 21:33:37 peter
  1894. * location_force_reg gives IE when size=OS_NO
  1895. Revision 1.143 2003/09/14 19:18:10 peter
  1896. * remove obsolete code already in comments
  1897. Revision 1.142 2003/09/11 11:54:59 florian
  1898. * improved arm code generation
  1899. * move some protected and private field around
  1900. * the temp. register for register parameters/arguments are now released
  1901. before the move to the parameter register is done. This improves
  1902. the code in a lot of cases.
  1903. Revision 1.141 2003/09/10 08:31:47 marco
  1904. * Patch from Peter for paraloc
  1905. Revision 1.140 2003/09/07 22:09:35 peter
  1906. * preparations for different default calling conventions
  1907. * various RA fixes
  1908. Revision 1.139 2003/09/03 15:55:01 peter
  1909. * NEWRA branch merged
  1910. Revision 1.138 2003/09/03 11:18:37 florian
  1911. * fixed arm concatcopy
  1912. + arm support in the common compiler sources added
  1913. * moved some generic cg code around
  1914. + tfputype added
  1915. * ...
  1916. Revision 1.137.2.4 2003/08/31 15:46:26 peter
  1917. * more updates for tregister
  1918. Revision 1.137.2.3 2003/08/29 17:28:59 peter
  1919. * next batch of updates
  1920. Revision 1.137.2.2 2003/08/28 18:35:08 peter
  1921. * tregister changed to cardinal
  1922. Revision 1.137.2.1 2003/08/27 19:55:54 peter
  1923. * first tregister patch
  1924. Revision 1.137 2003/08/20 20:29:06 daniel
  1925. * Some more R_NO changes
  1926. * Preventive code to loadref added
  1927. Revision 1.136 2003/08/20 17:48:49 peter
  1928. * fixed stackalloc to not allocate localst.datasize twice
  1929. * order of stackalloc code fixed for implicit init/final
  1930. Revision 1.135 2003/08/17 16:59:20 jonas
  1931. * fixed regvars so they work with newra (at least for ppc)
  1932. * fixed some volatile register bugs
  1933. + -dnotranslation option for -dnewra, which causes the registers not to
  1934. be translated from virtual to normal registers. Requires support in
  1935. the assembler writer as well, which is only implemented in aggas/
  1936. agppcgas currently
  1937. Revision 1.134 2003/08/11 21:18:20 peter
  1938. * start of sparc support for newra
  1939. Revision 1.133 2003/08/09 18:56:54 daniel
  1940. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1941. allocator
  1942. * Some preventive changes to i386 spillinh code
  1943. Revision 1.132 2003/08/03 14:09:50 daniel
  1944. * Fixed a register allocator bug
  1945. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1946. statements: changes in location_force. These moves are now no longer
  1947. constrained so they are optimized away.
  1948. Revision 1.131 2003/07/23 11:04:15 jonas
  1949. * split en_exit_code into a part that may allocate a register and a part
  1950. that doesn't, so the former can be done before the register colouring
  1951. has been performed
  1952. Revision 1.130 2003/07/06 17:58:22 peter
  1953. * framepointer fixes for sparc
  1954. * parent framepointer code more generic
  1955. Revision 1.129 2003/07/06 15:31:20 daniel
  1956. * Fixed register allocator. *Lots* of fixes.
  1957. Revision 1.128 2003/07/02 22:18:04 peter
  1958. * paraloc splitted in callerparaloc,calleeparaloc
  1959. * sparc calling convention updates
  1960. Revision 1.127 2003/06/17 18:13:51 jonas
  1961. * fixed -dnewra compilation problems
  1962. Revision 1.126 2003/06/17 16:32:44 peter
  1963. * alloc register for function result
  1964. Revision 1.125 2003/06/13 21:19:30 peter
  1965. * current_procdef removed, use current_procinfo.procdef instead
  1966. Revision 1.124 2003/06/09 12:23:30 peter
  1967. * init/final of procedure data splitted from genentrycode
  1968. * use asmnode getposition to insert final at the correct position
  1969. als for the implicit try...finally
  1970. Revision 1.123 2003/06/07 18:57:04 jonas
  1971. + added freeintparaloc
  1972. * ppc get/freeintparaloc now check whether the parameter regs are
  1973. properly allocated/deallocated (and get an extra list para)
  1974. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1975. * fixed lot of missing pi_do_call's
  1976. Revision 1.122 2003/06/06 14:43:02 peter
  1977. * g_copyopenarrayvalue gets length reference
  1978. * don't copy open arrays for cdecl
  1979. Revision 1.121 2003/06/03 21:11:09 peter
  1980. * cg.a_load_* get a from and to size specifier
  1981. * makeregsize only accepts newregister
  1982. * i386 uses generic tcgnotnode,tcgunaryminus
  1983. Revision 1.120 2003/06/03 15:49:49 jonas
  1984. * fixed ref/loc problems
  1985. Revision 1.119 2003/06/03 15:06:37 daniel
  1986. * fixed conflict marks
  1987. Revision 1.118 2003/06/03 13:01:59 daniel
  1988. * Register allocator finished
  1989. Revision 1.117 2003/06/02 21:42:05 jonas
  1990. * function results can now also be regvars
  1991. - removed tprocinfo.return_offset, never use it again since it's invalid
  1992. if the result is a regvar
  1993. Revision 1.116 2003/06/01 21:38:06 peter
  1994. * getregisterfpu size parameter added
  1995. * op_const_reg size parameter added
  1996. * sparc updates
  1997. Revision 1.115 2003/05/31 20:28:17 jonas
  1998. * changed copyvalueparas so it also supports register parameters
  1999. (except for copy_value_openarray, but that one is seriously broken
  2000. anyway, since it expects that the high parameter will always be in
  2001. memory right after the pointer to the array, while it could just as
  2002. will be in a register)
  2003. Revision 1.114 2003/05/31 15:05:28 peter
  2004. * FUNCTION_RESULT64_LOW/HIGH_REG added for int64 results
  2005. Revision 1.113 2003/05/31 00:48:15 jonas
  2006. * fixed my previous commit
  2007. Revision 1.112 2003/05/30 23:57:08 peter
  2008. * more sparc cleanup
  2009. * accumulator removed, splitted in function_return_reg (called) and
  2010. function_result_reg (caller)
  2011. Revision 1.111 2003/05/30 23:49:18 jonas
  2012. * a_load_loc_reg now has an extra size parameter for the destination
  2013. register (properly fixes what I worked around in revision 1.106 of
  2014. ncgutil.pas)
  2015. Revision 1.110 2003/05/30 18:52:10 jonas
  2016. * fixed bug with intregvars
  2017. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2018. rcgppc.a_param_ref, which previously got bogus size values
  2019. Revision 1.109 2003/05/27 21:19:08 jonas
  2020. * fixed ppc cycle
  2021. Revision 1.108 2003/05/27 14:28:14 jonas
  2022. * patch from Peter for nested procedures
  2023. Revision 1.107 2003/05/26 21:17:17 peter
  2024. * procinlinenode removed
  2025. * aktexit2label removed, fast exit removed
  2026. + tcallnode.inlined_pass_2 added
  2027. Revision 1.106 2003/05/24 11:59:42 jonas
  2028. * fixed integer typeconversion problems
  2029. Revision 1.105 2003/05/23 14:27:35 peter
  2030. * remove some unit dependencies
  2031. * current_procinfo changes to store more info
  2032. Revision 1.104 2003/05/15 18:58:53 peter
  2033. * removed selfpointer_offset, vmtpointer_offset
  2034. * tvarsym.adjusted_address
  2035. * address in localsymtable is now in the real direction
  2036. * removed some obsolete globals
  2037. Revision 1.103 2003/05/14 19:37:25 jonas
  2038. * patch from Peter for int64 function results
  2039. Revision 1.102 2003/05/13 19:14:41 peter
  2040. * failn removed
  2041. * inherited result code check moven to pexpr
  2042. Revision 1.101 2003/05/13 15:16:13 peter
  2043. * removed ret_in_acc, it's the reverse of ret_in_param
  2044. * fixed ret_in_param for win32 cdecl array
  2045. Revision 1.100 2003/05/12 08:08:27 jonas
  2046. * fixed several initialization and finalization related bugs (missing
  2047. tg.direction's, wrong paralocation for decreasing refcount of
  2048. everything but ansistrings)
  2049. Revision 1.99 2003/05/11 21:37:03 peter
  2050. * moved implicit exception frame from ncgutil to psub
  2051. * constructor/destructor helpers moved from cobj/ncgutil to psub
  2052. Revision 1.98 2003/05/11 14:45:12 peter
  2053. * tloadnode does not support objectsymtable,withsymtable anymore
  2054. * withnode cleanup
  2055. * direct with rewritten to use temprefnode
  2056. Revision 1.97 2003/05/10 13:20:23 jonas
  2057. * moved storing of register parameters to memory even earlier in the
  2058. entry code to fix problems with constructors
  2059. Revision 1.96 2003/05/09 17:47:02 peter
  2060. * self moved to hidden parameter
  2061. * removed hdisposen,hnewn,selfn
  2062. Revision 1.95 2003/04/29 07:28:52 michael
  2063. + Patch from peter to fix wrong pushing of ansistring function results in open array
  2064. Revision 1.94 2003/04/28 21:17:38 peter
  2065. * do not finalize function results
  2066. Revision 1.93 2003/04/27 16:30:34 jonas
  2067. * store register para's to memory before copying the valuepara's, because
  2068. that one requires them to be there already (and it calls subroutines ->
  2069. could overwrite those registers)
  2070. Revision 1.92 2003/04/27 11:21:33 peter
  2071. * aktprocdef renamed to current_procinfo.procdef
  2072. * procinfo renamed to current_procinfo
  2073. * procinfo will now be stored in current_module so it can be
  2074. cleaned up properly
  2075. * gen_main_procsym changed to create_main_proc and release_main_proc
  2076. to also generate a tprocinfo structure
  2077. * fixed unit implicit initfinal
  2078. Revision 1.91 2003/04/27 07:29:50 peter
  2079. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  2080. a new procdef declaration
  2081. * aktprocsym removed
  2082. * lexlevel removed, use symtable.symtablelevel instead
  2083. * implicit init/final code uses the normal genentry/genexit
  2084. * funcret state checking updated for new funcret handling
  2085. Revision 1.90 2003/04/26 17:21:08 florian
  2086. * fixed passing of fpu values by fpu register
  2087. Revision 1.89 2003/04/25 20:59:33 peter
  2088. * removed funcretn,funcretsym, function result is now in varsym
  2089. and aliases for result and function name are added using absolutesym
  2090. * vs_hidden parameter for funcret passed in parameter
  2091. * vs_hidden fixes
  2092. * writenode changed to printnode and released from extdebug
  2093. * -vp option added to generate a tree.log with the nodetree
  2094. * nicer printnode for statements, callnode
  2095. Revision 1.88 2003/04/23 12:35:34 florian
  2096. * fixed several issues with powerpc
  2097. + applied a patch from Jonas for nested function calls (PowerPC only)
  2098. * ...
  2099. Revision 1.87 2003/04/22 14:33:38 peter
  2100. * removed some notes/hints
  2101. Revision 1.86 2003/04/22 13:47:08 peter
  2102. * fixed C style array of const
  2103. * fixed C array passing
  2104. * fixed left to right with high parameters
  2105. Revision 1.85 2003/04/22 10:09:35 daniel
  2106. + Implemented the actual register allocator
  2107. + Scratch registers unavailable when new register allocator used
  2108. + maybe_save/maybe_restore unavailable when new register allocator used
  2109. Revision 1.84 2003/04/16 09:26:55 jonas
  2110. * assembler procedures now again get a stackframe if they have local
  2111. variables. No space is reserved for a function result however.
  2112. Also, the register parameters aren't automatically saved on the stack
  2113. anymore in assembler procedures.
  2114. Revision 1.83 2003/04/06 21:11:23 olle
  2115. * changed newasmsymbol to newasmsymboldata for data symbols
  2116. Revision 1.82 2003/03/30 20:59:07 peter
  2117. * fix classmethod from classmethod call
  2118. * move BeforeDestruction/AfterConstruction calls to
  2119. genentrycode/genexitcode instead of generating them on the fly
  2120. after a call to a constructor
  2121. Revision 1.81 2003/03/28 19:16:56 peter
  2122. * generic constructor working for i386
  2123. * remove fixed self register
  2124. * esi added as address register for i386
  2125. Revision 1.80 2003/03/17 15:52:20 peter
  2126. * fix range error
  2127. Revision 1.79 2003/03/11 21:46:24 jonas
  2128. * lots of new regallocator fixes, both in generic and ppc-specific code
  2129. (ppc compiler still can't compile the linux system unit though)
  2130. Revision 1.78 2003/02/26 21:15:43 daniel
  2131. * Fixed the optimizer
  2132. Revision 1.77 2003/02/19 22:00:14 daniel
  2133. * Code generator converted to new register notation
  2134. - Horribily outdated todo.txt removed
  2135. Revision 1.76 2003/02/15 22:17:38 carl
  2136. * bugfix of FPU emulation code
  2137. Revision 1.75 2003/01/09 22:00:53 florian
  2138. * fixed some PowerPC issues
  2139. Revision 1.74 2003/01/09 20:41:10 florian
  2140. * fixed broken PowerPC compiler
  2141. Revision 1.73 2003/01/08 18:43:56 daniel
  2142. * Tregister changed into a record
  2143. Revision 1.72 2002/12/29 23:51:43 florian
  2144. * web bug 2214 fixed: ie 10 in const array constructors
  2145. Revision 1.71 2002/12/24 15:56:50 peter
  2146. * stackpointer_alloc added for adjusting ESP. Win32 needs
  2147. this for the pageprotection
  2148. Revision 1.70 2002/12/05 14:39:21 florian
  2149. * added missing then, Carl did you really a make fullcycle :) ?
  2150. Revision 1.69 2002/12/03 22:13:39 carl
  2151. * bugfix of problems with profile code which clobbers some registers
  2152. Revision 1.68 2002/12/01 22:06:59 carl
  2153. * warning of portabilitiy problems with parasize / localsize
  2154. Revision 1.67 2002/11/30 18:44:57 carl
  2155. + profiling support for Win32
  2156. Revision 1.66 2002/11/30 14:39:15 carl
  2157. * try to fix profiling for win32
  2158. Revision 1.65 2002/11/28 23:28:14 florian
  2159. * push_value_para didn't release floatdef locations, fixes tw2045
  2160. Revision 1.64 2002/11/27 02:33:19 peter
  2161. * copy_value_on_stack method added for cdecl record passing
  2162. Revision 1.63 2002/11/25 17:43:18 peter
  2163. * splitted defbase in defutil,symutil,defcmp
  2164. * merged isconvertable and is_equal into compare_defs(_ext)
  2165. * made operator search faster by walking the list only once
  2166. Revision 1.62 2002/11/18 17:31:55 peter
  2167. * pass proccalloption to ret_in_xxx and push_xxx functions
  2168. Revision 1.61 2002/11/17 17:49:08 mazen
  2169. + return_result_reg and FUNCTION_RESULT_REG are now used, in all plateforms, to pass functions result between called function and its caller. See the explanation of each one
  2170. Revision 1.60 2002/11/17 16:31:56 carl
  2171. * memory optimization (3-4%) : cleanup of tai fields,
  2172. cleanup of tdef and tsym fields.
  2173. * make it work for m68k
  2174. Revision 1.59 2002/11/15 01:58:51 peter
  2175. * merged changes from 1.0.7 up to 04-11
  2176. - -V option for generating bug report tracing
  2177. - more tracing for option parsing
  2178. - errors for cdecl and high()
  2179. - win32 import stabs
  2180. - win32 records<=8 are returned in eax:edx (turned off by default)
  2181. - heaptrc update
  2182. - more info for temp management in .s file with EXTDEBUG
  2183. Revision 1.58 2002/11/10 19:07:45 mazen
  2184. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  2185. Revision 1.57 2002/11/03 20:22:40 mazen
  2186. * parameter handling updated
  2187. Revision 1.56 2002/10/16 19:01:43 peter
  2188. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  2189. implicit exception frames for procedures with initialized variables
  2190. and for constructors. The default is on for compatibility
  2191. Revision 1.55 2002/10/14 19:42:33 peter
  2192. * only use init tables for threadvars
  2193. Revision 1.54 2002/10/06 19:41:30 peter
  2194. * Add finalization of typed consts
  2195. * Finalization of globals in the main program
  2196. Revision 1.53 2002/10/05 15:18:42 carl
  2197. * fix heap leaks
  2198. Revision 1.52 2002/09/30 07:00:46 florian
  2199. * fixes to common code to get the alpha compiler compiled applied
  2200. Revision 1.51 2002/09/22 14:02:35 carl
  2201. * stack checking cannot be called before system unit is initialized
  2202. * MC68020 define
  2203. Revision 1.50 2002/09/17 18:54:03 jonas
  2204. * a_load_reg_reg() now has two size parameters: source and dest. This
  2205. allows some optimizations on architectures that don't encode the
  2206. register size in the register name.
  2207. Revision 1.49 2002/09/10 21:48:30 florian
  2208. * improved handling of procedures with register calling conventions
  2209. Revision 1.48 2002/09/07 15:25:03 peter
  2210. * old logs removed and tabs fixed
  2211. Revision 1.47 2002/09/02 18:44:48 peter
  2212. * fixed (not) pushing of empty parameters
  2213. * fixed implicit initialization/finalization generation
  2214. * fixed/optimized local copy of value arguments init/final
  2215. Revision 1.46 2002/09/01 19:27:34 peter
  2216. * use index register when available for generating a reference with
  2217. only a signle register. Using the base register could possibly
  2218. destroy the framepointer
  2219. Revision 1.45 2002/09/01 18:50:20 peter
  2220. * fixed maybe_save that did not support a reference with only
  2221. a index register. It now also updates the location with the new
  2222. base register only
  2223. Revision 1.44 2002/09/01 14:42:41 peter
  2224. * removevaluepara added to fix the stackpointer so restoring of
  2225. saved registers works
  2226. Revision 1.43 2002/08/25 19:25:18 peter
  2227. * sym.insert_in_data removed
  2228. * symtable.insertvardata/insertconstdata added
  2229. * removed insert_in_data call from symtable.insert, it needs to be
  2230. called separatly. This allows to deref the address calculation
  2231. * procedures now calculate the parast addresses after the procedure
  2232. directives are parsed. This fixes the cdecl parast problem
  2233. * push_addr_param has an extra argument that specifies if cdecl is used
  2234. or not
  2235. Revision 1.42 2002/08/24 18:38:26 peter
  2236. * really use tt_noreuse for exception frame buffers
  2237. Revision 1.41 2002/08/23 16:14:49 peter
  2238. * tempgen cleanup
  2239. * tt_noreuse temp type added that will be used in genentrycode
  2240. Revision 1.40 2002/08/18 10:42:37 florian
  2241. * remaining assembler writer bugs fixed, the errors in the
  2242. system unit are inline assembler problems
  2243. Revision 1.39 2002/08/17 09:23:36 florian
  2244. * first part of procinfo rewrite
  2245. Revision 1.38 2002/08/16 14:24:57 carl
  2246. * issameref() to test if two references are the same (then emit no opcodes)
  2247. + ret_in_reg to replace ret_in_acc
  2248. (fix some register allocation bugs at the same time)
  2249. + save_std_register now has an extra parameter which is the
  2250. usedinproc registers
  2251. Revision 1.37 2002/08/15 15:15:55 carl
  2252. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  2253. * more generic nodes for maths
  2254. * several fixes for better m68k support
  2255. Revision 1.36 2002/08/14 19:25:09 carl
  2256. * fix Florian's last commit for m68k compilation
  2257. Revision 1.35 2002/08/13 21:40:56 florian
  2258. * more fixes for ppc calling conventions
  2259. Revision 1.34 2002/08/12 15:08:39 carl
  2260. + stab register indexes for powerpc (moved from gdb to cpubase)
  2261. + tprocessor enumeration moved to cpuinfo
  2262. + linker in target_info is now a class
  2263. * many many updates for m68k (will soon start to compile)
  2264. - removed some ifdef or correct them for correct cpu
  2265. Revision 1.33 2002/08/11 14:32:27 peter
  2266. * renamed current_library to objectlibrary
  2267. Revision 1.32 2002/08/11 13:24:12 peter
  2268. * saving of asmsymbols in ppu supported
  2269. * asmsymbollist global is removed and moved into a new class
  2270. tasmlibrarydata that will hold the info of a .a file which
  2271. corresponds with a single module. Added librarydata to tmodule
  2272. to keep the library info stored for the module. In the future the
  2273. objectfiles will also be stored to the tasmlibrarydata class
  2274. * all getlabel/newasmsymbol and friends are moved to the new class
  2275. Revision 1.31 2002/08/09 19:16:57 carl
  2276. * stack allocation is now done separately (at the end) of genentrycode
  2277. so temps. can be allocated before.
  2278. * fix generic exception handling
  2279. Revision 1.30 2002/08/06 20:55:21 florian
  2280. * first part of ppc calling conventions fix
  2281. Revision 1.29 2002/08/04 19:09:22 carl
  2282. + added generic exception support (still does not work!)
  2283. + more documentation
  2284. Revision 1.28 2002/07/29 21:23:42 florian
  2285. * more fixes for the ppc
  2286. + wrappers for the tcnvnode.first_* stuff introduced
  2287. }