ncgutil.pas 96 KB

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