cgcpu.pas 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  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. globtype,symtype,
  23. cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,node,cg64f32,rgcpu;
  26. type
  27. tcgppc = class(tcg)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. procedure ungetreference(list:Taasmoutput;const r:Treference);override;
  31. { passing parameters, per default the parameter is pushed }
  32. { nr gives the number of the parameter (enumerated from }
  33. { left to right), this allows to move the parameter to }
  34. { register, if the cpu supports register calling }
  35. { conventions }
  36. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aint;const locpara : tparalocation);override;
  37. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  38. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  39. procedure a_call_name(list : taasmoutput;const s : string);override;
  40. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  41. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; 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: aint; 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 : aint;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 : aint;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_name(list : taasmoutput;const s : string); override;
  61. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  62. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  63. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  64. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);override;
  65. procedure g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean); override;
  66. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  67. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint; delsource,loadref : boolean);override;
  68. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  69. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  70. { that's the case, we can use rlwinm to do an AND operation }
  71. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  72. procedure g_save_standard_registers(list:Taasmoutput);override;
  73. procedure g_restore_standard_registers(list:Taasmoutput);override;
  74. procedure g_save_all_registers(list : taasmoutput);override;
  75. procedure g_restore_all_registers(list : taasmoutput;const funcretparaloc:tparalocation);override;
  76. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  77. private
  78. (* NOT IN USE: *)
  79. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  80. (* NOT IN USE: *)
  81. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  82. { Make sure ref is a valid reference for the PowerPC and sets the }
  83. { base to the value of the index if (base = R_NO). }
  84. { Returns true if the reference contained a base, index and an }
  85. { offset or symbol, in which case the base will have been changed }
  86. { to a tempreg (which has to be freed by the caller) containing }
  87. { the sum of part of the original reference }
  88. function fixref(list: taasmoutput; var ref: treference): boolean;
  89. { returns whether a reference can be used immediately in a powerpc }
  90. { instruction }
  91. function issimpleref(const ref: treference): boolean;
  92. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  93. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  94. ref: treference);
  95. { creates the correct branch instruction for a given combination }
  96. { of asmcondflags and destination addressing mode }
  97. procedure a_jmp(list: taasmoutput; op: tasmop;
  98. c: tasmcondflag; crval: longint; l: tasmlabel);
  99. function save_regs(list : taasmoutput):longint;
  100. procedure restore_regs(list : taasmoutput);
  101. function get_darwin_call_stub(const s: string): tasmsymbol;
  102. end;
  103. tcg64fppc = class(tcg64f32)
  104. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  105. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);override;
  106. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);override;
  107. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  108. end;
  109. const
  110. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  111. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  112. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  113. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  114. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  115. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  116. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  117. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  118. implementation
  119. uses
  120. globals,verbose,systems,cutils,
  121. symconst,symdef,symsym,
  122. rgobj,tgobj,cpupi,procinfo,paramgr,
  123. cgutils;
  124. procedure tcgppc.init_register_allocators;
  125. begin
  126. inherited init_register_allocators;
  127. if target_info.system=system_powerpc_darwin then
  128. begin
  129. if pi_needs_got in current_procinfo.flags then
  130. begin
  131. current_procinfo.got:=NR_R31;
  132. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  133. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  134. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  135. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  136. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  137. RS_R14,RS_R13],first_int_imreg,[]);
  138. end
  139. else
  140. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  141. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  142. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  143. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  144. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  145. RS_R14,RS_R13],first_int_imreg,[]);
  146. end
  147. else
  148. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  149. [RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  150. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,RS_R30,RS_R29,
  151. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  152. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  153. RS_R14,RS_R13],first_int_imreg,[]);
  154. case target_info.abi of
  155. abi_powerpc_aix:
  156. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  157. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  158. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  159. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  160. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  161. abi_powerpc_sysv:
  162. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  163. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  164. RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,RS_F26,RS_F25,RS_F24,RS_F23,
  165. RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,RS_F17,RS_F16,RS_F15,RS_F14,
  166. RS_F13,RS_F12,RS_F11,RS_F10],first_fpu_imreg,[]);
  167. else
  168. internalerror(2003122903);
  169. end;
  170. {$warning FIX ME}
  171. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  172. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  173. end;
  174. procedure tcgppc.done_register_allocators;
  175. begin
  176. rg[R_INTREGISTER].free;
  177. rg[R_FPUREGISTER].free;
  178. rg[R_MMREGISTER].free;
  179. inherited done_register_allocators;
  180. end;
  181. procedure tcgppc.ungetreference(list:Taasmoutput;const r:Treference);
  182. begin
  183. if r.base<>NR_NO then
  184. ungetregister(list,r.base);
  185. if r.index<>NR_NO then
  186. ungetregister(list,r.index);
  187. end;
  188. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aint;const locpara : tparalocation);
  189. var
  190. ref: treference;
  191. begin
  192. case locpara.loc of
  193. LOC_REGISTER,LOC_CREGISTER:
  194. a_load_const_reg(list,size,a,locpara.register);
  195. LOC_REFERENCE:
  196. begin
  197. reference_reset(ref);
  198. ref.base:=locpara.reference.index;
  199. ref.offset:=locpara.reference.offset;
  200. a_load_const_ref(list,size,a,ref);
  201. end;
  202. else
  203. internalerror(2002081101);
  204. end;
  205. end;
  206. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  207. var
  208. ref: treference;
  209. tmpreg: tregister;
  210. begin
  211. case locpara.loc of
  212. LOC_REGISTER,LOC_CREGISTER:
  213. a_load_ref_reg(list,size,size,r,locpara.register);
  214. LOC_REFERENCE:
  215. begin
  216. reference_reset(ref);
  217. ref.base:=locpara.reference.index;
  218. ref.offset:=locpara.reference.offset;
  219. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  220. a_load_ref_reg(list,size,size,r,tmpreg);
  221. a_load_reg_ref(list,size,size,tmpreg,ref);
  222. rg[R_INTREGISTER].ungetregister(list,tmpreg);
  223. end;
  224. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  225. case size of
  226. OS_F32, OS_F64:
  227. a_loadfpu_ref_reg(list,size,r,locpara.register);
  228. else
  229. internalerror(2002072801);
  230. end;
  231. else
  232. internalerror(2002081103);
  233. end;
  234. end;
  235. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  236. var
  237. ref: treference;
  238. tmpreg: tregister;
  239. begin
  240. case locpara.loc of
  241. LOC_REGISTER,LOC_CREGISTER:
  242. a_loadaddr_ref_reg(list,r,locpara.register);
  243. LOC_REFERENCE:
  244. begin
  245. reference_reset(ref);
  246. ref.base := locpara.reference.index;
  247. ref.offset := locpara.reference.offset;
  248. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  249. a_loadaddr_ref_reg(list,r,tmpreg);
  250. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  251. rg[R_INTREGISTER].ungetregister(list,tmpreg);
  252. end;
  253. else
  254. internalerror(2002080701);
  255. end;
  256. end;
  257. function tcgppc.get_darwin_call_stub(const s: string): tasmsymbol;
  258. var
  259. stubname: string;
  260. href: treference;
  261. l1: tasmsymbol;
  262. begin
  263. { function declared in the current unit? }
  264. result := objectlibrary.getasmsymbol(s);
  265. if not(assigned(result)) then
  266. begin
  267. stubname := 'L'+s+'$stub';
  268. result := objectlibrary.getasmsymbol(stubname);
  269. end;
  270. if assigned(result) then
  271. exit;
  272. if not(assigned(importssection)) then
  273. importssection:=TAAsmoutput.create;
  274. importsSection.concat(Tai_section.Create(sec_data,'',0));
  275. importsSection.concat(Tai_direct.create(strpnew('.section __TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16')));
  276. importsSection.concat(Tai_align.Create(4));
  277. result := objectlibrary.newasmsymbol(stubname,AB_EXTERNAL,AT_FUNCTION);
  278. importsSection.concat(Tai_symbol.Create(result,0));
  279. importsSection.concat(Tai_direct.create(strpnew((#9+'.indirect_symbol ')+s)));
  280. l1 := objectlibrary.newasmsymbol('L'+s+'$lazy_ptr',AB_EXTERNAL,AT_FUNCTION);
  281. reference_reset_symbol(href,l1,0);
  282. {$ifdef powerpc}
  283. href.refaddr := addr_hi;
  284. importsSection.concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  285. href.refaddr := addr_lo;
  286. href.base := NR_R11;
  287. importsSection.concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  288. importsSection.concat(taicpu.op_reg(A_MTCTR,NR_R12));
  289. importsSection.concat(taicpu.op_none(A_BCTR));
  290. {$else powerpc}
  291. internalerror(2004010502);
  292. {$endif powerpc}
  293. importsSection.concat(Tai_section.Create(sec_data,'',0));
  294. importsSection.concat(Tai_direct.create(strpnew('.lazy_symbol_pointer')));
  295. importsSection.concat(Tai_symbol.Create(l1,0));
  296. importsSection.concat(Tai_direct.create(strpnew((#9+'.indirect_symbol ')+s)));
  297. importsSection.concat(tai_const.createname(strpnew('dyld_stub_binding_helper'),AT_FUNCTION,0));
  298. end;
  299. { calling a procedure by name }
  300. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  301. var
  302. href : treference;
  303. begin
  304. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  305. if it is a cross-TOC call. If so, it also replaces the NOP
  306. with some restore code.}
  307. if (target_info.system <> system_powerpc_darwin) then
  308. begin
  309. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  310. if target_info.system=system_powerpc_macos then
  311. list.concat(taicpu.op_none(A_NOP));
  312. end
  313. else
  314. begin
  315. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  316. end;
  317. if not(pi_do_call in current_procinfo.flags) then
  318. internalerror(2003060703);
  319. end;
  320. { calling a procedure by address }
  321. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  322. var
  323. tmpreg : tregister;
  324. tmpref : treference;
  325. begin
  326. if target_info.system=system_powerpc_macos then
  327. begin
  328. {Generate instruction to load the procedure address from
  329. the transition vector.}
  330. //TODO: Support cross-TOC calls.
  331. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  332. reference_reset(tmpref);
  333. tmpref.offset := 0;
  334. //tmpref.symaddr := refs_full;
  335. tmpref.base:= reg;
  336. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  337. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  338. rg[R_INTREGISTER].ungetregister(list,tmpreg);
  339. end
  340. else
  341. list.concat(taicpu.op_reg(A_MTCTR,reg));
  342. list.concat(taicpu.op_none(A_BCTRL));
  343. //if target_info.system=system_powerpc_macos then
  344. // //NOP is not needed here.
  345. // list.concat(taicpu.op_none(A_NOP));
  346. if not(pi_do_call in current_procinfo.flags) then
  347. internalerror(2003060704);
  348. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  349. end;
  350. {********************** load instructions ********************}
  351. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aint; reg : TRegister);
  352. begin
  353. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  354. internalerror(2002090902);
  355. if (a >= low(smallint)) and
  356. (a <= high(smallint)) then
  357. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  358. else if ((a and $ffff) <> 0) then
  359. begin
  360. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  361. if ((a shr 16) <> 0) or
  362. (smallint(a and $ffff) < 0) then
  363. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  364. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  365. end
  366. else
  367. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  368. end;
  369. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  370. const
  371. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  372. { indexed? updating?}
  373. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  374. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  375. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  376. var
  377. op: TAsmOp;
  378. ref2: TReference;
  379. freereg: boolean;
  380. begin
  381. ref2 := ref;
  382. freereg := fixref(list,ref2);
  383. if tosize in [OS_S8..OS_S16] then
  384. { storing is the same for signed and unsigned values }
  385. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  386. { 64 bit stuff should be handled separately }
  387. if tosize in [OS_64,OS_S64] then
  388. internalerror(200109236);
  389. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  390. a_load_store(list,op,reg,ref2);
  391. if freereg then
  392. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  393. End;
  394. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  395. const
  396. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  397. { indexed? updating?}
  398. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  399. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  400. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  401. { 64bit stuff should be handled separately }
  402. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  403. { 128bit stuff too }
  404. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  405. { there's no load-byte-with-sign-extend :( }
  406. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  407. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  408. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  409. var
  410. op: tasmop;
  411. tmpreg: tregister;
  412. ref2, tmpref: treference;
  413. freereg: boolean;
  414. begin
  415. { TODO: optimize/take into consideration fromsize/tosize. Will }
  416. { probably only matter for OS_S8 loads though }
  417. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  418. internalerror(2002090902);
  419. ref2 := ref;
  420. freereg := fixref(list,ref2);
  421. { the caller is expected to have adjusted the reference already }
  422. { in this case }
  423. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  424. fromsize := tosize;
  425. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  426. a_load_store(list,op,reg,ref2);
  427. if freereg then
  428. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  429. { sign extend shortint if necessary, since there is no }
  430. { load instruction that does that automatically (JM) }
  431. if fromsize = OS_S8 then
  432. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  433. end;
  434. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  435. var
  436. instr: taicpu;
  437. begin
  438. case tosize of
  439. OS_8:
  440. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  441. reg2,reg1,0,31-8+1,31);
  442. OS_S8:
  443. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  444. OS_16:
  445. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  446. reg2,reg1,0,31-16+1,31);
  447. OS_S16:
  448. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  449. OS_32,OS_S32:
  450. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  451. else internalerror(2002090901);
  452. end;
  453. list.concat(instr);
  454. rg[R_INTREGISTER].add_move_instruction(instr);
  455. end;
  456. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  457. var
  458. instr: taicpu;
  459. begin
  460. instr := taicpu.op_reg_reg(A_FMR,reg2,reg1);
  461. list.concat(instr);
  462. rg[R_FPUREGISTER].add_move_instruction(instr);
  463. end;
  464. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  465. const
  466. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  467. { indexed? updating?}
  468. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  469. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  470. var
  471. op: tasmop;
  472. ref2: treference;
  473. freereg: boolean;
  474. begin
  475. { several functions call this procedure with OS_32 or OS_64 }
  476. { so this makes life easier (FK) }
  477. case size of
  478. OS_32,OS_F32:
  479. size:=OS_F32;
  480. OS_64,OS_F64,OS_C64:
  481. size:=OS_F64;
  482. else
  483. internalerror(200201121);
  484. end;
  485. ref2 := ref;
  486. freereg := fixref(list,ref2);
  487. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  488. a_load_store(list,op,reg,ref2);
  489. if freereg then
  490. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  491. end;
  492. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  493. const
  494. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  495. { indexed? updating?}
  496. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  497. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  498. var
  499. op: tasmop;
  500. ref2: treference;
  501. freereg: boolean;
  502. begin
  503. if not(size in [OS_F32,OS_F64]) then
  504. internalerror(200201122);
  505. ref2 := ref;
  506. freereg := fixref(list,ref2);
  507. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  508. a_load_store(list,op,reg,ref2);
  509. if freereg then
  510. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  511. end;
  512. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  513. begin
  514. a_op_const_reg_reg(list,op,size,a,reg,reg);
  515. end;
  516. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  517. begin
  518. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  519. end;
  520. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  521. size: tcgsize; a: aint; src, dst: tregister);
  522. var
  523. l1,l2: longint;
  524. oplo, ophi: tasmop;
  525. scratchreg: tregister;
  526. useReg, gotrlwi: boolean;
  527. procedure do_lo_hi;
  528. begin
  529. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  530. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  531. end;
  532. begin
  533. if op = OP_SUB then
  534. begin
  535. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  536. exit;
  537. end;
  538. ophi := TOpCG2AsmOpConstHi[op];
  539. oplo := TOpCG2AsmOpConstLo[op];
  540. gotrlwi := get_rlwi_const(a,l1,l2);
  541. if (op in [OP_AND,OP_OR,OP_XOR]) then
  542. begin
  543. if (a = 0) then
  544. begin
  545. if op = OP_AND then
  546. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  547. else
  548. a_load_reg_reg(list,size,size,src,dst);
  549. exit;
  550. end
  551. else if (a = -1) then
  552. begin
  553. case op of
  554. OP_OR:
  555. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  556. OP_XOR:
  557. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  558. OP_AND:
  559. a_load_reg_reg(list,size,size,src,dst);
  560. end;
  561. exit;
  562. end
  563. else if (aword(a) <= high(word)) and
  564. ((op <> OP_AND) or
  565. not gotrlwi) then
  566. begin
  567. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  568. exit;
  569. end;
  570. { all basic constant instructions also have a shifted form that }
  571. { works only on the highest 16bits, so if lo(a) is 0, we can }
  572. { use that one }
  573. if (word(a) = 0) and
  574. (not(op = OP_AND) or
  575. not gotrlwi) then
  576. begin
  577. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  578. exit;
  579. end;
  580. end
  581. else if (op = OP_ADD) then
  582. if a = 0 then
  583. begin
  584. a_load_reg_reg(list,size,size,src,dst);
  585. exit
  586. end
  587. else if (a >= low(smallint)) and
  588. (a <= high(smallint)) then
  589. begin
  590. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  591. exit;
  592. end;
  593. { otherwise, the instructions we can generate depend on the }
  594. { operation }
  595. useReg := false;
  596. case op of
  597. OP_DIV,OP_IDIV:
  598. if (a = 0) then
  599. internalerror(200208103)
  600. else if (a = 1) then
  601. begin
  602. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  603. exit
  604. end
  605. else if ispowerof2(a,l1) then
  606. begin
  607. case op of
  608. OP_DIV:
  609. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  610. OP_IDIV:
  611. begin
  612. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  613. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  614. end;
  615. end;
  616. exit;
  617. end
  618. else
  619. usereg := true;
  620. OP_IMUL, OP_MUL:
  621. if (a = 0) then
  622. begin
  623. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  624. exit
  625. end
  626. else if (a = 1) then
  627. begin
  628. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  629. exit
  630. end
  631. else if ispowerof2(a,l1) then
  632. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  633. else if (longint(a) >= low(smallint)) and
  634. (longint(a) <= high(smallint)) then
  635. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  636. else
  637. usereg := true;
  638. OP_ADD:
  639. begin
  640. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  641. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  642. smallint((a shr 16) + ord(smallint(a) < 0))));
  643. end;
  644. OP_OR:
  645. { try to use rlwimi }
  646. if gotrlwi and
  647. (src = dst) then
  648. begin
  649. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  650. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  651. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  652. scratchreg,0,l1,l2));
  653. rg[R_INTREGISTER].ungetregister(list,scratchreg);
  654. end
  655. else
  656. do_lo_hi;
  657. OP_AND:
  658. { try to use rlwinm }
  659. if gotrlwi then
  660. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  661. src,0,l1,l2))
  662. else
  663. useReg := true;
  664. OP_XOR:
  665. do_lo_hi;
  666. OP_SHL,OP_SHR,OP_SAR:
  667. begin
  668. if (a and 31) <> 0 Then
  669. list.concat(taicpu.op_reg_reg_const(
  670. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  671. else
  672. a_load_reg_reg(list,size,size,src,dst);
  673. if (a shr 5) <> 0 then
  674. internalError(68991);
  675. end
  676. else
  677. internalerror(200109091);
  678. end;
  679. { if all else failed, load the constant in a register and then }
  680. { perform the operation }
  681. if useReg then
  682. begin
  683. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  684. a_load_const_reg(list,OS_32,a,scratchreg);
  685. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  686. rg[R_INTREGISTER].ungetregister(list,scratchreg);
  687. end;
  688. end;
  689. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  690. size: tcgsize; src1, src2, dst: tregister);
  691. const
  692. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  693. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  694. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  695. begin
  696. case op of
  697. OP_NEG,OP_NOT:
  698. begin
  699. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  700. if (op = OP_NOT) and
  701. not(size in [OS_32,OS_S32]) then
  702. { zero/sign extend result again }
  703. a_load_reg_reg(list,OS_32,size,dst,dst);
  704. end;
  705. else
  706. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  707. end;
  708. end;
  709. {*************** compare instructructions ****************}
  710. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  711. l : tasmlabel);
  712. var
  713. p: taicpu;
  714. scratch_register: TRegister;
  715. signed: boolean;
  716. begin
  717. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  718. { in the following case, we generate more efficient code when }
  719. { signed is true }
  720. if (cmp_op in [OC_EQ,OC_NE]) and
  721. (aword(a) > $ffff) then
  722. signed := true;
  723. if signed then
  724. if (a >= low(smallint)) and (a <= high(smallint)) Then
  725. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  726. else
  727. begin
  728. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  729. a_load_const_reg(list,OS_32,a,scratch_register);
  730. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  731. rg[R_INTREGISTER].ungetregister(list,scratch_register);
  732. end
  733. else
  734. if (aword(a) <= $ffff) then
  735. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  736. else
  737. begin
  738. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  739. a_load_const_reg(list,OS_32,a,scratch_register);
  740. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  741. rg[R_INTREGISTER].ungetregister(list,scratch_register);
  742. end;
  743. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  744. end;
  745. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  746. reg1,reg2 : tregister;l : tasmlabel);
  747. var
  748. p: taicpu;
  749. op: tasmop;
  750. begin
  751. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  752. op := A_CMPW
  753. else
  754. op := A_CMPLW;
  755. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  756. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  757. end;
  758. procedure tcgppc.g_save_standard_registers(list:Taasmoutput);
  759. begin
  760. {$warning FIX ME}
  761. end;
  762. procedure tcgppc.g_restore_standard_registers(list:Taasmoutput);
  763. begin
  764. {$warning FIX ME}
  765. end;
  766. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  767. begin
  768. {$warning FIX ME}
  769. end;
  770. procedure tcgppc.g_restore_all_registers(list : taasmoutput;const funcretparaloc:tparalocation);
  771. begin
  772. {$warning FIX ME}
  773. end;
  774. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  775. begin
  776. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  777. end;
  778. procedure tcgppc.a_jmp_name(list : taasmoutput;const s : string);
  779. var
  780. p : taicpu;
  781. begin
  782. p := taicpu.op_sym(A_B,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION));
  783. p.is_jmp := true;
  784. list.concat(p)
  785. end;
  786. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  787. begin
  788. a_jmp(list,A_B,C_None,0,l);
  789. end;
  790. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  791. var
  792. c: tasmcond;
  793. begin
  794. c := flags_to_cond(f);
  795. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  796. end;
  797. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  798. var
  799. testbit: byte;
  800. bitvalue: boolean;
  801. begin
  802. { get the bit to extract from the conditional register + its }
  803. { requested value (0 or 1) }
  804. testbit := ((f.cr-RS_CR0) * 4);
  805. case f.flag of
  806. F_EQ,F_NE:
  807. begin
  808. inc(testbit,2);
  809. bitvalue := f.flag = F_EQ;
  810. end;
  811. F_LT,F_GE:
  812. begin
  813. bitvalue := f.flag = F_LT;
  814. end;
  815. F_GT,F_LE:
  816. begin
  817. inc(testbit);
  818. bitvalue := f.flag = F_GT;
  819. end;
  820. else
  821. internalerror(200112261);
  822. end;
  823. { load the conditional register in the destination reg }
  824. list.concat(taicpu.op_reg(A_MFCR,reg));
  825. { we will move the bit that has to be tested to bit 0 by rotating }
  826. { left }
  827. testbit := (testbit + 1) and 31;
  828. { extract bit }
  829. list.concat(taicpu.op_reg_reg_const_const_const(
  830. A_RLWINM,reg,reg,testbit,31,31));
  831. { if we need the inverse, xor with 1 }
  832. if not bitvalue then
  833. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  834. end;
  835. (*
  836. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  837. var
  838. testbit: byte;
  839. bitvalue: boolean;
  840. begin
  841. { get the bit to extract from the conditional register + its }
  842. { requested value (0 or 1) }
  843. case f.simple of
  844. false:
  845. begin
  846. { we don't generate this in the compiler }
  847. internalerror(200109062);
  848. end;
  849. true:
  850. case f.cond of
  851. C_None:
  852. internalerror(200109063);
  853. C_LT..C_NU:
  854. begin
  855. testbit := (ord(f.cr) - ord(R_CR0))*4;
  856. inc(testbit,AsmCondFlag2BI[f.cond]);
  857. bitvalue := AsmCondFlagTF[f.cond];
  858. end;
  859. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  860. begin
  861. testbit := f.crbit
  862. bitvalue := AsmCondFlagTF[f.cond];
  863. end;
  864. else
  865. internalerror(200109064);
  866. end;
  867. end;
  868. { load the conditional register in the destination reg }
  869. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  870. { we will move the bit that has to be tested to bit 31 -> rotate }
  871. { left by bitpos+1 (remember, this is big-endian!) }
  872. if bitpos <> 31 then
  873. inc(bitpos)
  874. else
  875. bitpos := 0;
  876. { extract bit }
  877. list.concat(taicpu.op_reg_reg_const_const_const(
  878. A_RLWINM,reg,reg,bitpos,31,31));
  879. { if we need the inverse, xor with 1 }
  880. if not bitvalue then
  881. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  882. end;
  883. *)
  884. { *********** entry/exit code and address loading ************ }
  885. procedure tcgppc.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  886. { generated the entry code of a procedure/function. Note: localsize is the }
  887. { sum of the size necessary for local variables and the maximum possible }
  888. { combined size of ALL the parameters of a procedure called by the current }
  889. { one. }
  890. { This procedure may be called before, as well as after g_return_from_proc }
  891. { is called. NOTE registers are not to be allocated through the register }
  892. { allocator here, because the register colouring has already occured !! }
  893. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  894. href,href2 : treference;
  895. usesfpr,usesgpr,gotgot : boolean;
  896. parastart : aint;
  897. l : tasmlabel;
  898. regcounter2, firstfpureg: Tsuperregister;
  899. hp: tparaitem;
  900. cond : tasmcond;
  901. instr : taicpu;
  902. size: tcgsize;
  903. begin
  904. { CR and LR only have to be saved in case they are modified by the current }
  905. { procedure, but currently this isn't checked, so save them always }
  906. { following is the entry code as described in "Altivec Programming }
  907. { Interface Manual", bar the saving of AltiVec registers }
  908. a_reg_alloc(list,NR_STACK_POINTER_REG);
  909. a_reg_alloc(list,NR_R0);
  910. if current_procinfo.procdef.parast.symtablelevel>1 then
  911. a_reg_alloc(list,NR_R11);
  912. usesfpr:=false;
  913. if not (po_assembler in current_procinfo.procdef.procoptions) then
  914. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  915. case target_info.abi of
  916. abi_powerpc_aix:
  917. firstfpureg := RS_F14;
  918. abi_powerpc_sysv:
  919. firstfpureg := RS_F9;
  920. else
  921. internalerror(2003122903);
  922. end;
  923. for regcounter:=firstfpureg to RS_F31 do
  924. begin
  925. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  926. begin
  927. usesfpr:= true;
  928. firstregfpu:=regcounter;
  929. break;
  930. end;
  931. end;
  932. usesgpr:=false;
  933. if not (po_assembler in current_procinfo.procdef.procoptions) then
  934. for regcounter2:=RS_R13 to RS_R31 do
  935. begin
  936. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  937. begin
  938. usesgpr:=true;
  939. firstreggpr:=regcounter2;
  940. break;
  941. end;
  942. end;
  943. { save link register? }
  944. if not (po_assembler in current_procinfo.procdef.procoptions) then
  945. if (pi_do_call in current_procinfo.flags) then
  946. begin
  947. { save return address... }
  948. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  949. { ... in caller's frame }
  950. case target_info.abi of
  951. abi_powerpc_aix:
  952. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  953. abi_powerpc_sysv:
  954. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  955. end;
  956. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  957. a_reg_dealloc(list,NR_R0);
  958. end;
  959. { save the CR if necessary in callers frame. }
  960. if not (po_assembler in current_procinfo.procdef.procoptions) then
  961. if target_info.abi = abi_powerpc_aix then
  962. if false then { Not needed at the moment. }
  963. begin
  964. a_reg_alloc(list,NR_R0);
  965. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  966. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  967. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  968. a_reg_dealloc(list,NR_R0);
  969. end;
  970. { !!! always allocate space for all registers for now !!! }
  971. if not (po_assembler in current_procinfo.procdef.procoptions) then
  972. { if usesfpr or usesgpr then }
  973. begin
  974. a_reg_alloc(list,NR_R12);
  975. { save end of fpr save area }
  976. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  977. end;
  978. if (localsize <> 0) then
  979. begin
  980. if (localsize <= high(smallint)) then
  981. begin
  982. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  983. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  984. end
  985. else
  986. begin
  987. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  988. { can't use getregisterint here, the register colouring }
  989. { is already done when we get here }
  990. href.index := NR_R11;
  991. a_reg_alloc(list,href.index);
  992. a_load_const_reg(list,OS_S32,-localsize,href.index);
  993. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  994. a_reg_dealloc(list,href.index);
  995. end;
  996. end;
  997. { no GOT pointer loaded yet }
  998. gotgot:=false;
  999. if usesfpr then
  1000. begin
  1001. { save floating-point registers
  1002. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  1003. begin
  1004. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  1005. gotgot:=true;
  1006. end
  1007. else
  1008. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14),AB_EXTERNAL,AT_FUNCTION));
  1009. }
  1010. reference_reset_base(href,NR_R12,-8);
  1011. for regcounter:=firstregfpu to RS_F31 do
  1012. begin
  1013. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1014. begin
  1015. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  1016. dec(href.offset,8);
  1017. end;
  1018. end;
  1019. { compute end of gpr save area }
  1020. a_op_const_reg(list,OP_ADD,OS_ADDR,href.offset+8,NR_R12);
  1021. end;
  1022. { save gprs and fetch GOT pointer }
  1023. if usesgpr then
  1024. begin
  1025. {
  1026. if cs_create_pic in aktmoduleswitches then
  1027. begin
  1028. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  1029. gotgot:=true;
  1030. end
  1031. else
  1032. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14),AB_EXTERNAL,AT_FUNCTION))
  1033. }
  1034. reference_reset_base(href,NR_R12,-4);
  1035. for regcounter2:=RS_R13 to RS_R31 do
  1036. begin
  1037. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1038. begin
  1039. usesgpr:=true;
  1040. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  1041. dec(href.offset,4);
  1042. end;
  1043. end;
  1044. {
  1045. r.enum:=R_INTREGISTER;
  1046. r.:=;
  1047. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  1048. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1049. }
  1050. end;
  1051. if assigned(current_procinfo.procdef.parast) then
  1052. begin
  1053. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1054. begin
  1055. { copy memory parameters to local parast }
  1056. hp:=tparaitem(current_procinfo.procdef.para.first);
  1057. while assigned(hp) do
  1058. begin
  1059. if (hp.paraloc[calleeside].loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1060. begin
  1061. case tvarsym(hp.parasym).localloc.loc of
  1062. LOC_REFERENCE:
  1063. begin
  1064. reference_reset_base(href,tvarsym(hp.parasym).localloc.reference.index,tvarsym(hp.parasym).localloc.reference.offset);
  1065. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  1066. { we can't use functions here which allocate registers (FK)
  1067. cg.a_load_ref_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,href);
  1068. }
  1069. case hp.paraloc[calleeside].size of
  1070. OS_F32:
  1071. size := OS_32;
  1072. OS_64,OS_S64:
  1073. size := OS_F64;
  1074. else
  1075. size := hp.paraloc[calleeside].size;
  1076. end;
  1077. case size of
  1078. OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32:
  1079. begin
  1080. cg.a_load_ref_reg(list,size,size,href2,NR_R0);
  1081. cg.a_load_reg_ref(list,size,size,NR_R0,href);
  1082. end;
  1083. OS_F64:
  1084. begin
  1085. cg.a_loadfpu_ref_reg(list,size,href2,NR_F0);
  1086. cg.a_loadfpu_reg_ref(list,size,NR_F0,href);
  1087. end;
  1088. else
  1089. internalerror(2004070910);
  1090. end;
  1091. end;
  1092. LOC_CREGISTER:
  1093. begin
  1094. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  1095. cg.a_load_ref_reg(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,tvarsym(hp.parasym).localloc.register);
  1096. end;
  1097. LOC_CFPUREGISTER:
  1098. begin
  1099. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  1100. cg.a_loadfpu_ref_reg(list,hp.paraloc[calleeside].size,href2,tvarsym(hp.parasym).localloc.register);
  1101. end;
  1102. else
  1103. internalerror(2004070911);
  1104. end;
  1105. end;
  1106. hp := tparaitem(hp.next);
  1107. end;
  1108. end;
  1109. end;
  1110. if usesfpr or usesgpr then
  1111. a_reg_dealloc(list,NR_R12);
  1112. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1113. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1114. case target_info.system of
  1115. system_powerpc_darwin:
  1116. begin
  1117. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1118. fillchar(cond,sizeof(cond),0);
  1119. cond.simple:=false;
  1120. cond.bo:=20;
  1121. cond.bi:=31;
  1122. instr:=taicpu.op_sym(A_BCL,current_procinfo.gotlabel);
  1123. instr.setcondition(cond);
  1124. list.concat(instr);
  1125. a_label(list,current_procinfo.gotlabel);
  1126. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1127. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1128. end;
  1129. else
  1130. begin
  1131. a_reg_alloc(list,NR_R31);
  1132. { place GOT ptr in r31 }
  1133. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1134. end;
  1135. end;
  1136. { save the CR if necessary ( !!! always done currently ) }
  1137. { still need to find out where this has to be done for SystemV
  1138. a_reg_alloc(list,R_0);
  1139. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1140. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1141. new_reference(STACK_POINTER_REG,LA_CR)));
  1142. a_reg_dealloc(list,R_0); }
  1143. { now comes the AltiVec context save, not yet implemented !!! }
  1144. end;
  1145. procedure tcgppc.g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean);
  1146. { This procedure may be called before, as well as after g_stackframe_entry }
  1147. { is called. NOTE registers are not to be allocated through the register }
  1148. { allocator here, because the register colouring has already occured !! }
  1149. var
  1150. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  1151. href : treference;
  1152. usesfpr,usesgpr,genret : boolean;
  1153. regcounter2, firstfpureg:Tsuperregister;
  1154. localsize: aint;
  1155. begin
  1156. { AltiVec context restore, not yet implemented !!! }
  1157. usesfpr:=false;
  1158. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1159. begin
  1160. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1161. case target_info.abi of
  1162. abi_powerpc_aix:
  1163. firstfpureg := RS_F14;
  1164. abi_powerpc_sysv:
  1165. firstfpureg := RS_F9;
  1166. else
  1167. internalerror(2003122903);
  1168. end;
  1169. for regcounter:=firstfpureg to RS_F31 do
  1170. begin
  1171. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1172. begin
  1173. usesfpr:=true;
  1174. firstregfpu:=regcounter;
  1175. break;
  1176. end;
  1177. end;
  1178. end;
  1179. usesgpr:=false;
  1180. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1181. for regcounter2:=RS_R13 to RS_R31 do
  1182. begin
  1183. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1184. begin
  1185. usesgpr:=true;
  1186. firstreggpr:=regcounter2;
  1187. break;
  1188. end;
  1189. end;
  1190. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1191. { no return (blr) generated yet }
  1192. genret:=true;
  1193. if usesgpr or usesfpr then
  1194. begin
  1195. { address of gpr save area to r11 }
  1196. { (register allocator is no longer valid at this time and an add of 0 }
  1197. { is translated into a move, which is then registered with the register }
  1198. { allocator, causing a crash }
  1199. if (localsize <> 0) then
  1200. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12)
  1201. else
  1202. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  1203. if usesfpr then
  1204. begin
  1205. reference_reset_base(href,NR_R12,-8);
  1206. for regcounter := firstregfpu to RS_F31 do
  1207. begin
  1208. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1209. begin
  1210. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1211. dec(href.offset,8);
  1212. end;
  1213. end;
  1214. inc(href.offset,4);
  1215. end
  1216. else
  1217. reference_reset_base(href,NR_R12,-4);
  1218. for regcounter2:=RS_R13 to RS_R31 do
  1219. begin
  1220. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1221. begin
  1222. usesgpr:=true;
  1223. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1224. dec(href.offset,4);
  1225. end;
  1226. end;
  1227. (*
  1228. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1229. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1230. *)
  1231. end;
  1232. (*
  1233. { restore fprs and return }
  1234. if usesfpr then
  1235. begin
  1236. { address of fpr save area to r11 }
  1237. r:=NR_R12;
  1238. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1239. {
  1240. if (pi_do_call in current_procinfo.flags) then
  1241. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1242. '_x',AB_EXTERNAL,AT_FUNCTION))
  1243. else
  1244. { leaf node => lr haven't to be restored }
  1245. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1246. '_l');
  1247. genret:=false;
  1248. }
  1249. end;
  1250. *)
  1251. { if we didn't generate the return code, we've to do it now }
  1252. if genret then
  1253. begin
  1254. { adjust r1 }
  1255. { (register allocator is no longer valid at this time and an add of 0 }
  1256. { is translated into a move, which is then registered with the register }
  1257. { allocator, causing a crash }
  1258. if (localsize <> 0) then
  1259. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1260. { load link register? }
  1261. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1262. begin
  1263. if (pi_do_call in current_procinfo.flags) then
  1264. begin
  1265. case target_info.abi of
  1266. abi_powerpc_aix:
  1267. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1268. abi_powerpc_sysv:
  1269. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1270. end;
  1271. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1272. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1273. end;
  1274. { restore the CR if necessary from callers frame}
  1275. if target_info.abi = abi_powerpc_aix then
  1276. if false then { Not needed at the moment. }
  1277. begin
  1278. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1279. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1280. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1281. a_reg_dealloc(list,NR_R0);
  1282. end;
  1283. end;
  1284. list.concat(taicpu.op_none(A_BLR));
  1285. end;
  1286. end;
  1287. function tcgppc.save_regs(list : taasmoutput):longint;
  1288. {Generates code which saves used non-volatile registers in
  1289. the save area right below the address the stackpointer point to.
  1290. Returns the actual used save area size.}
  1291. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1292. usesfpr,usesgpr: boolean;
  1293. href : treference;
  1294. offset: aint;
  1295. regcounter2, firstfpureg: Tsuperregister;
  1296. begin
  1297. usesfpr:=false;
  1298. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1299. begin
  1300. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1301. case target_info.abi of
  1302. abi_powerpc_aix:
  1303. firstfpureg := RS_F14;
  1304. abi_powerpc_sysv:
  1305. firstfpureg := RS_F9;
  1306. else
  1307. internalerror(2003122903);
  1308. end;
  1309. for regcounter:=firstfpureg to RS_F31 do
  1310. begin
  1311. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1312. begin
  1313. usesfpr:=true;
  1314. firstregfpu:=regcounter;
  1315. break;
  1316. end;
  1317. end;
  1318. end;
  1319. usesgpr:=false;
  1320. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1321. for regcounter2:=RS_R13 to RS_R31 do
  1322. begin
  1323. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1324. begin
  1325. usesgpr:=true;
  1326. firstreggpr:=regcounter2;
  1327. break;
  1328. end;
  1329. end;
  1330. offset:= 0;
  1331. { save floating-point registers }
  1332. if usesfpr then
  1333. for regcounter := firstregfpu to RS_F31 do
  1334. begin
  1335. offset:= offset - 8;
  1336. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1337. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1338. end;
  1339. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1340. { save gprs in gpr save area }
  1341. if usesgpr then
  1342. if firstreggpr < RS_R30 then
  1343. begin
  1344. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1345. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1346. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1347. {STMW stores multiple registers}
  1348. end
  1349. else
  1350. begin
  1351. for regcounter := firstreggpr to RS_R31 do
  1352. begin
  1353. offset:= offset - 4;
  1354. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1355. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1356. end;
  1357. end;
  1358. { now comes the AltiVec context save, not yet implemented !!! }
  1359. save_regs:= -offset;
  1360. end;
  1361. procedure tcgppc.restore_regs(list : taasmoutput);
  1362. {Generates code which restores used non-volatile registers from
  1363. the save area right below the address the stackpointer point to.}
  1364. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1365. usesfpr,usesgpr: boolean;
  1366. href : treference;
  1367. offset: integer;
  1368. regcounter2, firstfpureg: Tsuperregister;
  1369. begin
  1370. usesfpr:=false;
  1371. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1372. begin
  1373. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1374. case target_info.abi of
  1375. abi_powerpc_aix:
  1376. firstfpureg := RS_F14;
  1377. abi_powerpc_sysv:
  1378. firstfpureg := RS_F9;
  1379. else
  1380. internalerror(2003122903);
  1381. end;
  1382. for regcounter:=firstfpureg to RS_F31 do
  1383. begin
  1384. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1385. begin
  1386. usesfpr:=true;
  1387. firstregfpu:=regcounter;
  1388. break;
  1389. end;
  1390. end;
  1391. end;
  1392. usesgpr:=false;
  1393. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1394. for regcounter2:=RS_R13 to RS_R31 do
  1395. begin
  1396. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1397. begin
  1398. usesgpr:=true;
  1399. firstreggpr:=regcounter2;
  1400. break;
  1401. end;
  1402. end;
  1403. offset:= 0;
  1404. { restore fp registers }
  1405. if usesfpr then
  1406. for regcounter := firstregfpu to RS_F31 do
  1407. begin
  1408. offset:= offset - 8;
  1409. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1410. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1411. end;
  1412. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1413. { restore gprs }
  1414. if usesgpr then
  1415. if firstreggpr < RS_R30 then
  1416. begin
  1417. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1418. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1419. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1420. {LMW loads multiple registers}
  1421. end
  1422. else
  1423. begin
  1424. for regcounter := firstreggpr to RS_R31 do
  1425. begin
  1426. offset:= offset - 4;
  1427. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1428. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1429. end;
  1430. end;
  1431. { now comes the AltiVec context restore, not yet implemented !!! }
  1432. end;
  1433. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1434. (* NOT IN USE *)
  1435. { generated the entry code of a procedure/function. Note: localsize is the }
  1436. { sum of the size necessary for local variables and the maximum possible }
  1437. { combined size of ALL the parameters of a procedure called by the current }
  1438. { one }
  1439. const
  1440. macosLinkageAreaSize = 24;
  1441. var regcounter: TRegister;
  1442. href : treference;
  1443. registerSaveAreaSize : longint;
  1444. begin
  1445. if (localsize mod 8) <> 0 then
  1446. internalerror(58991);
  1447. { CR and LR only have to be saved in case they are modified by the current }
  1448. { procedure, but currently this isn't checked, so save them always }
  1449. { following is the entry code as described in "Altivec Programming }
  1450. { Interface Manual", bar the saving of AltiVec registers }
  1451. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1452. a_reg_alloc(list,NR_R0);
  1453. { save return address in callers frame}
  1454. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1455. { ... in caller's frame }
  1456. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1457. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1458. a_reg_dealloc(list,NR_R0);
  1459. { save non-volatile registers in callers frame}
  1460. registerSaveAreaSize:= save_regs(list);
  1461. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1462. a_reg_alloc(list,NR_R0);
  1463. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1464. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1465. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1466. a_reg_dealloc(list,NR_R0);
  1467. (*
  1468. { save pointer to incoming arguments }
  1469. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1470. *)
  1471. (*
  1472. a_reg_alloc(list,R_12);
  1473. { 0 or 8 based on SP alignment }
  1474. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1475. R_12,STACK_POINTER_REG,0,28,28));
  1476. { add in stack length }
  1477. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1478. -localsize));
  1479. { establish new alignment }
  1480. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1481. a_reg_dealloc(list,R_12);
  1482. *)
  1483. { allocate stack frame }
  1484. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1485. inc(localsize,tg.lasttemp);
  1486. localsize:=align(localsize,16);
  1487. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1488. if (localsize <> 0) then
  1489. begin
  1490. if (localsize <= high(smallint)) then
  1491. begin
  1492. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1493. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1494. end
  1495. else
  1496. begin
  1497. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1498. href.index := NR_R11;
  1499. a_reg_alloc(list,href.index);
  1500. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1501. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1502. a_reg_dealloc(list,href.index);
  1503. end;
  1504. end;
  1505. end;
  1506. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  1507. (* NOT IN USE *)
  1508. var
  1509. href : treference;
  1510. begin
  1511. a_reg_alloc(list,NR_R0);
  1512. { restore stack pointer }
  1513. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1514. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1515. (*
  1516. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1517. *)
  1518. { restore the CR if necessary from callers frame
  1519. ( !!! always done currently ) }
  1520. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1521. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1522. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1523. a_reg_dealloc(list,NR_R0);
  1524. (*
  1525. { restore return address from callers frame }
  1526. reference_reset_base(href,STACK_POINTER_REG,8);
  1527. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1528. *)
  1529. { restore non-volatile registers from callers frame }
  1530. restore_regs(list);
  1531. (*
  1532. { return to caller }
  1533. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1534. list.concat(taicpu.op_none(A_BLR));
  1535. *)
  1536. { restore return address from callers frame }
  1537. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1538. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1539. { return to caller }
  1540. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1541. list.concat(taicpu.op_none(A_BLR));
  1542. end;
  1543. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1544. var
  1545. ref2, tmpref: treference;
  1546. freereg: boolean;
  1547. tmpreg:Tregister;
  1548. begin
  1549. ref2 := ref;
  1550. freereg := fixref(list,ref2);
  1551. if assigned(ref2.symbol) then
  1552. begin
  1553. if target_info.system = system_powerpc_macos then
  1554. begin
  1555. if macos_direct_globals then
  1556. begin
  1557. reference_reset(tmpref);
  1558. tmpref.offset := ref2.offset;
  1559. tmpref.symbol := ref2.symbol;
  1560. tmpref.base := NR_NO;
  1561. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1562. end
  1563. else
  1564. begin
  1565. reference_reset(tmpref);
  1566. tmpref.symbol := ref2.symbol;
  1567. tmpref.offset := 0;
  1568. tmpref.base := NR_RTOC;
  1569. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1570. if ref2.offset <> 0 then
  1571. begin
  1572. reference_reset(tmpref);
  1573. tmpref.offset := ref2.offset;
  1574. tmpref.base:= r;
  1575. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1576. end;
  1577. end;
  1578. if ref2.base <> NR_NO then
  1579. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1580. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1581. end
  1582. else
  1583. begin
  1584. { add the symbol's value to the base of the reference, and if the }
  1585. { reference doesn't have a base, create one }
  1586. reference_reset(tmpref);
  1587. tmpref.offset := ref2.offset;
  1588. tmpref.symbol := ref2.symbol;
  1589. tmpref.relsymbol := ref2.relsymbol;
  1590. tmpref.refaddr := addr_hi;
  1591. if ref2.base<> NR_NO then
  1592. begin
  1593. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1594. ref2.base,tmpref));
  1595. if freereg then
  1596. begin
  1597. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  1598. freereg := false;
  1599. end;
  1600. end
  1601. else
  1602. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1603. tmpref.base := NR_NO;
  1604. tmpref.refaddr := addr_lo;
  1605. { can be folded with one of the next instructions by the }
  1606. { optimizer probably }
  1607. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1608. end
  1609. end
  1610. else if ref2.offset <> 0 Then
  1611. if ref2.base <> NR_NO then
  1612. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1613. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1614. { occurs, so now only ref.offset has to be loaded }
  1615. else
  1616. a_load_const_reg(list,OS_32,ref2.offset,r)
  1617. else if ref.index <> NR_NO Then
  1618. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1619. else if (ref2.base <> NR_NO) and
  1620. (r <> ref2.base) then
  1621. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1622. else
  1623. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1624. if freereg then
  1625. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  1626. end;
  1627. { ************* concatcopy ************ }
  1628. {$ifndef ppc603}
  1629. const
  1630. maxmoveunit = 8;
  1631. {$else ppc603}
  1632. const
  1633. maxmoveunit = 4;
  1634. {$endif ppc603}
  1635. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint; delsource,loadref : boolean);
  1636. var
  1637. countreg: TRegister;
  1638. src, dst: TReference;
  1639. lab: tasmlabel;
  1640. count, count2: aint;
  1641. orgsrc, orgdst: boolean;
  1642. size: tcgsize;
  1643. begin
  1644. {$ifdef extdebug}
  1645. if len > high(longint) then
  1646. internalerror(2002072704);
  1647. {$endif extdebug}
  1648. { make sure short loads are handled as optimally as possible }
  1649. if not loadref then
  1650. if (len <= maxmoveunit) and
  1651. (byte(len) in [1,2,4,8]) then
  1652. begin
  1653. if len < 8 then
  1654. begin
  1655. size := int_cgsize(len);
  1656. a_load_ref_ref(list,size,size,source,dest);
  1657. if delsource then
  1658. begin
  1659. reference_release(list,source);
  1660. tg.ungetiftemp(list,source);
  1661. end;
  1662. end
  1663. else
  1664. begin
  1665. a_reg_alloc(list,NR_F0);
  1666. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1667. if delsource then
  1668. begin
  1669. reference_release(list,source);
  1670. tg.ungetiftemp(list,source);
  1671. end;
  1672. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1673. a_reg_dealloc(list,NR_F0);
  1674. end;
  1675. exit;
  1676. end;
  1677. count := len div maxmoveunit;
  1678. reference_reset(src);
  1679. reference_reset(dst);
  1680. { load the address of source into src.base }
  1681. if loadref then
  1682. begin
  1683. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1684. a_load_ref_reg(list,OS_32,OS_32,source,src.base);
  1685. orgsrc := false;
  1686. end
  1687. else if (count > 4) or
  1688. not issimpleref(source) or
  1689. ((source.index <> NR_NO) and
  1690. ((source.offset + longint(len)) > high(smallint))) then
  1691. begin
  1692. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1693. a_loadaddr_ref_reg(list,source,src.base);
  1694. orgsrc := false;
  1695. end
  1696. else
  1697. begin
  1698. src := source;
  1699. orgsrc := true;
  1700. end;
  1701. if not orgsrc and delsource then
  1702. reference_release(list,source);
  1703. { load the address of dest into dst.base }
  1704. if (count > 4) or
  1705. not issimpleref(dest) or
  1706. ((dest.index <> NR_NO) and
  1707. ((dest.offset + longint(len)) > high(smallint))) then
  1708. begin
  1709. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1710. a_loadaddr_ref_reg(list,dest,dst.base);
  1711. orgdst := false;
  1712. end
  1713. else
  1714. begin
  1715. dst := dest;
  1716. orgdst := true;
  1717. end;
  1718. {$ifndef ppc603}
  1719. if count > 4 then
  1720. { generate a loop }
  1721. begin
  1722. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1723. { have to be set to 8. I put an Inc there so debugging may be }
  1724. { easier (should offset be different from zero here, it will be }
  1725. { easy to notice in the generated assembler }
  1726. inc(dst.offset,8);
  1727. inc(src.offset,8);
  1728. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1729. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1730. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1731. a_load_const_reg(list,OS_32,count,countreg);
  1732. { explicitely allocate R_0 since it can be used safely here }
  1733. { (for holding date that's being copied) }
  1734. a_reg_alloc(list,NR_F0);
  1735. objectlibrary.getlabel(lab);
  1736. a_label(list, lab);
  1737. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1738. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1739. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1740. a_jmp(list,A_BC,C_NE,0,lab);
  1741. rg[R_INTREGISTER].ungetregister(list,countreg);
  1742. a_reg_dealloc(list,NR_F0);
  1743. len := len mod 8;
  1744. end;
  1745. count := len div 8;
  1746. if count > 0 then
  1747. { unrolled loop }
  1748. begin
  1749. a_reg_alloc(list,NR_F0);
  1750. for count2 := 1 to count do
  1751. begin
  1752. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1753. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1754. inc(src.offset,8);
  1755. inc(dst.offset,8);
  1756. end;
  1757. a_reg_dealloc(list,NR_F0);
  1758. len := len mod 8;
  1759. end;
  1760. if (len and 4) <> 0 then
  1761. begin
  1762. a_reg_alloc(list,NR_R0);
  1763. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1764. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1765. inc(src.offset,4);
  1766. inc(dst.offset,4);
  1767. a_reg_dealloc(list,NR_R0);
  1768. end;
  1769. {$else not ppc603}
  1770. if count > 4 then
  1771. { generate a loop }
  1772. begin
  1773. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1774. { have to be set to 4. I put an Inc there so debugging may be }
  1775. { easier (should offset be different from zero here, it will be }
  1776. { easy to notice in the generated assembler }
  1777. inc(dst.offset,4);
  1778. inc(src.offset,4);
  1779. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1780. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1781. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1782. a_load_const_reg(list,OS_32,count,countreg);
  1783. { explicitely allocate R_0 since it can be used safely here }
  1784. { (for holding date that's being copied) }
  1785. a_reg_alloc(list,NR_R0);
  1786. objectlibrary.getlabel(lab);
  1787. a_label(list, lab);
  1788. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1789. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1790. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1791. a_jmp(list,A_BC,C_NE,0,lab);
  1792. rg[R_INTREGISTER].ungetregister(list,countreg);
  1793. a_reg_dealloc(list,NR_R0);
  1794. len := len mod 4;
  1795. end;
  1796. count := len div 4;
  1797. if count > 0 then
  1798. { unrolled loop }
  1799. begin
  1800. a_reg_alloc(list,NR_R0);
  1801. for count2 := 1 to count do
  1802. begin
  1803. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1804. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1805. inc(src.offset,4);
  1806. inc(dst.offset,4);
  1807. end;
  1808. a_reg_dealloc(list,NR_R0);
  1809. len := len mod 4;
  1810. end;
  1811. {$endif not ppc603}
  1812. { copy the leftovers }
  1813. if (len and 2) <> 0 then
  1814. begin
  1815. a_reg_alloc(list,NR_R0);
  1816. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1817. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1818. inc(src.offset,2);
  1819. inc(dst.offset,2);
  1820. a_reg_dealloc(list,NR_R0);
  1821. end;
  1822. if (len and 1) <> 0 then
  1823. begin
  1824. a_reg_alloc(list,NR_R0);
  1825. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1826. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1827. a_reg_dealloc(list,NR_R0);
  1828. end;
  1829. if orgsrc then
  1830. begin
  1831. if delsource then
  1832. reference_release(list,source);
  1833. end
  1834. else
  1835. rg[R_INTREGISTER].ungetregister(list,src.base);
  1836. if not orgdst then
  1837. rg[R_INTREGISTER].ungetregister(list,dst.base);
  1838. if delsource then
  1839. tg.ungetiftemp(list,source);
  1840. end;
  1841. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1842. var
  1843. hl : tasmlabel;
  1844. begin
  1845. if not(cs_check_overflow in aktlocalswitches) then
  1846. exit;
  1847. objectlibrary.getlabel(hl);
  1848. if not ((def.deftype=pointerdef) or
  1849. ((def.deftype=orddef) and
  1850. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1851. bool8bit,bool16bit,bool32bit]))) then
  1852. begin
  1853. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1854. a_jmp(list,A_BC,C_NO,7,hl)
  1855. end
  1856. else
  1857. a_jmp_cond(list,OC_AE,hl);
  1858. a_call_name(list,'FPC_OVERFLOW');
  1859. a_label(list,hl);
  1860. end;
  1861. {***************** This is private property, keep out! :) *****************}
  1862. function tcgppc.issimpleref(const ref: treference): boolean;
  1863. begin
  1864. if (ref.base = NR_NO) and
  1865. (ref.index <> NR_NO) then
  1866. internalerror(200208101);
  1867. result :=
  1868. not(assigned(ref.symbol)) and
  1869. (((ref.index = NR_NO) and
  1870. (ref.offset >= low(smallint)) and
  1871. (ref.offset <= high(smallint))) or
  1872. ((ref.index <> NR_NO) and
  1873. (ref.offset = 0)));
  1874. end;
  1875. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1876. var
  1877. tmpreg: tregister;
  1878. orgindex: tregister;
  1879. begin
  1880. result := false;
  1881. if (ref.base = NR_NO) then
  1882. begin
  1883. ref.base := ref.index;
  1884. ref.base := NR_NO;
  1885. end;
  1886. if (ref.base <> NR_NO) then
  1887. begin
  1888. if (ref.index <> NR_NO) and
  1889. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1890. begin
  1891. result := true;
  1892. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1893. list.concat(taicpu.op_reg_reg_reg(
  1894. A_ADD,tmpreg,ref.base,ref.index));
  1895. ref.index := NR_NO;
  1896. ref.base := tmpreg;
  1897. end
  1898. end
  1899. else
  1900. if ref.index <> NR_NO then
  1901. internalerror(200208102);
  1902. end;
  1903. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1904. { that's the case, we can use rlwinm to do an AND operation }
  1905. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1906. var
  1907. temp : longint;
  1908. testbit : aint;
  1909. compare: boolean;
  1910. begin
  1911. get_rlwi_const := false;
  1912. if (a = 0) or (a = -1) then
  1913. exit;
  1914. { start with the lowest bit }
  1915. testbit := 1;
  1916. { check its value }
  1917. compare := boolean(a and testbit);
  1918. { find out how long the run of bits with this value is }
  1919. { (it's impossible that all bits are 1 or 0, because in that case }
  1920. { this function wouldn't have been called) }
  1921. l1 := 31;
  1922. while (((a and testbit) <> 0) = compare) do
  1923. begin
  1924. testbit := testbit shl 1;
  1925. dec(l1);
  1926. end;
  1927. { check the length of the run of bits that comes next }
  1928. compare := not compare;
  1929. l2 := l1;
  1930. while (((a and testbit) <> 0) = compare) and
  1931. (l2 >= 0) do
  1932. begin
  1933. testbit := testbit shl 1;
  1934. dec(l2);
  1935. end;
  1936. { and finally the check whether the rest of the bits all have the }
  1937. { same value }
  1938. compare := not compare;
  1939. temp := l2;
  1940. if temp >= 0 then
  1941. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1942. exit;
  1943. { we have done "not(not(compare))", so compare is back to its }
  1944. { initial value. If the lowest bit was 0, a is of the form }
  1945. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1946. { because l2 now contains the position of the last zero of the }
  1947. { first run instead of that of the first 1) so switch l1 and l2 }
  1948. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1949. if not compare then
  1950. begin
  1951. temp := l1;
  1952. l1 := l2+1;
  1953. l2 := temp;
  1954. end
  1955. else
  1956. { otherwise, l1 currently contains the position of the last }
  1957. { zero instead of that of the first 1 of the second run -> +1 }
  1958. inc(l1);
  1959. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1960. l1 := l1 and 31;
  1961. l2 := l2 and 31;
  1962. get_rlwi_const := true;
  1963. end;
  1964. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1965. ref: treference);
  1966. var
  1967. tmpreg: tregister;
  1968. tmpref: treference;
  1969. largeOffset: Boolean;
  1970. begin
  1971. tmpreg := NR_NO;
  1972. if target_info.system = system_powerpc_macos then
  1973. begin
  1974. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1975. high(smallint)-low(smallint));
  1976. if assigned(ref.symbol) then
  1977. begin {Load symbol's value}
  1978. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1979. reference_reset(tmpref);
  1980. tmpref.symbol := ref.symbol;
  1981. tmpref.base := NR_RTOC;
  1982. if macos_direct_globals then
  1983. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1984. else
  1985. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1986. end;
  1987. if largeOffset then
  1988. begin {Add hi part of offset}
  1989. reference_reset(tmpref);
  1990. if Smallint(Lo(ref.offset)) < 0 then
  1991. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1992. else
  1993. tmpref.offset := Hi(ref.offset);
  1994. if (tmpreg <> NR_NO) then
  1995. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1996. else
  1997. begin
  1998. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1999. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  2000. end;
  2001. end;
  2002. if (tmpreg <> NR_NO) then
  2003. begin
  2004. {Add content of base register}
  2005. if ref.base <> NR_NO then
  2006. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  2007. ref.base,tmpreg));
  2008. {Make ref ready to be used by op}
  2009. ref.symbol:= nil;
  2010. ref.base:= tmpreg;
  2011. if largeOffset then
  2012. ref.offset := Smallint(Lo(ref.offset));
  2013. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2014. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  2015. end
  2016. else
  2017. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2018. end
  2019. else {if target_info.system <> system_powerpc_macos}
  2020. begin
  2021. if assigned(ref.symbol) or
  2022. (cardinal(ref.offset-low(smallint)) >
  2023. high(smallint)-low(smallint)) then
  2024. begin
  2025. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2026. reference_reset(tmpref);
  2027. tmpref.symbol := ref.symbol;
  2028. tmpref.relsymbol := ref.relsymbol;
  2029. tmpref.offset := ref.offset;
  2030. tmpref.refaddr := addr_hi;
  2031. if ref.base <> NR_NO then
  2032. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  2033. ref.base,tmpref))
  2034. else
  2035. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  2036. ref.base := tmpreg;
  2037. ref.refaddr := addr_lo;
  2038. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2039. end
  2040. else
  2041. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2042. end;
  2043. if (tmpreg <> NR_NO) then
  2044. rg[R_INTREGISTER].ungetregister(list,tmpreg);
  2045. end;
  2046. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  2047. crval: longint; l: tasmlabel);
  2048. var
  2049. p: taicpu;
  2050. begin
  2051. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name,AB_EXTERNAL,AT_FUNCTION));
  2052. if op <> A_B then
  2053. create_cond_norm(c,crval,p.condition);
  2054. p.is_jmp := true;
  2055. list.concat(p)
  2056. end;
  2057. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2058. begin
  2059. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2060. end;
  2061. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);
  2062. begin
  2063. a_op64_const_reg_reg(list,op,value,reg,reg);
  2064. end;
  2065. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2066. begin
  2067. case op of
  2068. OP_AND,OP_OR,OP_XOR:
  2069. begin
  2070. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2071. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2072. end;
  2073. OP_ADD:
  2074. begin
  2075. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2076. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2077. end;
  2078. OP_SUB:
  2079. begin
  2080. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2081. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2082. end;
  2083. else
  2084. internalerror(2002072801);
  2085. end;
  2086. end;
  2087. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);
  2088. const
  2089. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2090. (A_SUBIC,A_SUBC,A_ADDME));
  2091. var
  2092. tmpreg: tregister;
  2093. tmpreg64: tregister64;
  2094. issub: boolean;
  2095. begin
  2096. case op of
  2097. OP_AND,OP_OR,OP_XOR:
  2098. begin
  2099. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  2100. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2101. regdst.reghi);
  2102. end;
  2103. OP_ADD, OP_SUB:
  2104. begin
  2105. if (value < 0) then
  2106. begin
  2107. if op = OP_ADD then
  2108. op := OP_SUB
  2109. else
  2110. op := OP_ADD;
  2111. value := -value;
  2112. end;
  2113. if (longint(value) <> 0) then
  2114. begin
  2115. issub := op = OP_SUB;
  2116. if (value > 0) and
  2117. (value-ord(issub) <= 32767) then
  2118. begin
  2119. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2120. regdst.reglo,regsrc.reglo,longint(value)));
  2121. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2122. regdst.reghi,regsrc.reghi));
  2123. end
  2124. else if ((value shr 32) = 0) then
  2125. begin
  2126. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2127. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2128. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2129. regdst.reglo,regsrc.reglo,tmpreg));
  2130. tcgppc(cg).rg[R_INTREGISTER].ungetregister(list,tmpreg);
  2131. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2132. regdst.reghi,regsrc.reghi));
  2133. end
  2134. else
  2135. begin
  2136. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2137. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2138. a_load64_const_reg(list,value,tmpreg64);
  2139. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2140. tcgppc(cg).rg[R_INTREGISTER].ungetregister(list,tmpreg64.reglo);
  2141. tcgppc(cg).rg[R_INTREGISTER].ungetregister(list,tmpreg64.reghi);
  2142. end
  2143. end
  2144. else
  2145. begin
  2146. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2147. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2148. regdst.reghi);
  2149. end;
  2150. end;
  2151. else
  2152. internalerror(2002072802);
  2153. end;
  2154. end;
  2155. begin
  2156. cg := tcgppc.create;
  2157. cg64 :=tcg64fppc.create;
  2158. end.
  2159. {
  2160. $Log$
  2161. Revision 1.176 2004-07-17 14:48:20 jonas
  2162. * fixed op_const_reg_reg for (OP_ADD,0,reg1,reg2)
  2163. Revision 1.175 2004/07/09 21:45:24 jonas
  2164. * fixed passing of fpu paras on the stack
  2165. * fixed number of fpu parameters passed in registers
  2166. * skip corresponding integer registers when using an fpu register for a
  2167. parameter under the AIX abi
  2168. Revision 1.174 2004/07/01 18:00:00 jonas
  2169. * fixed several errors due to aword -> aint change
  2170. Revision 1.173 2004/06/20 08:55:32 florian
  2171. * logs truncated
  2172. Revision 1.172 2004/06/17 16:55:46 peter
  2173. * powerpc compiles again
  2174. Revision 1.171 2004/06/02 17:18:10 jonas
  2175. * parameters passed on the stack now also work as register variables
  2176. Revision 1.170 2004/05/31 18:08:41 jonas
  2177. * changed calling of external procedures to be the same as under gcc
  2178. (don't worry about all the generated stubs, they're optimized away
  2179. by the linker)
  2180. -> side effect: no need anymore to use special declarations for
  2181. external C functions under Darwin compared to other platforms
  2182. (it's still necessary for variables though)
  2183. Revision 1.169 2004/04/04 17:50:36 olle
  2184. * macos: fixed large offsets in references
  2185. Revision 1.168 2004/03/06 21:37:45 florian
  2186. * fixed ppc compilation
  2187. }