ncgutil.pas 100 KB

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