cgcpu.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  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 cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. cginfo,cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,node,cg64f32;
  26. type
  27. tcgppc = class(tcg)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  36. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  37. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  38. procedure a_call_name(list : taasmoutput;const s : string);override;
  39. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  40. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  41. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); override;
  42. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  43. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  44. size: tcgsize; a: aword; src, dst: tregister); override;
  45. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  46. size: tcgsize; src1, src2, dst: tregister); override;
  47. { move instructions }
  48. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  49. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  50. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  51. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  52. { fpu move instructions }
  53. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  54. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  55. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  56. { comparison operations }
  57. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  58. l : tasmlabel);override;
  59. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  60. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  61. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  62. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  63. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);override;
  64. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  65. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  66. procedure g_restore_frame_pointer(list : taasmoutput);override;
  67. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  68. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  69. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  70. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  71. { that's the case, we can use rlwinm to do an AND operation }
  72. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  73. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);override;
  74. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);override;
  75. procedure g_save_all_registers(list : taasmoutput);override;
  76. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  77. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  78. private
  79. (* NOT IN USE: *)
  80. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  81. (* NOT IN USE: *)
  82. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  83. { Make sure ref is a valid reference for the PowerPC and sets the }
  84. { base to the value of the index if (base = R_NO). }
  85. { Returns true if the reference contained a base, index and an }
  86. { offset or symbol, in which case the base will have been changed }
  87. { to a tempreg (which has to be freed by the caller) containing }
  88. { the sum of part of the original reference }
  89. function fixref(list: taasmoutput; var ref: treference): boolean;
  90. { returns whether a reference can be used immediately in a powerpc }
  91. { instruction }
  92. function issimpleref(const ref: treference): boolean;
  93. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  94. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  95. ref: treference);
  96. { creates the correct branch instruction for a given combination }
  97. { of asmcondflags and destination addressing mode }
  98. procedure a_jmp(list: taasmoutput; op: tasmop;
  99. c: tasmcondflag; crval: longint; l: tasmlabel);
  100. end;
  101. tcg64fppc = class(tcg64f32)
  102. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  103. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  104. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  105. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  106. end;
  107. const
  108. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  109. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  110. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  111. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  112. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  113. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  114. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  115. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  116. implementation
  117. uses
  118. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi, rgcpu;
  119. procedure tcgppc.init_register_allocators;
  120. begin
  121. rg := trgcpu.create(29,chr(ord(RS_R3))+chr(ord(RS_R4))+chr(ord(RS_R5))+chr(ord(RS_R6))+chr(ord(RS_R7))+chr(ord(RS_R8))+chr(ord(RS_R9))+chr(ord(RS_R10))+chr(ord(RS_R11))+chr(ord(RS_R12))+chr(ord(RS_R31))+chr(ord(RS_R30))+chr(ord(RS_R29))+chr(ord(RS_R28))+chr(ord(RS_R27))+chr(ord(RS_R26))+chr(ord(RS_R25))+chr(ord(RS_R24))+chr(ord(RS_R23))+chr(ord(RS_R22))+chr(ord(RS_R21))+chr(ord(RS_R20))+chr(ord(RS_R19))+chr(ord(RS_R18))+chr(ord(RS_R17))+chr(ord(RS_R16))+chr(ord(RS_R15))+chr(ord(RS_R14))+chr(ord(RS_R13)));
  122. end;
  123. procedure tcgppc.done_register_allocators;
  124. begin
  125. rg.free;
  126. end;
  127. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  128. var
  129. ref: treference;
  130. begin
  131. case locpara.loc of
  132. LOC_REGISTER,LOC_CREGISTER:
  133. a_load_const_reg(list,size,a,locpara.register);
  134. LOC_REFERENCE:
  135. begin
  136. reference_reset(ref);
  137. ref.base:=locpara.reference.index;
  138. ref.offset:=locpara.reference.offset;
  139. a_load_const_ref(list,size,a,ref);
  140. end;
  141. else
  142. internalerror(2002081101);
  143. end;
  144. if locpara.sp_fixup<>0 then
  145. internalerror(2002081102);
  146. end;
  147. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  148. var
  149. ref: treference;
  150. tmpreg: tregister;
  151. begin
  152. case locpara.loc of
  153. LOC_REGISTER,LOC_CREGISTER:
  154. a_load_ref_reg(list,size,size,r,locpara.register);
  155. LOC_REFERENCE:
  156. begin
  157. reference_reset(ref);
  158. ref.base:=locpara.reference.index;
  159. ref.offset:=locpara.reference.offset;
  160. tmpreg := rg.getregisterint(list,size);
  161. a_load_ref_reg(list,size,size,r,tmpreg);
  162. a_load_reg_ref(list,size,size,tmpreg,ref);
  163. rg.ungetregisterint(list,tmpreg);
  164. end;
  165. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  166. case size of
  167. OS_F32, OS_F64:
  168. a_loadfpu_ref_reg(list,size,r,locpara.register);
  169. else
  170. internalerror(2002072801);
  171. end;
  172. else
  173. internalerror(2002081103);
  174. end;
  175. if locpara.sp_fixup<>0 then
  176. internalerror(2002081104);
  177. end;
  178. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  179. var
  180. ref: treference;
  181. tmpreg: tregister;
  182. begin
  183. case locpara.loc of
  184. LOC_REGISTER,LOC_CREGISTER:
  185. a_loadaddr_ref_reg(list,r,locpara.register);
  186. LOC_REFERENCE:
  187. begin
  188. reference_reset(ref);
  189. ref.base := locpara.reference.index;
  190. ref.offset := locpara.reference.offset;
  191. tmpreg := rg.getregisterint(list,OS_ADDR);
  192. a_loadaddr_ref_reg(list,r,tmpreg);
  193. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  194. rg.ungetregisterint(list,tmpreg);
  195. end;
  196. else
  197. internalerror(2002080701);
  198. end;
  199. end;
  200. { calling a procedure by name }
  201. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  202. var
  203. href : treference;
  204. begin
  205. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  206. if it is a cross-TOC call. If so, it also replaces the NOP
  207. with some restore code.}
  208. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  209. if target_info.system=system_powerpc_macos then
  210. list.concat(taicpu.op_none(A_NOP));
  211. if not(pi_do_call in current_procinfo.flags) then
  212. internalerror(2003060703);
  213. end;
  214. { calling a procedure by address }
  215. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  216. var
  217. tmpreg : tregister;
  218. tmpref : treference;
  219. begin
  220. if target_info.system=system_powerpc_macos then
  221. begin
  222. {Generate instruction to load the procedure address from
  223. the transition vector.}
  224. //TODO: Support cross-TOC calls.
  225. tmpreg := rg.getregisterint(list,OS_INT);
  226. reference_reset(tmpref);
  227. tmpref.offset := 0;
  228. //tmpref.symaddr := refs_full;
  229. tmpref.base:= reg;
  230. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  231. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  232. rg.ungetregisterint(list,tmpreg);
  233. end
  234. else
  235. list.concat(taicpu.op_reg(A_MTCTR,reg));
  236. list.concat(taicpu.op_none(A_BCTRL));
  237. //if target_info.system=system_powerpc_macos then
  238. // //NOP is not needed here.
  239. // list.concat(taicpu.op_none(A_NOP));
  240. if not(pi_do_call in current_procinfo.flags) then
  241. internalerror(2003060704);
  242. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  243. end;
  244. { calling a procedure by address }
  245. procedure tcgppc.a_call_ref(list : taasmoutput;const ref : treference);
  246. var
  247. tmpreg : tregister;
  248. tmpref : treference;
  249. begin
  250. tmpreg := rg.getregisterint(list,OS_ADDR);
  251. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,tmpreg);
  252. if target_info.system=system_powerpc_macos then
  253. begin
  254. {Generate instruction to load the procedure address from
  255. the transition vector.}
  256. //TODO: Support cross-TOC calls.
  257. reference_reset(tmpref);
  258. tmpref.offset := 0;
  259. //tmpref.symaddr := refs_full;
  260. tmpref.base:= tmpreg;
  261. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  262. end;
  263. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  264. rg.ungetregisterint(list,tmpreg);
  265. list.concat(taicpu.op_none(A_BCTRL));
  266. //if target_info.system=system_powerpc_macos then
  267. // //NOP is not needed here.
  268. // list.concat(taicpu.op_none(A_NOP));
  269. if not(pi_do_call in current_procinfo.flags) then
  270. internalerror(2003060705);
  271. //list.concat(tai_comment.create(strpnew('***** a_call_ref')));
  272. end;
  273. {********************** load instructions ********************}
  274. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  275. begin
  276. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  277. internalerror(2002090902);
  278. if (longint(a) >= low(smallint)) and
  279. (longint(a) <= high(smallint)) then
  280. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  281. else if ((a and $ffff) <> 0) then
  282. begin
  283. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  284. if ((a shr 16) <> 0) or
  285. (smallint(a and $ffff) < 0) then
  286. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  287. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  288. end
  289. else
  290. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  291. end;
  292. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  293. const
  294. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  295. { indexed? updating?}
  296. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  297. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  298. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  299. var
  300. op: TAsmOp;
  301. ref2: TReference;
  302. freereg: boolean;
  303. begin
  304. ref2 := ref;
  305. freereg := fixref(list,ref2);
  306. if tosize in [OS_S8..OS_S16] then
  307. { storing is the same for signed and unsigned values }
  308. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  309. { 64 bit stuff should be handled separately }
  310. if tosize in [OS_64,OS_S64] then
  311. internalerror(200109236);
  312. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  313. a_load_store(list,op,reg,ref2);
  314. if freereg then
  315. rg.ungetregisterint(list,ref2.base);
  316. End;
  317. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  318. const
  319. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  320. { indexed? updating?}
  321. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  322. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  323. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  324. { 64bit stuff should be handled separately }
  325. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  326. { there's no load-byte-with-sign-extend :( }
  327. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  328. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  329. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  330. var
  331. op: tasmop;
  332. tmpreg: tregister;
  333. ref2, tmpref: treference;
  334. freereg: boolean;
  335. begin
  336. { TODO: optimize/take into consideration fromsize/tosize. Will }
  337. { probably only matter for OS_S8 loads though }
  338. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  339. internalerror(2002090902);
  340. ref2 := ref;
  341. freereg := fixref(list,ref2);
  342. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  343. a_load_store(list,op,reg,ref2);
  344. if freereg then
  345. rg.ungetregisterint(list,ref2.base);
  346. { sign extend shortint if necessary, since there is no }
  347. { load instruction that does that automatically (JM) }
  348. if fromsize = OS_S8 then
  349. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  350. end;
  351. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  352. var
  353. instr: taicpu;
  354. begin
  355. if (reg1<>reg2) or
  356. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  357. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  358. (tosize <> fromsize) and
  359. not(fromsize in [OS_32,OS_S32])) then
  360. begin
  361. case tosize of
  362. OS_8:
  363. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  364. reg2,reg1,0,31-8+1,31);
  365. OS_S8:
  366. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  367. OS_16:
  368. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  369. reg2,reg1,0,31-16+1,31);
  370. OS_S16:
  371. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  372. OS_32,OS_S32:
  373. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  374. else internalerror(2002090901);
  375. end;
  376. list.concat(instr);
  377. rg.add_move_instruction(instr);
  378. end;
  379. end;
  380. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  381. begin
  382. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  383. end;
  384. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  385. const
  386. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  387. { indexed? updating?}
  388. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  389. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  390. var
  391. op: tasmop;
  392. ref2: treference;
  393. freereg: boolean;
  394. begin
  395. { several functions call this procedure with OS_32 or OS_64 }
  396. { so this makes life easier (FK) }
  397. case size of
  398. OS_32,OS_F32:
  399. size:=OS_F32;
  400. OS_64,OS_F64,OS_C64:
  401. size:=OS_F64;
  402. else
  403. internalerror(200201121);
  404. end;
  405. ref2 := ref;
  406. freereg := fixref(list,ref2);
  407. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  408. a_load_store(list,op,reg,ref2);
  409. if freereg then
  410. rg.ungetregisterint(list,ref2.base);
  411. end;
  412. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  413. const
  414. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  415. { indexed? updating?}
  416. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  417. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  418. var
  419. op: tasmop;
  420. ref2: treference;
  421. freereg: boolean;
  422. begin
  423. if not(size in [OS_F32,OS_F64]) then
  424. internalerror(200201122);
  425. ref2 := ref;
  426. freereg := fixref(list,ref2);
  427. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  428. a_load_store(list,op,reg,ref2);
  429. if freereg then
  430. rg.ungetregisterint(list,ref2.base);
  431. end;
  432. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  433. begin
  434. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  435. end;
  436. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  437. begin
  438. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  439. end;
  440. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  441. size: tcgsize; a: aword; src, dst: tregister);
  442. var
  443. l1,l2: longint;
  444. oplo, ophi: tasmop;
  445. scratchreg: tregister;
  446. useReg, gotrlwi: boolean;
  447. procedure do_lo_hi;
  448. begin
  449. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  450. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  451. end;
  452. begin
  453. if op = OP_SUB then
  454. begin
  455. {$ifopt q+}
  456. {$q-}
  457. {$define overflowon}
  458. {$endif}
  459. a_op_const_reg_reg(list,OP_ADD,size,aword(-longint(a)),src,dst);
  460. {$ifdef overflowon}
  461. {$q+}
  462. {$undef overflowon}
  463. {$endif}
  464. exit;
  465. end;
  466. ophi := TOpCG2AsmOpConstHi[op];
  467. oplo := TOpCG2AsmOpConstLo[op];
  468. gotrlwi := get_rlwi_const(a,l1,l2);
  469. if (op in [OP_AND,OP_OR,OP_XOR]) then
  470. begin
  471. if (a = 0) then
  472. begin
  473. if op = OP_AND then
  474. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  475. else
  476. a_load_reg_reg(list,size,size,src,dst);
  477. exit;
  478. end
  479. else if (a = high(aword)) then
  480. begin
  481. case op of
  482. OP_OR:
  483. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  484. OP_XOR:
  485. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  486. OP_AND:
  487. a_load_reg_reg(list,size,size,src,dst);
  488. end;
  489. exit;
  490. end
  491. else if (a <= high(word)) and
  492. ((op <> OP_AND) or
  493. not gotrlwi) then
  494. begin
  495. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  496. exit;
  497. end;
  498. { all basic constant instructions also have a shifted form that }
  499. { works only on the highest 16bits, so if lo(a) is 0, we can }
  500. { use that one }
  501. if (word(a) = 0) and
  502. (not(op = OP_AND) or
  503. not gotrlwi) then
  504. begin
  505. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  506. exit;
  507. end;
  508. end
  509. else if (op = OP_ADD) then
  510. if a = 0 then
  511. exit
  512. else if (longint(a) >= low(smallint)) and
  513. (longint(a) <= high(smallint)) then
  514. begin
  515. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  516. exit;
  517. end;
  518. { otherwise, the instructions we can generate depend on the }
  519. { operation }
  520. useReg := false;
  521. case op of
  522. OP_DIV,OP_IDIV:
  523. if (a = 0) then
  524. internalerror(200208103)
  525. else if (a = 1) then
  526. begin
  527. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  528. exit
  529. end
  530. else if ispowerof2(a,l1) then
  531. begin
  532. case op of
  533. OP_DIV:
  534. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  535. OP_IDIV:
  536. begin
  537. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  538. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  539. end;
  540. end;
  541. exit;
  542. end
  543. else
  544. usereg := true;
  545. OP_IMUL, OP_MUL:
  546. if (a = 0) then
  547. begin
  548. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  549. exit
  550. end
  551. else if (a = 1) then
  552. begin
  553. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  554. exit
  555. end
  556. else if ispowerof2(a,l1) then
  557. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  558. else if (longint(a) >= low(smallint)) and
  559. (longint(a) <= high(smallint)) then
  560. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  561. else
  562. usereg := true;
  563. OP_ADD:
  564. begin
  565. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  566. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  567. smallint((a shr 16) + ord(smallint(a) < 0))));
  568. end;
  569. OP_OR:
  570. { try to use rlwimi }
  571. if gotrlwi and
  572. (src = dst) then
  573. begin
  574. scratchreg := rg.getregisterint(list,OS_INT);
  575. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  576. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  577. scratchreg,0,l1,l2));
  578. rg.ungetregisterint(list,scratchreg);
  579. end
  580. else
  581. do_lo_hi;
  582. OP_AND:
  583. { try to use rlwinm }
  584. if gotrlwi then
  585. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  586. src,0,l1,l2))
  587. else
  588. useReg := true;
  589. OP_XOR:
  590. do_lo_hi;
  591. OP_SHL,OP_SHR,OP_SAR:
  592. begin
  593. if (a and 31) <> 0 Then
  594. list.concat(taicpu.op_reg_reg_const(
  595. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  596. else
  597. a_load_reg_reg(list,size,size,src,dst);
  598. if (a shr 5) <> 0 then
  599. internalError(68991);
  600. end
  601. else
  602. internalerror(200109091);
  603. end;
  604. { if all else failed, load the constant in a register and then }
  605. { perform the operation }
  606. if useReg then
  607. begin
  608. scratchreg := rg.getregisterint(list,OS_INT);
  609. a_load_const_reg(list,OS_32,a,scratchreg);
  610. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  611. rg.ungetregisterint(list,scratchreg);
  612. end;
  613. end;
  614. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  615. size: tcgsize; src1, src2, dst: tregister);
  616. const
  617. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  618. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  619. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  620. begin
  621. case op of
  622. OP_NEG,OP_NOT:
  623. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  624. else
  625. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  626. end;
  627. end;
  628. {*************** compare instructructions ****************}
  629. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  630. l : tasmlabel);
  631. var
  632. p: taicpu;
  633. scratch_register: TRegister;
  634. signed: boolean;
  635. begin
  636. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  637. { in the following case, we generate more efficient code when }
  638. { signed is true }
  639. if (cmp_op in [OC_EQ,OC_NE]) and
  640. (a > $ffff) then
  641. signed := true;
  642. if signed then
  643. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  644. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,longint(a)))
  645. else
  646. begin
  647. scratch_register := rg.getregisterint(list,OS_INT);
  648. a_load_const_reg(list,OS_32,a,scratch_register);
  649. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  650. rg.ungetregisterint(list,scratch_register);
  651. end
  652. else
  653. if (a <= $ffff) then
  654. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,a))
  655. else
  656. begin
  657. scratch_register := rg.getregisterint(list,OS_INT);
  658. a_load_const_reg(list,OS_32,a,scratch_register);
  659. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  660. rg.ungetregisterint(list,scratch_register);
  661. end;
  662. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  663. end;
  664. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  665. reg1,reg2 : tregister;l : tasmlabel);
  666. var
  667. p: taicpu;
  668. op: tasmop;
  669. begin
  670. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  671. op := A_CMPW
  672. else
  673. op := A_CMPLW;
  674. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  675. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  676. end;
  677. procedure tcgppc.g_save_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);
  678. begin
  679. {$warning FIX ME}
  680. end;
  681. procedure tcgppc.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsuperregisterset);
  682. begin
  683. {$warning FIX ME}
  684. end;
  685. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  686. begin
  687. {$warning FIX ME}
  688. end;
  689. procedure tcgppc.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  690. begin
  691. {$warning FIX ME}
  692. end;
  693. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  694. begin
  695. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  696. end;
  697. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  698. begin
  699. a_jmp(list,A_B,C_None,0,l);
  700. end;
  701. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  702. var
  703. c: tasmcond;
  704. begin
  705. c := flags_to_cond(f);
  706. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  707. end;
  708. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  709. var
  710. testbit: byte;
  711. bitvalue: boolean;
  712. begin
  713. { get the bit to extract from the conditional register + its }
  714. { requested value (0 or 1) }
  715. testbit := ((f.cr-RS_CR0) * 4);
  716. case f.flag of
  717. F_EQ,F_NE:
  718. begin
  719. inc(testbit,2);
  720. bitvalue := f.flag = F_EQ;
  721. end;
  722. F_LT,F_GE:
  723. begin
  724. bitvalue := f.flag = F_LT;
  725. end;
  726. F_GT,F_LE:
  727. begin
  728. inc(testbit);
  729. bitvalue := f.flag = F_GT;
  730. end;
  731. else
  732. internalerror(200112261);
  733. end;
  734. { load the conditional register in the destination reg }
  735. list.concat(taicpu.op_reg(A_MFCR,reg));
  736. { we will move the bit that has to be tested to bit 0 by rotating }
  737. { left }
  738. testbit := (testbit + 1) and 31;
  739. { extract bit }
  740. list.concat(taicpu.op_reg_reg_const_const_const(
  741. A_RLWINM,reg,reg,testbit,31,31));
  742. { if we need the inverse, xor with 1 }
  743. if not bitvalue then
  744. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  745. end;
  746. (*
  747. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  748. var
  749. testbit: byte;
  750. bitvalue: boolean;
  751. begin
  752. { get the bit to extract from the conditional register + its }
  753. { requested value (0 or 1) }
  754. case f.simple of
  755. false:
  756. begin
  757. { we don't generate this in the compiler }
  758. internalerror(200109062);
  759. end;
  760. true:
  761. case f.cond of
  762. C_None:
  763. internalerror(200109063);
  764. C_LT..C_NU:
  765. begin
  766. testbit := (ord(f.cr) - ord(R_CR0))*4;
  767. inc(testbit,AsmCondFlag2BI[f.cond]);
  768. bitvalue := AsmCondFlagTF[f.cond];
  769. end;
  770. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  771. begin
  772. testbit := f.crbit
  773. bitvalue := AsmCondFlagTF[f.cond];
  774. end;
  775. else
  776. internalerror(200109064);
  777. end;
  778. end;
  779. { load the conditional register in the destination reg }
  780. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  781. { we will move the bit that has to be tested to bit 31 -> rotate }
  782. { left by bitpos+1 (remember, this is big-endian!) }
  783. if bitpos <> 31 then
  784. inc(bitpos)
  785. else
  786. bitpos := 0;
  787. { extract bit }
  788. list.concat(taicpu.op_reg_reg_const_const_const(
  789. A_RLWINM,reg,reg,bitpos,31,31));
  790. { if we need the inverse, xor with 1 }
  791. if not bitvalue then
  792. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  793. end;
  794. *)
  795. { *********** entry/exit code and address loading ************ }
  796. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  797. { generated the entry code of a procedure/function. Note: localsize is the }
  798. { sum of the size necessary for local variables and the maximum possible }
  799. { combined size of ALL the parameters of a procedure called by the current }
  800. { one. }
  801. { This procedure may be called before, as well as after
  802. g_return_from_proc is called.}
  803. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  804. href,href2 : treference;
  805. usesfpr,usesgpr,gotgot : boolean;
  806. parastart : aword;
  807. offset : aword;
  808. // r,r2,rsp:Tregister;
  809. regcounter2: Tsuperregister;
  810. regidx : tregisterindex;
  811. hp: tparaitem;
  812. begin
  813. { CR and LR only have to be saved in case they are modified by the current }
  814. { procedure, but currently this isn't checked, so save them always }
  815. { following is the entry code as described in "Altivec Programming }
  816. { Interface Manual", bar the saving of AltiVec registers }
  817. a_reg_alloc(list,NR_STACK_POINTER_REG);
  818. a_reg_alloc(list,NR_R0);
  819. if current_procinfo.procdef.parast.symtablelevel>1 then
  820. a_reg_alloc(list,NR_R11);
  821. usesfpr:=false;
  822. if not (po_assembler in current_procinfo.procdef.procoptions) then
  823. {$warning FIXME!!}
  824. { FIXME: has to be R_F8 instad of R_F14 for SYSV abi }
  825. for regcounter:=RS_F14 to RS_F31 do
  826. begin
  827. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  828. if regidx in rg.used_in_proc_other then
  829. begin
  830. usesfpr:= true;
  831. firstregfpu:=regcounter;
  832. break;
  833. end;
  834. end;
  835. usesgpr:=false;
  836. if not (po_assembler in current_procinfo.procdef.procoptions) then
  837. for regcounter2:=firstsaveintreg to RS_R31 do
  838. begin
  839. if regcounter2 in rg.used_in_proc_int then
  840. begin
  841. usesgpr:=true;
  842. firstreggpr:=regcounter2;
  843. break;
  844. end;
  845. end;
  846. { save link register? }
  847. if not (po_assembler in current_procinfo.procdef.procoptions) then
  848. if (pi_do_call in current_procinfo.flags) then
  849. begin
  850. { save return address... }
  851. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  852. { ... in caller's frame }
  853. case target_info.abi of
  854. abi_powerpc_aix:
  855. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  856. abi_powerpc_sysv:
  857. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  858. end;
  859. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  860. a_reg_dealloc(list,NR_R0);
  861. end;
  862. { save the CR if necessary in callers frame. }
  863. if not (po_assembler in current_procinfo.procdef.procoptions) then
  864. if target_info.abi = abi_powerpc_aix then
  865. if false then { Not needed at the moment. }
  866. begin
  867. a_reg_alloc(list,NR_R0);
  868. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  869. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  870. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  871. a_reg_dealloc(list,NR_R0);
  872. end;
  873. { !!! always allocate space for all registers for now !!! }
  874. if not (po_assembler in current_procinfo.procdef.procoptions) then
  875. { if usesfpr or usesgpr then }
  876. begin
  877. a_reg_alloc(list,NR_R12);
  878. { save end of fpr save area }
  879. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  880. end;
  881. if (localsize <> 0) then
  882. begin
  883. if (localsize <= high(smallint)) then
  884. begin
  885. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  886. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  887. end
  888. else
  889. begin
  890. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  891. { can't use getregisterint here, the register colouring }
  892. { is already done when we get here }
  893. href.index := NR_R11;
  894. a_reg_alloc(list,href.index);
  895. a_load_const_reg(list,OS_S32,-localsize,href.index);
  896. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  897. a_reg_dealloc(list,href.index);
  898. end;
  899. end;
  900. { no GOT pointer loaded yet }
  901. gotgot:=false;
  902. if usesfpr then
  903. begin
  904. { save floating-point registers
  905. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  906. begin
  907. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g');
  908. gotgot:=true;
  909. end
  910. else
  911. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14));
  912. }
  913. reference_reset_base(href,NR_R12,-8);
  914. for regcounter:=firstregfpu to RS_F31 do
  915. begin
  916. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  917. if regidx in rg.used_in_proc_other then
  918. begin
  919. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  920. dec(href.offset,8);
  921. end;
  922. end;
  923. { compute end of gpr save area }
  924. a_op_const_reg(list,OP_ADD,OS_ADDR,aword(href.offset+8),NR_R12);
  925. end;
  926. { save gprs and fetch GOT pointer }
  927. if usesgpr then
  928. begin
  929. {
  930. if cs_create_pic in aktmoduleswitches then
  931. begin
  932. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g');
  933. gotgot:=true;
  934. end
  935. else
  936. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14))
  937. }
  938. reference_reset_base(href,NR_R12,-4);
  939. for regcounter2:=firstsaveintreg to RS_R31 do
  940. begin
  941. if regcounter2 in rg.used_in_proc_int then
  942. begin
  943. usesgpr:=true;
  944. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  945. dec(href.offset,4);
  946. end;
  947. end;
  948. {
  949. r.enum:=R_INTREGISTER;
  950. r.:=;
  951. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  952. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  953. }
  954. end;
  955. if assigned(current_procinfo.procdef.parast) then
  956. begin
  957. if not (po_assembler in current_procinfo.procdef.procoptions) then
  958. begin
  959. { copy memory parameters to local parast }
  960. hp:=tparaitem(current_procinfo.procdef.para.first);
  961. while assigned(hp) do
  962. begin
  963. if (hp.paraloc[calleeside].loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  964. begin
  965. reference_reset_base(href,current_procinfo.framepointer,tvarsym(hp.parasym).adjusted_address);
  966. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  967. cg.a_load_ref_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,href);
  968. end
  969. {$ifdef dummy}
  970. else if (hp.calleeparaloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  971. begin
  972. rg.getexplicitregisterint(list,hp.calleeparaloc.register);
  973. end
  974. {$endif dummy}
  975. ;
  976. hp := tparaitem(hp.next);
  977. end;
  978. end;
  979. end;
  980. if usesfpr or usesgpr then
  981. a_reg_dealloc(list,NR_R12);
  982. { PIC code support, }
  983. if cs_create_pic in aktmoduleswitches then
  984. begin
  985. { if we didn't get the GOT pointer till now, we've to calculate it now }
  986. if not(gotgot) then
  987. begin
  988. {!!!!!!!!!!!!!}
  989. end;
  990. a_reg_alloc(list,NR_R31);
  991. { place GOT ptr in r31 }
  992. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  993. end;
  994. { save the CR if necessary ( !!! always done currently ) }
  995. { still need to find out where this has to be done for SystemV
  996. a_reg_alloc(list,R_0);
  997. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  998. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  999. new_reference(STACK_POINTER_REG,LA_CR)));
  1000. a_reg_dealloc(list,R_0); }
  1001. { now comes the AltiVec context save, not yet implemented !!! }
  1002. { if we're in a nested procedure, we've to save R11 }
  1003. if current_procinfo.procdef.parast.symtablelevel>2 then
  1004. begin
  1005. reference_reset_base(href,NR_STACK_POINTER_REG,PARENT_FRAMEPOINTER_OFFSET);
  1006. list.concat(taicpu.op_reg_ref(A_STW,NR_R11,href));
  1007. end;
  1008. end;
  1009. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  1010. { This procedure may be called before, as well as after
  1011. g_stackframe_entry is called.}
  1012. var
  1013. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  1014. href : treference;
  1015. usesfpr,usesgpr,genret : boolean;
  1016. regcounter2:Tsuperregister;
  1017. localsize: aword;
  1018. regidx : tregisterindex;
  1019. begin
  1020. { AltiVec context restore, not yet implemented !!! }
  1021. usesfpr:=false;
  1022. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1023. for regcounter:=RS_F14 to RS_F31 do
  1024. begin
  1025. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1026. if regidx in rg.used_in_proc_other then
  1027. begin
  1028. usesfpr:=true;
  1029. firstregfpu:=regcounter;
  1030. break;
  1031. end;
  1032. end;
  1033. usesgpr:=false;
  1034. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1035. for regcounter2:=firstsaveintreg to RS_R31 do
  1036. begin
  1037. if regcounter2 in rg.used_in_proc_int then
  1038. begin
  1039. usesgpr:=true;
  1040. firstreggpr:=regcounter2;
  1041. break;
  1042. end;
  1043. end;
  1044. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1045. { no return (blr) generated yet }
  1046. genret:=true;
  1047. if usesgpr or usesfpr then
  1048. begin
  1049. { address of gpr save area to r11 }
  1050. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12);
  1051. if usesfpr then
  1052. begin
  1053. reference_reset_base(href,NR_R12,-8);
  1054. for regcounter := firstregfpu to RS_F31 do
  1055. begin
  1056. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1057. if regidx in rg.used_in_proc_other then
  1058. begin
  1059. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1060. dec(href.offset,8);
  1061. end;
  1062. end;
  1063. inc(href.offset,4);
  1064. end
  1065. else
  1066. reference_reset_base(href,NR_R12,-4);
  1067. for regcounter2:=firstsaveintreg to RS_R31 do
  1068. begin
  1069. if regcounter2 in rg.used_in_proc_int then
  1070. begin
  1071. usesgpr:=true;
  1072. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1073. dec(href.offset,4);
  1074. end;
  1075. end;
  1076. (*
  1077. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1078. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1079. *)
  1080. end;
  1081. (*
  1082. { restore fprs and return }
  1083. if usesfpr then
  1084. begin
  1085. { address of fpr save area to r11 }
  1086. r:=NR_R12;
  1087. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1088. {
  1089. if (pi_do_call in current_procinfo.flags) then
  1090. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1091. '_x')
  1092. else
  1093. { leaf node => lr haven't to be restored }
  1094. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1095. '_l');
  1096. genret:=false;
  1097. }
  1098. end;
  1099. *)
  1100. { if we didn't generate the return code, we've to do it now }
  1101. if genret then
  1102. begin
  1103. { adjust r1 }
  1104. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1105. { load link register? }
  1106. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1107. begin
  1108. if (pi_do_call in current_procinfo.flags) then
  1109. begin
  1110. case target_info.abi of
  1111. abi_powerpc_aix:
  1112. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1113. abi_powerpc_sysv:
  1114. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1115. end;
  1116. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1117. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1118. end;
  1119. { restore the CR if necessary from callers frame}
  1120. if target_info.abi = abi_powerpc_aix then
  1121. if false then { Not needed at the moment. }
  1122. begin
  1123. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1124. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1125. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1126. a_reg_dealloc(list,NR_R0);
  1127. end;
  1128. end;
  1129. list.concat(taicpu.op_none(A_BLR));
  1130. end;
  1131. end;
  1132. function save_regs(list : taasmoutput):longint;
  1133. {Generates code which saves used non-volatile registers in
  1134. the save area right below the address the stackpointer point to.
  1135. Returns the actual used save area size.}
  1136. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1137. usesfpr,usesgpr: boolean;
  1138. href : treference;
  1139. offset: integer;
  1140. regcounter2: Tsuperregister;
  1141. regidx : tregisterindex;
  1142. begin
  1143. usesfpr:=false;
  1144. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1145. for regcounter:=RS_F14 to RS_F31 do
  1146. begin
  1147. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1148. if regidx in rg.used_in_proc_other then
  1149. begin
  1150. usesfpr:=true;
  1151. firstregfpu:=regcounter;
  1152. break;
  1153. end;
  1154. end;
  1155. usesgpr:=false;
  1156. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1157. for regcounter2:=firstsaveintreg to RS_R31 do
  1158. begin
  1159. if regcounter2 in rg.used_in_proc_int then
  1160. begin
  1161. usesgpr:=true;
  1162. firstreggpr:=regcounter2;
  1163. break;
  1164. end;
  1165. end;
  1166. offset:= 0;
  1167. { save floating-point registers }
  1168. if usesfpr then
  1169. for regcounter := firstregfpu to RS_F31 do
  1170. begin
  1171. offset:= offset - 8;
  1172. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1173. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  1174. end;
  1175. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1176. { save gprs in gpr save area }
  1177. if usesgpr then
  1178. if firstreggpr < RS_R30 then
  1179. begin
  1180. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1181. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1182. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1183. {STMW stores multiple registers}
  1184. end
  1185. else
  1186. begin
  1187. for regcounter := firstreggpr to RS_R31 do
  1188. begin
  1189. offset:= offset - 4;
  1190. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1191. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1192. end;
  1193. end;
  1194. { now comes the AltiVec context save, not yet implemented !!! }
  1195. save_regs:= -offset;
  1196. end;
  1197. procedure restore_regs(list : taasmoutput);
  1198. {Generates code which restores used non-volatile registers from
  1199. the save area right below the address the stackpointer point to.}
  1200. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1201. usesfpr,usesgpr: boolean;
  1202. href : treference;
  1203. offset: integer;
  1204. regcounter2: Tsuperregister;
  1205. regidx : tregisterindex;
  1206. begin
  1207. usesfpr:=false;
  1208. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1209. for regcounter:=RS_F14 to RS_F31 do
  1210. begin
  1211. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1212. if regidx in rg.used_in_proc_other then
  1213. begin
  1214. usesfpr:=true;
  1215. firstregfpu:=regcounter;
  1216. break;
  1217. end;
  1218. end;
  1219. usesgpr:=false;
  1220. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1221. for regcounter2:=RS_R13 to RS_R31 do
  1222. begin
  1223. if regcounter2 in rg.used_in_proc_int then
  1224. begin
  1225. usesgpr:=true;
  1226. firstreggpr:=regcounter2;
  1227. break;
  1228. end;
  1229. end;
  1230. offset:= 0;
  1231. { restore fp registers }
  1232. if usesfpr then
  1233. for regcounter := firstregfpu to RS_F31 do
  1234. begin
  1235. offset:= offset - 8;
  1236. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1237. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1238. end;
  1239. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1240. { restore gprs }
  1241. if usesgpr then
  1242. if firstreggpr < RS_R30 then
  1243. begin
  1244. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1245. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1246. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1247. {LMW loads multiple registers}
  1248. end
  1249. else
  1250. begin
  1251. for regcounter := firstreggpr to RS_R31 do
  1252. begin
  1253. offset:= offset - 4;
  1254. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1255. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1256. end;
  1257. end;
  1258. { now comes the AltiVec context restore, not yet implemented !!! }
  1259. end;
  1260. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1261. (* NOT IN USE *)
  1262. { generated the entry code of a procedure/function. Note: localsize is the }
  1263. { sum of the size necessary for local variables and the maximum possible }
  1264. { combined size of ALL the parameters of a procedure called by the current }
  1265. { one }
  1266. const
  1267. macosLinkageAreaSize = 24;
  1268. var regcounter: TRegister;
  1269. href : treference;
  1270. registerSaveAreaSize : longint;
  1271. begin
  1272. if (localsize mod 8) <> 0 then
  1273. internalerror(58991);
  1274. { CR and LR only have to be saved in case they are modified by the current }
  1275. { procedure, but currently this isn't checked, so save them always }
  1276. { following is the entry code as described in "Altivec Programming }
  1277. { Interface Manual", bar the saving of AltiVec registers }
  1278. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1279. a_reg_alloc(list,NR_R0);
  1280. { save return address in callers frame}
  1281. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1282. { ... in caller's frame }
  1283. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1284. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1285. a_reg_dealloc(list,NR_R0);
  1286. { save non-volatile registers in callers frame}
  1287. registerSaveAreaSize:= save_regs(list);
  1288. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1289. a_reg_alloc(list,NR_R0);
  1290. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1291. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1292. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1293. a_reg_dealloc(list,NR_R0);
  1294. (*
  1295. { save pointer to incoming arguments }
  1296. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1297. *)
  1298. (*
  1299. a_reg_alloc(list,R_12);
  1300. { 0 or 8 based on SP alignment }
  1301. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1302. R_12,STACK_POINTER_REG,0,28,28));
  1303. { add in stack length }
  1304. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1305. -localsize));
  1306. { establish new alignment }
  1307. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1308. a_reg_dealloc(list,R_12);
  1309. *)
  1310. { allocate stack frame }
  1311. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1312. inc(localsize,tg.lasttemp);
  1313. localsize:=align(localsize,16);
  1314. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1315. if (localsize <> 0) then
  1316. begin
  1317. if (localsize <= high(smallint)) then
  1318. begin
  1319. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1320. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1321. end
  1322. else
  1323. begin
  1324. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1325. href.index := NR_R11;
  1326. a_reg_alloc(list,href.index);
  1327. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1328. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1329. a_reg_dealloc(list,href.index);
  1330. end;
  1331. end;
  1332. end;
  1333. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1334. (* NOT IN USE *)
  1335. var
  1336. href : treference;
  1337. begin
  1338. a_reg_alloc(list,NR_R0);
  1339. { restore stack pointer }
  1340. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1341. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1342. (*
  1343. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1344. *)
  1345. { restore the CR if necessary from callers frame
  1346. ( !!! always done currently ) }
  1347. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1348. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1349. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1350. a_reg_dealloc(list,NR_R0);
  1351. (*
  1352. { restore return address from callers frame }
  1353. reference_reset_base(href,STACK_POINTER_REG,8);
  1354. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1355. *)
  1356. { restore non-volatile registers from callers frame }
  1357. restore_regs(list);
  1358. (*
  1359. { return to caller }
  1360. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1361. list.concat(taicpu.op_none(A_BLR));
  1362. *)
  1363. { restore return address from callers frame }
  1364. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1365. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1366. { return to caller }
  1367. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1368. list.concat(taicpu.op_none(A_BLR));
  1369. end;
  1370. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1371. begin
  1372. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1373. end;
  1374. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1375. var
  1376. ref2, tmpref: treference;
  1377. freereg: boolean;
  1378. tmpreg:Tregister;
  1379. begin
  1380. ref2 := ref;
  1381. freereg := fixref(list,ref2);
  1382. if assigned(ref2.symbol) then
  1383. begin
  1384. if target_info.system = system_powerpc_macos then
  1385. begin
  1386. if macos_direct_globals then
  1387. begin
  1388. reference_reset(tmpref);
  1389. tmpref.offset := ref2.offset;
  1390. tmpref.symbol := ref2.symbol;
  1391. tmpref.base := NR_NO;
  1392. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1393. end
  1394. else
  1395. begin
  1396. reference_reset(tmpref);
  1397. tmpref.symbol := ref2.symbol;
  1398. tmpref.offset := 0;
  1399. tmpref.base := NR_RTOC;
  1400. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1401. if ref2.offset <> 0 then
  1402. begin
  1403. reference_reset(tmpref);
  1404. tmpref.offset := ref2.offset;
  1405. tmpref.base:= r;
  1406. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1407. end;
  1408. end;
  1409. if ref2.base <> NR_NO then
  1410. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1411. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1412. end
  1413. else
  1414. begin
  1415. { add the symbol's value to the base of the reference, and if the }
  1416. { reference doesn't have a base, create one }
  1417. reference_reset(tmpref);
  1418. tmpref.offset := ref2.offset;
  1419. tmpref.symbol := ref2.symbol;
  1420. tmpref.symaddr := refs_ha;
  1421. if ref2.base<> NR_NO then
  1422. begin
  1423. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1424. ref2.base,tmpref));
  1425. if freereg then
  1426. begin
  1427. rg.ungetregisterint(list,ref2.base);
  1428. freereg := false;
  1429. end;
  1430. end
  1431. else
  1432. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1433. tmpref.base := NR_NO;
  1434. tmpref.symaddr := refs_l;
  1435. { can be folded with one of the next instructions by the }
  1436. { optimizer probably }
  1437. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1438. end
  1439. end
  1440. else if ref2.offset <> 0 Then
  1441. if ref2.base <> NR_NO then
  1442. a_op_const_reg_reg(list,OP_ADD,OS_32,aword(ref2.offset),ref2.base,r)
  1443. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1444. { occurs, so now only ref.offset has to be loaded }
  1445. else
  1446. a_load_const_reg(list,OS_32,ref2.offset,r)
  1447. else if ref.index <> NR_NO Then
  1448. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1449. else if (ref2.base <> NR_NO) and
  1450. (r <> ref2.base) then
  1451. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1452. if freereg then
  1453. rg.ungetregisterint(list,ref2.base);
  1454. end;
  1455. { ************* concatcopy ************ }
  1456. {$ifndef ppc603}
  1457. const
  1458. maxmoveunit = 8;
  1459. {$else ppc603}
  1460. const
  1461. maxmoveunit = 4;
  1462. {$endif ppc603}
  1463. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1464. var
  1465. countreg: TRegister;
  1466. src, dst: TReference;
  1467. lab: tasmlabel;
  1468. count, count2: aword;
  1469. orgsrc, orgdst: boolean;
  1470. size: tcgsize;
  1471. begin
  1472. {$ifdef extdebug}
  1473. if len > high(longint) then
  1474. internalerror(2002072704);
  1475. {$endif extdebug}
  1476. { make sure short loads are handled as optimally as possible }
  1477. if not loadref then
  1478. if (len <= maxmoveunit) and
  1479. (byte(len) in [1,2,4,8]) then
  1480. begin
  1481. if len < 8 then
  1482. begin
  1483. size := int_cgsize(len);
  1484. a_load_ref_ref(list,size,size,source,dest);
  1485. if delsource then
  1486. begin
  1487. reference_release(list,source);
  1488. tg.ungetiftemp(list,source);
  1489. end;
  1490. end
  1491. else
  1492. begin
  1493. a_reg_alloc(list,NR_F0);
  1494. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1495. if delsource then
  1496. begin
  1497. reference_release(list,source);
  1498. tg.ungetiftemp(list,source);
  1499. end;
  1500. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1501. a_reg_dealloc(list,NR_F0);
  1502. end;
  1503. exit;
  1504. end;
  1505. count := len div maxmoveunit;
  1506. reference_reset(src);
  1507. reference_reset(dst);
  1508. { load the address of source into src.base }
  1509. if loadref then
  1510. begin
  1511. src.base := rg.getregisterint(list,OS_ADDR);
  1512. a_load_ref_reg(list,OS_32,OS_32,source,src.base);
  1513. orgsrc := false;
  1514. end
  1515. else if (count > 4) or
  1516. not issimpleref(source) or
  1517. ((source.index <> NR_NO) and
  1518. ((source.offset + longint(len)) > high(smallint))) then
  1519. begin
  1520. src.base := rg.getregisterint(list,OS_ADDR);
  1521. a_loadaddr_ref_reg(list,source,src.base);
  1522. orgsrc := false;
  1523. end
  1524. else
  1525. begin
  1526. src := source;
  1527. orgsrc := true;
  1528. end;
  1529. if not orgsrc and delsource then
  1530. reference_release(list,source);
  1531. { load the address of dest into dst.base }
  1532. if (count > 4) or
  1533. not issimpleref(dest) or
  1534. ((dest.index <> NR_NO) and
  1535. ((dest.offset + longint(len)) > high(smallint))) then
  1536. begin
  1537. dst.base := rg.getregisterint(list,OS_ADDR);
  1538. a_loadaddr_ref_reg(list,dest,dst.base);
  1539. orgdst := false;
  1540. end
  1541. else
  1542. begin
  1543. dst := dest;
  1544. orgdst := true;
  1545. end;
  1546. {$ifndef ppc603}
  1547. if count > 4 then
  1548. { generate a loop }
  1549. begin
  1550. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1551. { have to be set to 8. I put an Inc there so debugging may be }
  1552. { easier (should offset be different from zero here, it will be }
  1553. { easy to notice in the generated assembler }
  1554. inc(dst.offset,8);
  1555. inc(src.offset,8);
  1556. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1557. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1558. countreg := rg.getregisterint(list,OS_INT);
  1559. a_load_const_reg(list,OS_32,count,countreg);
  1560. { explicitely allocate R_0 since it can be used safely here }
  1561. { (for holding date that's being copied) }
  1562. a_reg_alloc(list,NR_F0);
  1563. objectlibrary.getlabel(lab);
  1564. a_label(list, lab);
  1565. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1566. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1567. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1568. a_jmp(list,A_BC,C_NE,0,lab);
  1569. rg.ungetregisterint(list,countreg);
  1570. a_reg_dealloc(list,NR_F0);
  1571. len := len mod 8;
  1572. end;
  1573. count := len div 8;
  1574. if count > 0 then
  1575. { unrolled loop }
  1576. begin
  1577. a_reg_alloc(list,NR_F0);
  1578. for count2 := 1 to count do
  1579. begin
  1580. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1581. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1582. inc(src.offset,8);
  1583. inc(dst.offset,8);
  1584. end;
  1585. a_reg_dealloc(list,NR_F0);
  1586. len := len mod 8;
  1587. end;
  1588. if (len and 4) <> 0 then
  1589. begin
  1590. a_reg_alloc(list,NR_R0);
  1591. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1592. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1593. inc(src.offset,4);
  1594. inc(dst.offset,4);
  1595. a_reg_dealloc(list,NR_R0);
  1596. end;
  1597. {$else not ppc603}
  1598. if count > 4 then
  1599. { generate a loop }
  1600. begin
  1601. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1602. { have to be set to 4. I put an Inc there so debugging may be }
  1603. { easier (should offset be different from zero here, it will be }
  1604. { easy to notice in the generated assembler }
  1605. inc(dst.offset,4);
  1606. inc(src.offset,4);
  1607. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1608. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1609. countreg := rg.getregisterint(list,OS_INT);
  1610. a_load_const_reg(list,OS_32,count,countreg);
  1611. { explicitely allocate R_0 since it can be used safely here }
  1612. { (for holding date that's being copied) }
  1613. a_reg_alloc(list,NR_R0);
  1614. objectlibrary.getlabel(lab);
  1615. a_label(list, lab);
  1616. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1617. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1618. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1619. a_jmp(list,A_BC,C_NE,0,lab);
  1620. rg.ungetregisterint(list,countreg);
  1621. a_reg_dealloc(list,NR_R0);
  1622. len := len mod 4;
  1623. end;
  1624. count := len div 4;
  1625. if count > 0 then
  1626. { unrolled loop }
  1627. begin
  1628. a_reg_alloc(list,NR_R0);
  1629. for count2 := 1 to count do
  1630. begin
  1631. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1632. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1633. inc(src.offset,4);
  1634. inc(dst.offset,4);
  1635. end;
  1636. a_reg_dealloc(list,r);
  1637. len := len mod 4;
  1638. end;
  1639. {$endif not ppc603}
  1640. { copy the leftovers }
  1641. if (len and 2) <> 0 then
  1642. begin
  1643. a_reg_alloc(list,NR_R0);
  1644. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1645. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1646. inc(src.offset,2);
  1647. inc(dst.offset,2);
  1648. a_reg_dealloc(list,NR_R0);
  1649. end;
  1650. if (len and 1) <> 0 then
  1651. begin
  1652. a_reg_alloc(list,NR_R0);
  1653. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1654. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1655. a_reg_dealloc(list,NR_R0);
  1656. end;
  1657. if orgsrc then
  1658. begin
  1659. if delsource then
  1660. reference_release(list,source);
  1661. end
  1662. else
  1663. rg.ungetregisterint(list,src.base);
  1664. if not orgdst then
  1665. rg.ungetregisterint(list,dst.base);
  1666. if delsource then
  1667. tg.ungetiftemp(list,source);
  1668. end;
  1669. procedure tcgppc.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  1670. var
  1671. power,len : longint;
  1672. {$ifndef __NOWINPECOFF__}
  1673. again,ok : tasmlabel;
  1674. {$endif}
  1675. // r,r2,rsp:Tregister;
  1676. begin
  1677. {$warning !!!! FIX ME !!!!}
  1678. internalerror(200305231);
  1679. (* !!!!
  1680. lenref:=ref;
  1681. inc(lenref.offset,4);
  1682. { get stack space }
  1683. r.enum:=R_INTREGISTER;
  1684. r.number:=NR_EDI;
  1685. rsp.enum:=R_INTREGISTER;
  1686. rsp.number:=NR_ESP;
  1687. r2.enum:=R_INTREGISTER;
  1688. rg.getexplicitregisterint(list,NR_EDI);
  1689. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1690. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1691. if (elesize<>1) then
  1692. begin
  1693. if ispowerof2(elesize, power) then
  1694. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1695. else
  1696. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1697. end;
  1698. {$ifndef __NOWINPECOFF__}
  1699. { windows guards only a few pages for stack growing, }
  1700. { so we have to access every page first }
  1701. if target_info.system=system_i386_win32 then
  1702. begin
  1703. objectlibrary.getlabel(again);
  1704. objectlibrary.getlabel(ok);
  1705. a_label(list,again);
  1706. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1707. a_jmp_cond(list,OC_B,ok);
  1708. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1709. r2.number:=NR_EAX;
  1710. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1711. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1712. a_jmp_always(list,again);
  1713. a_label(list,ok);
  1714. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1715. rg.ungetregisterint(list,r);
  1716. { now reload EDI }
  1717. rg.getexplicitregisterint(list,NR_EDI);
  1718. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1719. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1720. if (elesize<>1) then
  1721. begin
  1722. if ispowerof2(elesize, power) then
  1723. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1724. else
  1725. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1726. end;
  1727. end
  1728. else
  1729. {$endif __NOWINPECOFF__}
  1730. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1731. { align stack on 4 bytes }
  1732. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1733. { load destination }
  1734. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1735. { don't destroy the registers! }
  1736. r2.number:=NR_ECX;
  1737. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1738. r2.number:=NR_ESI;
  1739. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1740. { load count }
  1741. r2.number:=NR_ECX;
  1742. a_load_ref_reg(list,OS_INT,lenref,r2);
  1743. { load source }
  1744. r2.number:=NR_ESI;
  1745. a_load_ref_reg(list,OS_INT,ref,r2);
  1746. { scheduled .... }
  1747. r2.number:=NR_ECX;
  1748. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1749. { calculate size }
  1750. len:=elesize;
  1751. opsize:=S_B;
  1752. if (len and 3)=0 then
  1753. begin
  1754. opsize:=S_L;
  1755. len:=len shr 2;
  1756. end
  1757. else
  1758. if (len and 1)=0 then
  1759. begin
  1760. opsize:=S_W;
  1761. len:=len shr 1;
  1762. end;
  1763. if ispowerof2(len, power) then
  1764. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1765. else
  1766. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1767. list.concat(Taicpu.op_none(A_REP,S_NO));
  1768. case opsize of
  1769. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1770. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1771. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1772. end;
  1773. rg.ungetregisterint(list,r);
  1774. r2.number:=NR_ESI;
  1775. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1776. r2.number:=NR_ECX;
  1777. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1778. { patch the new address }
  1779. a_load_reg_ref(list,OS_INT,rsp,ref);
  1780. !!!! *)
  1781. end;
  1782. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1783. var
  1784. hl : tasmlabel;
  1785. begin
  1786. if not(cs_check_overflow in aktlocalswitches) then
  1787. exit;
  1788. objectlibrary.getlabel(hl);
  1789. if not ((def.deftype=pointerdef) or
  1790. ((def.deftype=orddef) and
  1791. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1792. bool8bit,bool16bit,bool32bit]))) then
  1793. begin
  1794. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1795. a_jmp(list,A_BC,C_OV,7,hl)
  1796. end
  1797. else
  1798. a_jmp_cond(list,OC_AE,hl);
  1799. a_call_name(list,'FPC_OVERFLOW');
  1800. a_label(list,hl);
  1801. end;
  1802. {***************** This is private property, keep out! :) *****************}
  1803. function tcgppc.issimpleref(const ref: treference): boolean;
  1804. begin
  1805. if (ref.base = NR_NO) and
  1806. (ref.index <> NR_NO) then
  1807. internalerror(200208101);
  1808. result :=
  1809. not(assigned(ref.symbol)) and
  1810. (((ref.index = NR_NO) and
  1811. (ref.offset >= low(smallint)) and
  1812. (ref.offset <= high(smallint))) or
  1813. ((ref.index <> NR_NO) and
  1814. (ref.offset = 0)));
  1815. end;
  1816. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1817. var
  1818. tmpreg: tregister;
  1819. orgindex: tregister;
  1820. freeindex: boolean;
  1821. begin
  1822. result := false;
  1823. if (ref.base = NR_NO) then
  1824. begin
  1825. ref.base := ref.index;
  1826. ref.base := NR_NO;
  1827. end;
  1828. if (ref.base <> NR_NO) then
  1829. begin
  1830. if (ref.index <> NR_NO) and
  1831. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1832. begin
  1833. result := true;
  1834. { references are often freed before they are used. Since we allocate }
  1835. { a register here, we must first reallocate the index register, since }
  1836. { otherwise it may be overwritten (and it's still used afterwards) }
  1837. freeindex := false;
  1838. if (getsupreg(ref.index) >= first_int_supreg) and
  1839. (getsupreg(ref.index) in rg.unusedregsint) then
  1840. begin
  1841. rg.getexplicitregisterint(list,ref.index);
  1842. orgindex := ref.index;
  1843. freeindex := true;
  1844. end;
  1845. tmpreg := rg.getregisterint(list,OS_ADDR);
  1846. if not assigned(ref.symbol) and
  1847. (cardinal(ref.offset-low(smallint)) <=
  1848. high(smallint)-low(smallint)) then
  1849. begin
  1850. list.concat(taicpu.op_reg_reg_const(
  1851. A_ADDI,tmpreg,ref.base,ref.offset));
  1852. ref.offset := 0;
  1853. end
  1854. else
  1855. begin
  1856. list.concat(taicpu.op_reg_reg_reg(
  1857. A_ADD,tmpreg,ref.base,ref.index));
  1858. ref.index := NR_NO;
  1859. end;
  1860. ref.base := tmpreg;
  1861. if freeindex then
  1862. rg.ungetregisterint(list,orgindex);
  1863. end
  1864. end
  1865. else
  1866. if ref.index <> NR_NO then
  1867. internalerror(200208102);
  1868. end;
  1869. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1870. { that's the case, we can use rlwinm to do an AND operation }
  1871. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1872. var
  1873. temp : longint;
  1874. testbit : aword;
  1875. compare: boolean;
  1876. begin
  1877. get_rlwi_const := false;
  1878. if (a = 0) or (a = $ffffffff) then
  1879. exit;
  1880. { start with the lowest bit }
  1881. testbit := 1;
  1882. { check its value }
  1883. compare := boolean(a and testbit);
  1884. { find out how long the run of bits with this value is }
  1885. { (it's impossible that all bits are 1 or 0, because in that case }
  1886. { this function wouldn't have been called) }
  1887. l1 := 31;
  1888. while (((a and testbit) <> 0) = compare) do
  1889. begin
  1890. testbit := testbit shl 1;
  1891. dec(l1);
  1892. end;
  1893. { check the length of the run of bits that comes next }
  1894. compare := not compare;
  1895. l2 := l1;
  1896. while (((a and testbit) <> 0) = compare) and
  1897. (l2 >= 0) do
  1898. begin
  1899. testbit := testbit shl 1;
  1900. dec(l2);
  1901. end;
  1902. { and finally the check whether the rest of the bits all have the }
  1903. { same value }
  1904. compare := not compare;
  1905. temp := l2;
  1906. if temp >= 0 then
  1907. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1908. exit;
  1909. { we have done "not(not(compare))", so compare is back to its }
  1910. { initial value. If the lowest bit was 0, a is of the form }
  1911. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1912. { because l2 now contains the position of the last zero of the }
  1913. { first run instead of that of the first 1) so switch l1 and l2 }
  1914. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1915. if not compare then
  1916. begin
  1917. temp := l1;
  1918. l1 := l2+1;
  1919. l2 := temp;
  1920. end
  1921. else
  1922. { otherwise, l1 currently contains the position of the last }
  1923. { zero instead of that of the first 1 of the second run -> +1 }
  1924. inc(l1);
  1925. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1926. l1 := l1 and 31;
  1927. l2 := l2 and 31;
  1928. get_rlwi_const := true;
  1929. end;
  1930. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1931. ref: treference);
  1932. var
  1933. tmpreg: tregister;
  1934. tmpregUsed: Boolean;
  1935. tmpref: treference;
  1936. largeOffset: Boolean;
  1937. begin
  1938. tmpreg := NR_NO;
  1939. if target_info.system = system_powerpc_macos then
  1940. begin
  1941. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1942. high(smallint)-low(smallint));
  1943. tmpreg := rg.getregisterint(list,OS_ADDR);
  1944. tmpregUsed:= false;
  1945. if assigned(ref.symbol) then
  1946. begin //Load symbol's value
  1947. reference_reset(tmpref);
  1948. tmpref.symbol := ref.symbol;
  1949. tmpref.base := NR_RTOC;
  1950. if macos_direct_globals then
  1951. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1952. else
  1953. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1954. tmpregUsed:= true;
  1955. end;
  1956. if largeOffset then
  1957. begin //Add hi part of offset
  1958. reference_reset(tmpref);
  1959. tmpref.offset := Hi(ref.offset);
  1960. if tmpregUsed then
  1961. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1962. tmpreg,tmpref))
  1963. else
  1964. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1965. tmpregUsed:= true;
  1966. end;
  1967. if tmpregUsed then
  1968. begin
  1969. //Add content of base register
  1970. if ref.base <> NR_NO then
  1971. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1972. ref.base,tmpreg));
  1973. //Make ref ready to be used by op
  1974. ref.symbol:= nil;
  1975. ref.base:= tmpreg;
  1976. if largeOffset then
  1977. ref.offset := Lo(ref.offset);
  1978. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1979. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1980. end
  1981. else
  1982. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1983. end
  1984. else {if target_info.system <> system_powerpc_macos}
  1985. begin
  1986. if assigned(ref.symbol) or
  1987. (cardinal(ref.offset-low(smallint)) >
  1988. high(smallint)-low(smallint)) then
  1989. begin
  1990. tmpreg := rg.getregisterint(list,OS_ADDR);
  1991. reference_reset(tmpref);
  1992. tmpref.symbol := ref.symbol;
  1993. tmpref.offset := ref.offset;
  1994. tmpref.symaddr := refs_ha;
  1995. if ref.base <> NR_NO then
  1996. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1997. ref.base,tmpref))
  1998. else
  1999. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  2000. ref.base := tmpreg;
  2001. ref.symaddr := refs_l;
  2002. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2003. end
  2004. else
  2005. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2006. end;
  2007. if (tmpreg <> NR_NO) then
  2008. rg.ungetregisterint(list,tmpreg);
  2009. end;
  2010. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  2011. crval: longint; l: tasmlabel);
  2012. var
  2013. p: taicpu;
  2014. begin
  2015. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  2016. if op <> A_B then
  2017. create_cond_norm(c,crval,p.condition);
  2018. p.is_jmp := true;
  2019. list.concat(p)
  2020. end;
  2021. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2022. begin
  2023. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2024. end;
  2025. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  2026. begin
  2027. a_op64_const_reg_reg(list,op,value,reg,reg);
  2028. end;
  2029. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2030. begin
  2031. case op of
  2032. OP_AND,OP_OR,OP_XOR:
  2033. begin
  2034. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2035. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2036. end;
  2037. OP_ADD:
  2038. begin
  2039. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2040. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2041. end;
  2042. OP_SUB:
  2043. begin
  2044. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2045. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2046. end;
  2047. else
  2048. internalerror(2002072801);
  2049. end;
  2050. end;
  2051. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  2052. const
  2053. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2054. (A_SUBIC,A_SUBC,A_ADDME));
  2055. var
  2056. tmpreg: tregister;
  2057. tmpreg64: tregister64;
  2058. issub: boolean;
  2059. begin
  2060. case op of
  2061. OP_AND,OP_OR,OP_XOR:
  2062. begin
  2063. cg.a_op_const_reg_reg(list,op,OS_32,aword(value),regsrc.reglo,regdst.reglo);
  2064. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2065. regdst.reghi);
  2066. end;
  2067. OP_ADD, OP_SUB:
  2068. begin
  2069. if (int64(value) < 0) then
  2070. begin
  2071. if op = OP_ADD then
  2072. op := OP_SUB
  2073. else
  2074. op := OP_ADD;
  2075. int64(value) := -int64(value);
  2076. end;
  2077. if (longint(value) <> 0) then
  2078. begin
  2079. issub := op = OP_SUB;
  2080. if (int64(value) > 0) and
  2081. (int64(value)-ord(issub) <= 32767) then
  2082. begin
  2083. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2084. regdst.reglo,regsrc.reglo,longint(value)));
  2085. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2086. regdst.reghi,regsrc.reghi));
  2087. end
  2088. else if ((value shr 32) = 0) then
  2089. begin
  2090. tmpreg := rg.getregisterint(list,OS_32);
  2091. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2092. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2093. regdst.reglo,regsrc.reglo,tmpreg));
  2094. rg.ungetregisterint(list,tmpreg);
  2095. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2096. regdst.reghi,regsrc.reghi));
  2097. end
  2098. else
  2099. begin
  2100. tmpreg64.reglo := rg.getregisterint(list,OS_32);
  2101. tmpreg64.reghi := rg.getregisterint(list,OS_32);
  2102. a_load64_const_reg(list,value,tmpreg64);
  2103. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2104. rg.ungetregisterint(list,tmpreg64.reglo);
  2105. rg.ungetregisterint(list,tmpreg64.reghi);
  2106. end
  2107. end
  2108. else
  2109. begin
  2110. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2111. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2112. regdst.reghi);
  2113. end;
  2114. end;
  2115. else
  2116. internalerror(2002072802);
  2117. end;
  2118. end;
  2119. begin
  2120. cg := tcgppc.create;
  2121. cg64 :=tcg64fppc.create;
  2122. end.
  2123. {
  2124. $Log$
  2125. Revision 1.126 2003-09-14 16:37:20 jonas
  2126. * fixed some ppc problems
  2127. Revision 1.125 2003/09/03 21:04:14 peter
  2128. * some fixes for ppc
  2129. Revision 1.124 2003/09/03 19:35:24 peter
  2130. * powerpc compiles again
  2131. Revision 1.123 2003/09/03 15:55:01 peter
  2132. * NEWRA branch merged
  2133. Revision 1.122.2.1 2003/08/31 21:08:16 peter
  2134. * first batch of sparc fixes
  2135. Revision 1.122 2003/08/18 21:27:00 jonas
  2136. * some newra optimizations (eliminate lots of moves between registers)
  2137. Revision 1.121 2003/08/18 11:50:55 olle
  2138. + cleaning up in proc entry and exit, now calc_stack_frame always is used.
  2139. Revision 1.120 2003/08/17 16:59:20 jonas
  2140. * fixed regvars so they work with newra (at least for ppc)
  2141. * fixed some volatile register bugs
  2142. + -dnotranslation option for -dnewra, which causes the registers not to
  2143. be translated from virtual to normal registers. Requires support in
  2144. the assembler writer as well, which is only implemented in aggas/
  2145. agppcgas currently
  2146. Revision 1.119 2003/08/11 21:18:20 peter
  2147. * start of sparc support for newra
  2148. Revision 1.118 2003/08/08 15:50:45 olle
  2149. * merged macos entry/exit code generation into the general one.
  2150. Revision 1.117 2002/10/01 05:24:28 olle
  2151. * made a_load_store more robust and to accept large offsets and cleaned up code
  2152. Revision 1.116 2003/07/23 11:02:23 jonas
  2153. * don't use rg.getregisterint() anymore in g_stackframe_entry_*, because
  2154. the register colouring has already occurred then, use a hard-coded
  2155. register instead
  2156. Revision 1.115 2003/07/20 20:39:20 jonas
  2157. * fixed newra bug due to the fact that we sometimes need a temp reg
  2158. when loading/storing to memory (base+index+offset is not possible)
  2159. and because a reference is often freed before it is last used, this
  2160. temp register was soemtimes the same as one of the reference regs
  2161. Revision 1.114 2003/07/20 16:15:58 jonas
  2162. * fixed bug in g_concatcopy with -dnewra
  2163. Revision 1.113 2003/07/06 20:25:03 jonas
  2164. * fixed ppc compiler
  2165. Revision 1.112 2003/07/05 20:11:42 jonas
  2166. * create_paraloc_info() is now called separately for the caller and
  2167. callee info
  2168. * fixed ppc cycle
  2169. Revision 1.111 2003/07/02 22:18:04 peter
  2170. * paraloc splitted in callerparaloc,calleeparaloc
  2171. * sparc calling convention updates
  2172. Revision 1.110 2003/06/18 10:12:36 olle
  2173. * macos: fixes of loading-code
  2174. Revision 1.109 2003/06/14 22:32:43 jonas
  2175. * ppc compiles with -dnewra, haven't tried to compile anything with it
  2176. yet though
  2177. Revision 1.108 2003/06/13 21:19:31 peter
  2178. * current_procdef removed, use current_procinfo.procdef instead
  2179. Revision 1.107 2003/06/09 14:54:26 jonas
  2180. * (de)allocation of registers for parameters is now performed properly
  2181. (and checked on the ppc)
  2182. - removed obsolete allocation of all parameter registers at the start
  2183. of a procedure (and deallocation at the end)
  2184. Revision 1.106 2003/06/08 18:19:27 jonas
  2185. - removed duplicate identifier
  2186. Revision 1.105 2003/06/07 18:57:04 jonas
  2187. + added freeintparaloc
  2188. * ppc get/freeintparaloc now check whether the parameter regs are
  2189. properly allocated/deallocated (and get an extra list para)
  2190. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  2191. * fixed lot of missing pi_do_call's
  2192. Revision 1.104 2003/06/04 11:58:58 jonas
  2193. * calculate localsize also in g_return_from_proc since it's now called
  2194. before g_stackframe_entry (still have to fix macos)
  2195. * compilation fixes (cycle doesn't work yet though)
  2196. Revision 1.103 2003/06/01 21:38:06 peter
  2197. * getregisterfpu size parameter added
  2198. * op_const_reg size parameter added
  2199. * sparc updates
  2200. Revision 1.102 2003/06/01 13:42:18 jonas
  2201. * fix for bug in fixref that Peter found during the Sparc conversion
  2202. Revision 1.101 2003/05/30 18:52:10 jonas
  2203. * fixed bug with intregvars
  2204. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2205. rcgppc.a_param_ref, which previously got bogus size values
  2206. Revision 1.100 2003/05/29 21:17:27 jonas
  2207. * compile with -dppc603 to not use unaligned float loads in move() and
  2208. g_concatcopy, because the 603 and 604 take an exception for those
  2209. (and netbsd doesn't even handle those in the kernel). There are
  2210. still some of those left that could cause problems though (e.g.
  2211. in the set helpers)
  2212. Revision 1.99 2003/05/29 10:06:09 jonas
  2213. * also free temps in g_concatcopy if delsource is true
  2214. Revision 1.98 2003/05/28 23:58:18 jonas
  2215. * added missing initialization of rg.usedintin,byproc
  2216. * ppc now also saves/restores used fpu registers
  2217. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  2218. i386
  2219. Revision 1.97 2003/05/28 23:18:31 florian
  2220. * started to fix and clean up the sparc port
  2221. Revision 1.96 2003/05/24 11:59:42 jonas
  2222. * fixed integer typeconversion problems
  2223. Revision 1.95 2003/05/23 18:51:26 jonas
  2224. * fixed support for nested procedures and more parameters than those
  2225. which fit in registers (untested/probably not working: calling a
  2226. nested procedure from a deeper nested procedure)
  2227. Revision 1.94 2003/05/20 23:54:00 florian
  2228. + basic darwin support added
  2229. Revision 1.93 2003/05/15 22:14:42 florian
  2230. * fixed last commit, changing lastsaveintreg to r31 caused some strange problems
  2231. Revision 1.92 2003/05/15 21:37:00 florian
  2232. * sysv entry code saves r13 now as well
  2233. Revision 1.91 2003/05/15 19:39:09 florian
  2234. * fixed ppc compiler which was broken by Peter's changes
  2235. Revision 1.90 2003/05/12 18:43:50 jonas
  2236. * fixed g_concatcopy
  2237. Revision 1.89 2003/05/11 20:59:23 jonas
  2238. * fixed bug with large offsets in entrycode
  2239. Revision 1.88 2003/05/11 11:45:08 jonas
  2240. * fixed shifts
  2241. Revision 1.87 2003/05/11 11:07:33 jonas
  2242. * fixed optimizations in a_op_const_reg_reg()
  2243. Revision 1.86 2003/04/27 11:21:36 peter
  2244. * aktprocdef renamed to current_procinfo.procdef
  2245. * procinfo renamed to current_procinfo
  2246. * procinfo will now be stored in current_module so it can be
  2247. cleaned up properly
  2248. * gen_main_procsym changed to create_main_proc and release_main_proc
  2249. to also generate a tprocinfo structure
  2250. * fixed unit implicit initfinal
  2251. Revision 1.85 2003/04/26 22:56:11 jonas
  2252. * fix to a_op64_const_reg_reg
  2253. Revision 1.84 2003/04/26 16:08:41 jonas
  2254. * fixed g_flags2reg
  2255. Revision 1.83 2003/04/26 15:25:29 florian
  2256. * fixed cmp_reg_reg_reg, cmp operands were emitted in the wrong order
  2257. Revision 1.82 2003/04/25 20:55:34 florian
  2258. * stack frame calculations are now completly done using the code generator
  2259. routines instead of generating directly assembler so also large stack frames
  2260. are handle properly
  2261. Revision 1.81 2003/04/24 11:24:00 florian
  2262. * fixed several issues with nested procedures
  2263. Revision 1.80 2003/04/23 22:18:01 peter
  2264. * fixes to get rtl compiled
  2265. Revision 1.79 2003/04/23 12:35:35 florian
  2266. * fixed several issues with powerpc
  2267. + applied a patch from Jonas for nested function calls (PowerPC only)
  2268. * ...
  2269. Revision 1.78 2003/04/16 09:26:55 jonas
  2270. * assembler procedures now again get a stackframe if they have local
  2271. variables. No space is reserved for a function result however.
  2272. Also, the register parameters aren't automatically saved on the stack
  2273. anymore in assembler procedures.
  2274. Revision 1.77 2003/04/06 16:39:11 jonas
  2275. * don't generate entry/exit code for assembler procedures
  2276. Revision 1.76 2003/03/22 18:01:13 jonas
  2277. * fixed linux entry/exit code generation
  2278. Revision 1.75 2003/03/19 14:26:26 jonas
  2279. * fixed R_TOC bugs introduced by new register allocator conversion
  2280. Revision 1.74 2003/03/13 22:57:45 olle
  2281. * change in a_loadaddr_ref_reg
  2282. Revision 1.73 2003/03/12 22:43:38 jonas
  2283. * more powerpc and generic fixes related to the new register allocator
  2284. Revision 1.72 2003/03/11 21:46:24 jonas
  2285. * lots of new regallocator fixes, both in generic and ppc-specific code
  2286. (ppc compiler still can't compile the linux system unit though)
  2287. Revision 1.71 2003/02/19 22:00:16 daniel
  2288. * Code generator converted to new register notation
  2289. - Horribily outdated todo.txt removed
  2290. Revision 1.70 2003/01/13 17:17:50 olle
  2291. * changed global var access, TOC now contain pointers to globals
  2292. * fixed handling of function pointers
  2293. Revision 1.69 2003/01/09 22:00:53 florian
  2294. * fixed some PowerPC issues
  2295. Revision 1.68 2003/01/08 18:43:58 daniel
  2296. * Tregister changed into a record
  2297. Revision 1.67 2002/12/15 19:22:01 florian
  2298. * fixed some crashes and a rte 201
  2299. Revision 1.66 2002/11/28 10:55:16 olle
  2300. * macos: changing code gen for references to globals
  2301. Revision 1.65 2002/11/07 15:50:23 jonas
  2302. * fixed bctr(l) problems
  2303. Revision 1.64 2002/11/04 18:24:19 olle
  2304. * macos: globals are located in TOC and relative r2, instead of absolute
  2305. Revision 1.63 2002/10/28 22:24:28 olle
  2306. * macos entry/exit: only used registers are saved
  2307. - macos entry/exit: stackptr not saved in r31 anymore
  2308. * macos entry/exit: misc fixes
  2309. Revision 1.62 2002/10/19 23:51:48 olle
  2310. * macos stack frame size computing updated
  2311. + macos epilogue: control register now restored
  2312. * macos prologue and epilogue: fp reg now saved and restored
  2313. Revision 1.61 2002/10/19 12:50:36 olle
  2314. * reorganized prologue and epilogue routines
  2315. Revision 1.60 2002/10/02 21:49:51 florian
  2316. * all A_BL instructions replaced by calls to a_call_name
  2317. Revision 1.59 2002/10/02 13:24:58 jonas
  2318. * changed a_call_* so that no superfluous code is generated anymore
  2319. Revision 1.58 2002/09/17 18:54:06 jonas
  2320. * a_load_reg_reg() now has two size parameters: source and dest. This
  2321. allows some optimizations on architectures that don't encode the
  2322. register size in the register name.
  2323. Revision 1.57 2002/09/10 21:22:25 jonas
  2324. + added some internal errors
  2325. * fixed bug in sysv exit code
  2326. Revision 1.56 2002/09/08 20:11:56 jonas
  2327. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  2328. Revision 1.55 2002/09/08 13:03:26 jonas
  2329. * several large offset-related fixes
  2330. Revision 1.54 2002/09/07 17:54:58 florian
  2331. * first part of PowerPC fixes
  2332. Revision 1.53 2002/09/07 15:25:14 peter
  2333. * old logs removed and tabs fixed
  2334. Revision 1.52 2002/09/02 10:14:51 jonas
  2335. + a_call_reg()
  2336. * small fix in a_call_ref()
  2337. Revision 1.51 2002/09/02 06:09:02 jonas
  2338. * fixed range error
  2339. Revision 1.50 2002/09/01 21:04:49 florian
  2340. * several powerpc related stuff fixed
  2341. Revision 1.49 2002/09/01 12:09:27 peter
  2342. + a_call_reg, a_call_loc added
  2343. * removed exprasmlist references
  2344. Revision 1.48 2002/08/31 21:38:02 jonas
  2345. * fixed a_call_ref (it should load ctr, not lr)
  2346. Revision 1.47 2002/08/31 21:30:45 florian
  2347. * fixed several problems caused by Jonas' commit :)
  2348. Revision 1.46 2002/08/31 19:25:50 jonas
  2349. + implemented a_call_ref()
  2350. Revision 1.45 2002/08/18 22:16:14 florian
  2351. + the ppc gas assembler writer adds now registers aliases
  2352. to the assembler file
  2353. Revision 1.44 2002/08/17 18:23:53 florian
  2354. * some assembler writer bugs fixed
  2355. Revision 1.43 2002/08/17 09:23:49 florian
  2356. * first part of procinfo rewrite
  2357. Revision 1.42 2002/08/16 14:24:59 carl
  2358. * issameref() to test if two references are the same (then emit no opcodes)
  2359. + ret_in_reg to replace ret_in_acc
  2360. (fix some register allocation bugs at the same time)
  2361. + save_std_register now has an extra parameter which is the
  2362. usedinproc registers
  2363. Revision 1.41 2002/08/15 08:13:54 carl
  2364. - a_load_sym_ofs_reg removed
  2365. * loadvmt now calls loadaddr_ref_reg instead
  2366. Revision 1.40 2002/08/11 14:32:32 peter
  2367. * renamed current_library to objectlibrary
  2368. Revision 1.39 2002/08/11 13:24:18 peter
  2369. * saving of asmsymbols in ppu supported
  2370. * asmsymbollist global is removed and moved into a new class
  2371. tasmlibrarydata that will hold the info of a .a file which
  2372. corresponds with a single module. Added librarydata to tmodule
  2373. to keep the library info stored for the module. In the future the
  2374. objectfiles will also be stored to the tasmlibrarydata class
  2375. * all getlabel/newasmsymbol and friends are moved to the new class
  2376. Revision 1.38 2002/08/11 11:39:31 jonas
  2377. + powerpc-specific genlinearlist
  2378. Revision 1.37 2002/08/10 17:15:31 jonas
  2379. * various fixes and optimizations
  2380. Revision 1.36 2002/08/06 20:55:23 florian
  2381. * first part of ppc calling conventions fix
  2382. Revision 1.35 2002/08/06 07:12:05 jonas
  2383. * fixed bug in g_flags2reg()
  2384. * and yet more constant operation fixes :)
  2385. Revision 1.34 2002/08/05 08:58:53 jonas
  2386. * fixed compilation problems
  2387. Revision 1.33 2002/08/04 12:57:55 jonas
  2388. * more misc. fixes, mostly constant-related
  2389. }