ncgutil.pas 100 KB

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