ncgutil.pas 100 KB

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