ncgutil.pas 99 KB

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