ncgutil.pas 84 KB

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