ncgutil.pas 96 KB

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