ncgutil.pas 97 KB

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