cgcpu.pas 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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. exit
  584. else if (a >= low(smallint)) and
  585. (a <= high(smallint)) then
  586. begin
  587. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  588. exit;
  589. end;
  590. { otherwise, the instructions we can generate depend on the }
  591. { operation }
  592. useReg := false;
  593. case op of
  594. OP_DIV,OP_IDIV:
  595. if (a = 0) then
  596. internalerror(200208103)
  597. else if (a = 1) then
  598. begin
  599. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  600. exit
  601. end
  602. else if ispowerof2(a,l1) then
  603. begin
  604. case op of
  605. OP_DIV:
  606. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  607. OP_IDIV:
  608. begin
  609. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  610. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  611. end;
  612. end;
  613. exit;
  614. end
  615. else
  616. usereg := true;
  617. OP_IMUL, OP_MUL:
  618. if (a = 0) then
  619. begin
  620. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  621. exit
  622. end
  623. else if (a = 1) then
  624. begin
  625. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  626. exit
  627. end
  628. else if ispowerof2(a,l1) then
  629. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  630. else if (longint(a) >= low(smallint)) and
  631. (longint(a) <= high(smallint)) then
  632. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  633. else
  634. usereg := true;
  635. OP_ADD:
  636. begin
  637. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  638. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  639. smallint((a shr 16) + ord(smallint(a) < 0))));
  640. end;
  641. OP_OR:
  642. { try to use rlwimi }
  643. if gotrlwi and
  644. (src = dst) then
  645. begin
  646. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  647. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  648. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  649. scratchreg,0,l1,l2));
  650. rg[R_INTREGISTER].ungetregister(list,scratchreg);
  651. end
  652. else
  653. do_lo_hi;
  654. OP_AND:
  655. { try to use rlwinm }
  656. if gotrlwi then
  657. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  658. src,0,l1,l2))
  659. else
  660. useReg := true;
  661. OP_XOR:
  662. do_lo_hi;
  663. OP_SHL,OP_SHR,OP_SAR:
  664. begin
  665. if (a and 31) <> 0 Then
  666. list.concat(taicpu.op_reg_reg_const(
  667. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  668. else
  669. a_load_reg_reg(list,size,size,src,dst);
  670. if (a shr 5) <> 0 then
  671. internalError(68991);
  672. end
  673. else
  674. internalerror(200109091);
  675. end;
  676. { if all else failed, load the constant in a register and then }
  677. { perform the operation }
  678. if useReg then
  679. begin
  680. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  681. a_load_const_reg(list,OS_32,a,scratchreg);
  682. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  683. rg[R_INTREGISTER].ungetregister(list,scratchreg);
  684. end;
  685. end;
  686. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  687. size: tcgsize; src1, src2, dst: tregister);
  688. const
  689. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  690. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  691. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  692. begin
  693. case op of
  694. OP_NEG,OP_NOT:
  695. begin
  696. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  697. if (op = OP_NOT) and
  698. not(size in [OS_32,OS_S32]) then
  699. { zero/sign extend result again }
  700. a_load_reg_reg(list,OS_32,size,dst,dst);
  701. end;
  702. else
  703. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  704. end;
  705. end;
  706. {*************** compare instructructions ****************}
  707. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  708. l : tasmlabel);
  709. var
  710. p: taicpu;
  711. scratch_register: TRegister;
  712. signed: boolean;
  713. begin
  714. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  715. { in the following case, we generate more efficient code when }
  716. { signed is true }
  717. if (cmp_op in [OC_EQ,OC_NE]) and
  718. (aword(a) > $ffff) then
  719. signed := true;
  720. if signed then
  721. if (a >= low(smallint)) and (a <= high(smallint)) Then
  722. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  723. else
  724. begin
  725. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  726. a_load_const_reg(list,OS_32,a,scratch_register);
  727. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  728. rg[R_INTREGISTER].ungetregister(list,scratch_register);
  729. end
  730. else
  731. if (aword(a) <= $ffff) then
  732. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  733. else
  734. begin
  735. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  736. a_load_const_reg(list,OS_32,a,scratch_register);
  737. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  738. rg[R_INTREGISTER].ungetregister(list,scratch_register);
  739. end;
  740. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  741. end;
  742. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  743. reg1,reg2 : tregister;l : tasmlabel);
  744. var
  745. p: taicpu;
  746. op: tasmop;
  747. begin
  748. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  749. op := A_CMPW
  750. else
  751. op := A_CMPLW;
  752. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  753. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  754. end;
  755. procedure tcgppc.g_save_standard_registers(list:Taasmoutput);
  756. begin
  757. {$warning FIX ME}
  758. end;
  759. procedure tcgppc.g_restore_standard_registers(list:Taasmoutput);
  760. begin
  761. {$warning FIX ME}
  762. end;
  763. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  764. begin
  765. {$warning FIX ME}
  766. end;
  767. procedure tcgppc.g_restore_all_registers(list : taasmoutput;const funcretparaloc:tparalocation);
  768. begin
  769. {$warning FIX ME}
  770. end;
  771. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  772. begin
  773. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  774. end;
  775. procedure tcgppc.a_jmp_name(list : taasmoutput;const s : string);
  776. var
  777. p : taicpu;
  778. begin
  779. p := taicpu.op_sym(A_B,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION));
  780. p.is_jmp := true;
  781. list.concat(p)
  782. end;
  783. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  784. begin
  785. a_jmp(list,A_B,C_None,0,l);
  786. end;
  787. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  788. var
  789. c: tasmcond;
  790. begin
  791. c := flags_to_cond(f);
  792. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  793. end;
  794. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  795. var
  796. testbit: byte;
  797. bitvalue: boolean;
  798. begin
  799. { get the bit to extract from the conditional register + its }
  800. { requested value (0 or 1) }
  801. testbit := ((f.cr-RS_CR0) * 4);
  802. case f.flag of
  803. F_EQ,F_NE:
  804. begin
  805. inc(testbit,2);
  806. bitvalue := f.flag = F_EQ;
  807. end;
  808. F_LT,F_GE:
  809. begin
  810. bitvalue := f.flag = F_LT;
  811. end;
  812. F_GT,F_LE:
  813. begin
  814. inc(testbit);
  815. bitvalue := f.flag = F_GT;
  816. end;
  817. else
  818. internalerror(200112261);
  819. end;
  820. { load the conditional register in the destination reg }
  821. list.concat(taicpu.op_reg(A_MFCR,reg));
  822. { we will move the bit that has to be tested to bit 0 by rotating }
  823. { left }
  824. testbit := (testbit + 1) and 31;
  825. { extract bit }
  826. list.concat(taicpu.op_reg_reg_const_const_const(
  827. A_RLWINM,reg,reg,testbit,31,31));
  828. { if we need the inverse, xor with 1 }
  829. if not bitvalue then
  830. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  831. end;
  832. (*
  833. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  834. var
  835. testbit: byte;
  836. bitvalue: boolean;
  837. begin
  838. { get the bit to extract from the conditional register + its }
  839. { requested value (0 or 1) }
  840. case f.simple of
  841. false:
  842. begin
  843. { we don't generate this in the compiler }
  844. internalerror(200109062);
  845. end;
  846. true:
  847. case f.cond of
  848. C_None:
  849. internalerror(200109063);
  850. C_LT..C_NU:
  851. begin
  852. testbit := (ord(f.cr) - ord(R_CR0))*4;
  853. inc(testbit,AsmCondFlag2BI[f.cond]);
  854. bitvalue := AsmCondFlagTF[f.cond];
  855. end;
  856. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  857. begin
  858. testbit := f.crbit
  859. bitvalue := AsmCondFlagTF[f.cond];
  860. end;
  861. else
  862. internalerror(200109064);
  863. end;
  864. end;
  865. { load the conditional register in the destination reg }
  866. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  867. { we will move the bit that has to be tested to bit 31 -> rotate }
  868. { left by bitpos+1 (remember, this is big-endian!) }
  869. if bitpos <> 31 then
  870. inc(bitpos)
  871. else
  872. bitpos := 0;
  873. { extract bit }
  874. list.concat(taicpu.op_reg_reg_const_const_const(
  875. A_RLWINM,reg,reg,bitpos,31,31));
  876. { if we need the inverse, xor with 1 }
  877. if not bitvalue then
  878. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  879. end;
  880. *)
  881. { *********** entry/exit code and address loading ************ }
  882. procedure tcgppc.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  883. { generated the entry code of a procedure/function. Note: localsize is the }
  884. { sum of the size necessary for local variables and the maximum possible }
  885. { combined size of ALL the parameters of a procedure called by the current }
  886. { one. }
  887. { This procedure may be called before, as well as after g_return_from_proc }
  888. { is called. NOTE registers are not to be allocated through the register }
  889. { allocator here, because the register colouring has already occured !! }
  890. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  891. href,href2 : treference;
  892. usesfpr,usesgpr,gotgot : boolean;
  893. parastart : aint;
  894. // r,r2,rsp:Tregister;
  895. l : tasmlabel;
  896. regcounter2, firstfpureg: Tsuperregister;
  897. hp: tparaitem;
  898. cond : tasmcond;
  899. instr : taicpu;
  900. begin
  901. { CR and LR only have to be saved in case they are modified by the current }
  902. { procedure, but currently this isn't checked, so save them always }
  903. { following is the entry code as described in "Altivec Programming }
  904. { Interface Manual", bar the saving of AltiVec registers }
  905. a_reg_alloc(list,NR_STACK_POINTER_REG);
  906. a_reg_alloc(list,NR_R0);
  907. if current_procinfo.procdef.parast.symtablelevel>1 then
  908. a_reg_alloc(list,NR_R11);
  909. usesfpr:=false;
  910. if not (po_assembler in current_procinfo.procdef.procoptions) then
  911. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  912. case target_info.abi of
  913. abi_powerpc_aix:
  914. firstfpureg := RS_F14;
  915. abi_powerpc_sysv:
  916. firstfpureg := RS_F9;
  917. else
  918. internalerror(2003122903);
  919. end;
  920. for regcounter:=firstfpureg to RS_F31 do
  921. begin
  922. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  923. begin
  924. usesfpr:= true;
  925. firstregfpu:=regcounter;
  926. break;
  927. end;
  928. end;
  929. usesgpr:=false;
  930. if not (po_assembler in current_procinfo.procdef.procoptions) then
  931. for regcounter2:=RS_R13 to RS_R31 do
  932. begin
  933. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  934. begin
  935. usesgpr:=true;
  936. firstreggpr:=regcounter2;
  937. break;
  938. end;
  939. end;
  940. { save link register? }
  941. if not (po_assembler in current_procinfo.procdef.procoptions) then
  942. if (pi_do_call in current_procinfo.flags) then
  943. begin
  944. { save return address... }
  945. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  946. { ... in caller's frame }
  947. case target_info.abi of
  948. abi_powerpc_aix:
  949. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  950. abi_powerpc_sysv:
  951. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  952. end;
  953. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  954. a_reg_dealloc(list,NR_R0);
  955. end;
  956. { save the CR if necessary in callers frame. }
  957. if not (po_assembler in current_procinfo.procdef.procoptions) then
  958. if target_info.abi = abi_powerpc_aix then
  959. if false then { Not needed at the moment. }
  960. begin
  961. a_reg_alloc(list,NR_R0);
  962. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  963. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  964. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  965. a_reg_dealloc(list,NR_R0);
  966. end;
  967. { !!! always allocate space for all registers for now !!! }
  968. if not (po_assembler in current_procinfo.procdef.procoptions) then
  969. { if usesfpr or usesgpr then }
  970. begin
  971. a_reg_alloc(list,NR_R12);
  972. { save end of fpr save area }
  973. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  974. end;
  975. if (localsize <> 0) then
  976. begin
  977. if (localsize <= high(smallint)) then
  978. begin
  979. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  980. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  981. end
  982. else
  983. begin
  984. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  985. { can't use getregisterint here, the register colouring }
  986. { is already done when we get here }
  987. href.index := NR_R11;
  988. a_reg_alloc(list,href.index);
  989. a_load_const_reg(list,OS_S32,-localsize,href.index);
  990. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  991. a_reg_dealloc(list,href.index);
  992. end;
  993. end;
  994. { no GOT pointer loaded yet }
  995. gotgot:=false;
  996. if usesfpr then
  997. begin
  998. { save floating-point registers
  999. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  1000. begin
  1001. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  1002. gotgot:=true;
  1003. end
  1004. else
  1005. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14),AB_EXTERNAL,AT_FUNCTION));
  1006. }
  1007. reference_reset_base(href,NR_R12,-8);
  1008. for regcounter:=firstregfpu to RS_F31 do
  1009. begin
  1010. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1011. begin
  1012. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  1013. dec(href.offset,8);
  1014. end;
  1015. end;
  1016. { compute end of gpr save area }
  1017. a_op_const_reg(list,OP_ADD,OS_ADDR,href.offset+8,NR_R12);
  1018. end;
  1019. { save gprs and fetch GOT pointer }
  1020. if usesgpr then
  1021. begin
  1022. {
  1023. if cs_create_pic in aktmoduleswitches then
  1024. begin
  1025. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  1026. gotgot:=true;
  1027. end
  1028. else
  1029. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14),AB_EXTERNAL,AT_FUNCTION))
  1030. }
  1031. reference_reset_base(href,NR_R12,-4);
  1032. for regcounter2:=RS_R13 to RS_R31 do
  1033. begin
  1034. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1035. begin
  1036. usesgpr:=true;
  1037. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  1038. dec(href.offset,4);
  1039. end;
  1040. end;
  1041. {
  1042. r.enum:=R_INTREGISTER;
  1043. r.:=;
  1044. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  1045. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1046. }
  1047. end;
  1048. if assigned(current_procinfo.procdef.parast) then
  1049. begin
  1050. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1051. begin
  1052. { copy memory parameters to local parast }
  1053. hp:=tparaitem(current_procinfo.procdef.para.first);
  1054. while assigned(hp) do
  1055. begin
  1056. if (hp.paraloc[calleeside].loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1057. begin
  1058. case tvarsym(hp.parasym).localloc.loc of
  1059. LOC_REFERENCE:
  1060. begin
  1061. reference_reset_base(href,tvarsym(hp.parasym).localloc.reference.index,tvarsym(hp.parasym).localloc.reference.offset);
  1062. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  1063. { we can't use functions here which allocate registers (FK)
  1064. cg.a_load_ref_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,href);
  1065. }
  1066. cg.a_load_ref_reg(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,NR_R0);
  1067. cg.a_load_reg_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,NR_R0,href);
  1068. end;
  1069. LOC_CREGISTER:
  1070. begin
  1071. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  1072. cg.a_load_ref_reg(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,tvarsym(hp.parasym).localloc.register);
  1073. end;
  1074. end;
  1075. end
  1076. {$ifdef dummy}
  1077. else if (hp.calleeparaloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  1078. begin
  1079. rg.getexplicitregisterint(list,hp.calleeparaloc.register);
  1080. end
  1081. {$endif dummy}
  1082. ;
  1083. hp := tparaitem(hp.next);
  1084. end;
  1085. end;
  1086. end;
  1087. if usesfpr or usesgpr then
  1088. a_reg_dealloc(list,NR_R12);
  1089. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1090. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1091. case target_info.system of
  1092. system_powerpc_darwin:
  1093. begin
  1094. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1095. fillchar(cond,sizeof(cond),0);
  1096. cond.simple:=false;
  1097. cond.bo:=20;
  1098. cond.bi:=31;
  1099. instr:=taicpu.op_sym(A_BCL,current_procinfo.gotlabel);
  1100. instr.setcondition(cond);
  1101. list.concat(instr);
  1102. a_label(list,current_procinfo.gotlabel);
  1103. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1104. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1105. end;
  1106. else
  1107. begin
  1108. a_reg_alloc(list,NR_R31);
  1109. { place GOT ptr in r31 }
  1110. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1111. end;
  1112. end;
  1113. { save the CR if necessary ( !!! always done currently ) }
  1114. { still need to find out where this has to be done for SystemV
  1115. a_reg_alloc(list,R_0);
  1116. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1117. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1118. new_reference(STACK_POINTER_REG,LA_CR)));
  1119. a_reg_dealloc(list,R_0); }
  1120. { now comes the AltiVec context save, not yet implemented !!! }
  1121. end;
  1122. procedure tcgppc.g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean);
  1123. { This procedure may be called before, as well as after g_stackframe_entry }
  1124. { is called. NOTE registers are not to be allocated through the register }
  1125. { allocator here, because the register colouring has already occured !! }
  1126. var
  1127. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  1128. href : treference;
  1129. usesfpr,usesgpr,genret : boolean;
  1130. regcounter2, firstfpureg:Tsuperregister;
  1131. localsize: aint;
  1132. begin
  1133. { AltiVec context restore, not yet implemented !!! }
  1134. usesfpr:=false;
  1135. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1136. begin
  1137. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1138. case target_info.abi of
  1139. abi_powerpc_aix:
  1140. firstfpureg := RS_F14;
  1141. abi_powerpc_sysv:
  1142. firstfpureg := RS_F9;
  1143. else
  1144. internalerror(2003122903);
  1145. end;
  1146. for regcounter:=firstfpureg to RS_F31 do
  1147. begin
  1148. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1149. begin
  1150. usesfpr:=true;
  1151. firstregfpu:=regcounter;
  1152. break;
  1153. end;
  1154. end;
  1155. end;
  1156. usesgpr:=false;
  1157. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1158. for regcounter2:=RS_R13 to RS_R31 do
  1159. begin
  1160. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1161. begin
  1162. usesgpr:=true;
  1163. firstreggpr:=regcounter2;
  1164. break;
  1165. end;
  1166. end;
  1167. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1168. { no return (blr) generated yet }
  1169. genret:=true;
  1170. if usesgpr or usesfpr then
  1171. begin
  1172. { address of gpr save area to r11 }
  1173. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12);
  1174. if usesfpr then
  1175. begin
  1176. reference_reset_base(href,NR_R12,-8);
  1177. for regcounter := firstregfpu to RS_F31 do
  1178. begin
  1179. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1180. begin
  1181. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1182. dec(href.offset,8);
  1183. end;
  1184. end;
  1185. inc(href.offset,4);
  1186. end
  1187. else
  1188. reference_reset_base(href,NR_R12,-4);
  1189. for regcounter2:=RS_R13 to RS_R31 do
  1190. begin
  1191. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1192. begin
  1193. usesgpr:=true;
  1194. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1195. dec(href.offset,4);
  1196. end;
  1197. end;
  1198. (*
  1199. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1200. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1201. *)
  1202. end;
  1203. (*
  1204. { restore fprs and return }
  1205. if usesfpr then
  1206. begin
  1207. { address of fpr save area to r11 }
  1208. r:=NR_R12;
  1209. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1210. {
  1211. if (pi_do_call in current_procinfo.flags) then
  1212. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1213. '_x',AB_EXTERNAL,AT_FUNCTION))
  1214. else
  1215. { leaf node => lr haven't to be restored }
  1216. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1217. '_l');
  1218. genret:=false;
  1219. }
  1220. end;
  1221. *)
  1222. { if we didn't generate the return code, we've to do it now }
  1223. if genret then
  1224. begin
  1225. { adjust r1 }
  1226. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1227. { load link register? }
  1228. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1229. begin
  1230. if (pi_do_call in current_procinfo.flags) then
  1231. begin
  1232. case target_info.abi of
  1233. abi_powerpc_aix:
  1234. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1235. abi_powerpc_sysv:
  1236. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1237. end;
  1238. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1239. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1240. end;
  1241. { restore the CR if necessary from callers frame}
  1242. if target_info.abi = abi_powerpc_aix then
  1243. if false then { Not needed at the moment. }
  1244. begin
  1245. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1246. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1247. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1248. a_reg_dealloc(list,NR_R0);
  1249. end;
  1250. end;
  1251. list.concat(taicpu.op_none(A_BLR));
  1252. end;
  1253. end;
  1254. function tcgppc.save_regs(list : taasmoutput):longint;
  1255. {Generates code which saves used non-volatile registers in
  1256. the save area right below the address the stackpointer point to.
  1257. Returns the actual used save area size.}
  1258. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1259. usesfpr,usesgpr: boolean;
  1260. href : treference;
  1261. offset: aint;
  1262. regcounter2, firstfpureg: Tsuperregister;
  1263. begin
  1264. usesfpr:=false;
  1265. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1266. begin
  1267. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1268. case target_info.abi of
  1269. abi_powerpc_aix:
  1270. firstfpureg := RS_F14;
  1271. abi_powerpc_sysv:
  1272. firstfpureg := RS_F9;
  1273. else
  1274. internalerror(2003122903);
  1275. end;
  1276. for regcounter:=firstfpureg to RS_F31 do
  1277. begin
  1278. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1279. begin
  1280. usesfpr:=true;
  1281. firstregfpu:=regcounter;
  1282. break;
  1283. end;
  1284. end;
  1285. end;
  1286. usesgpr:=false;
  1287. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1288. for regcounter2:=RS_R13 to RS_R31 do
  1289. begin
  1290. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1291. begin
  1292. usesgpr:=true;
  1293. firstreggpr:=regcounter2;
  1294. break;
  1295. end;
  1296. end;
  1297. offset:= 0;
  1298. { save floating-point registers }
  1299. if usesfpr then
  1300. for regcounter := firstregfpu to RS_F31 do
  1301. begin
  1302. offset:= offset - 8;
  1303. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1304. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1305. end;
  1306. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1307. { save gprs in gpr save area }
  1308. if usesgpr then
  1309. if firstreggpr < RS_R30 then
  1310. begin
  1311. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1312. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1313. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1314. {STMW stores multiple registers}
  1315. end
  1316. else
  1317. begin
  1318. for regcounter := firstreggpr to RS_R31 do
  1319. begin
  1320. offset:= offset - 4;
  1321. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1322. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1323. end;
  1324. end;
  1325. { now comes the AltiVec context save, not yet implemented !!! }
  1326. save_regs:= -offset;
  1327. end;
  1328. procedure tcgppc.restore_regs(list : taasmoutput);
  1329. {Generates code which restores used non-volatile registers from
  1330. the save area right below the address the stackpointer point to.}
  1331. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1332. usesfpr,usesgpr: boolean;
  1333. href : treference;
  1334. offset: integer;
  1335. regcounter2, firstfpureg: Tsuperregister;
  1336. begin
  1337. usesfpr:=false;
  1338. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1339. begin
  1340. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1341. case target_info.abi of
  1342. abi_powerpc_aix:
  1343. firstfpureg := RS_F14;
  1344. abi_powerpc_sysv:
  1345. firstfpureg := RS_F9;
  1346. else
  1347. internalerror(2003122903);
  1348. end;
  1349. for regcounter:=firstfpureg to RS_F31 do
  1350. begin
  1351. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1352. begin
  1353. usesfpr:=true;
  1354. firstregfpu:=regcounter;
  1355. break;
  1356. end;
  1357. end;
  1358. end;
  1359. usesgpr:=false;
  1360. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1361. for regcounter2:=RS_R13 to RS_R31 do
  1362. begin
  1363. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1364. begin
  1365. usesgpr:=true;
  1366. firstreggpr:=regcounter2;
  1367. break;
  1368. end;
  1369. end;
  1370. offset:= 0;
  1371. { restore fp registers }
  1372. if usesfpr then
  1373. for regcounter := firstregfpu to RS_F31 do
  1374. begin
  1375. offset:= offset - 8;
  1376. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1377. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1378. end;
  1379. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1380. { restore gprs }
  1381. if usesgpr then
  1382. if firstreggpr < RS_R30 then
  1383. begin
  1384. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1385. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1386. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1387. {LMW loads multiple registers}
  1388. end
  1389. else
  1390. begin
  1391. for regcounter := firstreggpr to RS_R31 do
  1392. begin
  1393. offset:= offset - 4;
  1394. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1395. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1396. end;
  1397. end;
  1398. { now comes the AltiVec context restore, not yet implemented !!! }
  1399. end;
  1400. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1401. (* NOT IN USE *)
  1402. { generated the entry code of a procedure/function. Note: localsize is the }
  1403. { sum of the size necessary for local variables and the maximum possible }
  1404. { combined size of ALL the parameters of a procedure called by the current }
  1405. { one }
  1406. const
  1407. macosLinkageAreaSize = 24;
  1408. var regcounter: TRegister;
  1409. href : treference;
  1410. registerSaveAreaSize : longint;
  1411. begin
  1412. if (localsize mod 8) <> 0 then
  1413. internalerror(58991);
  1414. { CR and LR only have to be saved in case they are modified by the current }
  1415. { procedure, but currently this isn't checked, so save them always }
  1416. { following is the entry code as described in "Altivec Programming }
  1417. { Interface Manual", bar the saving of AltiVec registers }
  1418. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1419. a_reg_alloc(list,NR_R0);
  1420. { save return address in callers frame}
  1421. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1422. { ... in caller's frame }
  1423. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1424. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1425. a_reg_dealloc(list,NR_R0);
  1426. { save non-volatile registers in callers frame}
  1427. registerSaveAreaSize:= save_regs(list);
  1428. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1429. a_reg_alloc(list,NR_R0);
  1430. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1431. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1432. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1433. a_reg_dealloc(list,NR_R0);
  1434. (*
  1435. { save pointer to incoming arguments }
  1436. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1437. *)
  1438. (*
  1439. a_reg_alloc(list,R_12);
  1440. { 0 or 8 based on SP alignment }
  1441. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1442. R_12,STACK_POINTER_REG,0,28,28));
  1443. { add in stack length }
  1444. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1445. -localsize));
  1446. { establish new alignment }
  1447. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1448. a_reg_dealloc(list,R_12);
  1449. *)
  1450. { allocate stack frame }
  1451. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1452. inc(localsize,tg.lasttemp);
  1453. localsize:=align(localsize,16);
  1454. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1455. if (localsize <> 0) then
  1456. begin
  1457. if (localsize <= high(smallint)) then
  1458. begin
  1459. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1460. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1461. end
  1462. else
  1463. begin
  1464. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1465. href.index := NR_R11;
  1466. a_reg_alloc(list,href.index);
  1467. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1468. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1469. a_reg_dealloc(list,href.index);
  1470. end;
  1471. end;
  1472. end;
  1473. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  1474. (* NOT IN USE *)
  1475. var
  1476. href : treference;
  1477. begin
  1478. a_reg_alloc(list,NR_R0);
  1479. { restore stack pointer }
  1480. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1481. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1482. (*
  1483. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1484. *)
  1485. { restore the CR if necessary from callers frame
  1486. ( !!! always done currently ) }
  1487. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1488. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1489. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1490. a_reg_dealloc(list,NR_R0);
  1491. (*
  1492. { restore return address from callers frame }
  1493. reference_reset_base(href,STACK_POINTER_REG,8);
  1494. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1495. *)
  1496. { restore non-volatile registers from callers frame }
  1497. restore_regs(list);
  1498. (*
  1499. { return to caller }
  1500. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1501. list.concat(taicpu.op_none(A_BLR));
  1502. *)
  1503. { restore return address from callers frame }
  1504. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1505. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1506. { return to caller }
  1507. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1508. list.concat(taicpu.op_none(A_BLR));
  1509. end;
  1510. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1511. var
  1512. ref2, tmpref: treference;
  1513. freereg: boolean;
  1514. tmpreg:Tregister;
  1515. begin
  1516. ref2 := ref;
  1517. freereg := fixref(list,ref2);
  1518. if assigned(ref2.symbol) then
  1519. begin
  1520. if target_info.system = system_powerpc_macos then
  1521. begin
  1522. if macos_direct_globals then
  1523. begin
  1524. reference_reset(tmpref);
  1525. tmpref.offset := ref2.offset;
  1526. tmpref.symbol := ref2.symbol;
  1527. tmpref.base := NR_NO;
  1528. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1529. end
  1530. else
  1531. begin
  1532. reference_reset(tmpref);
  1533. tmpref.symbol := ref2.symbol;
  1534. tmpref.offset := 0;
  1535. tmpref.base := NR_RTOC;
  1536. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1537. if ref2.offset <> 0 then
  1538. begin
  1539. reference_reset(tmpref);
  1540. tmpref.offset := ref2.offset;
  1541. tmpref.base:= r;
  1542. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1543. end;
  1544. end;
  1545. if ref2.base <> NR_NO then
  1546. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1547. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1548. end
  1549. else
  1550. begin
  1551. { add the symbol's value to the base of the reference, and if the }
  1552. { reference doesn't have a base, create one }
  1553. reference_reset(tmpref);
  1554. tmpref.offset := ref2.offset;
  1555. tmpref.symbol := ref2.symbol;
  1556. tmpref.relsymbol := ref2.relsymbol;
  1557. tmpref.refaddr := addr_hi;
  1558. if ref2.base<> NR_NO then
  1559. begin
  1560. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1561. ref2.base,tmpref));
  1562. if freereg then
  1563. begin
  1564. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  1565. freereg := false;
  1566. end;
  1567. end
  1568. else
  1569. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1570. tmpref.base := NR_NO;
  1571. tmpref.refaddr := addr_lo;
  1572. { can be folded with one of the next instructions by the }
  1573. { optimizer probably }
  1574. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1575. end
  1576. end
  1577. else if ref2.offset <> 0 Then
  1578. if ref2.base <> NR_NO then
  1579. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1580. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1581. { occurs, so now only ref.offset has to be loaded }
  1582. else
  1583. a_load_const_reg(list,OS_32,ref2.offset,r)
  1584. else if ref.index <> NR_NO Then
  1585. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1586. else if (ref2.base <> NR_NO) and
  1587. (r <> ref2.base) then
  1588. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1589. else
  1590. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1591. if freereg then
  1592. rg[R_INTREGISTER].ungetregister(list,ref2.base);
  1593. end;
  1594. { ************* concatcopy ************ }
  1595. {$ifndef ppc603}
  1596. const
  1597. maxmoveunit = 8;
  1598. {$else ppc603}
  1599. const
  1600. maxmoveunit = 4;
  1601. {$endif ppc603}
  1602. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint; delsource,loadref : boolean);
  1603. var
  1604. countreg: TRegister;
  1605. src, dst: TReference;
  1606. lab: tasmlabel;
  1607. count, count2: aint;
  1608. orgsrc, orgdst: boolean;
  1609. size: tcgsize;
  1610. begin
  1611. {$ifdef extdebug}
  1612. if len > high(longint) then
  1613. internalerror(2002072704);
  1614. {$endif extdebug}
  1615. { make sure short loads are handled as optimally as possible }
  1616. if not loadref then
  1617. if (len <= maxmoveunit) and
  1618. (byte(len) in [1,2,4,8]) then
  1619. begin
  1620. if len < 8 then
  1621. begin
  1622. size := int_cgsize(len);
  1623. a_load_ref_ref(list,size,size,source,dest);
  1624. if delsource then
  1625. begin
  1626. reference_release(list,source);
  1627. tg.ungetiftemp(list,source);
  1628. end;
  1629. end
  1630. else
  1631. begin
  1632. a_reg_alloc(list,NR_F0);
  1633. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1634. if delsource then
  1635. begin
  1636. reference_release(list,source);
  1637. tg.ungetiftemp(list,source);
  1638. end;
  1639. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1640. a_reg_dealloc(list,NR_F0);
  1641. end;
  1642. exit;
  1643. end;
  1644. count := len div maxmoveunit;
  1645. reference_reset(src);
  1646. reference_reset(dst);
  1647. { load the address of source into src.base }
  1648. if loadref then
  1649. begin
  1650. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1651. a_load_ref_reg(list,OS_32,OS_32,source,src.base);
  1652. orgsrc := false;
  1653. end
  1654. else if (count > 4) or
  1655. not issimpleref(source) or
  1656. ((source.index <> NR_NO) and
  1657. ((source.offset + longint(len)) > high(smallint))) then
  1658. begin
  1659. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1660. a_loadaddr_ref_reg(list,source,src.base);
  1661. orgsrc := false;
  1662. end
  1663. else
  1664. begin
  1665. src := source;
  1666. orgsrc := true;
  1667. end;
  1668. if not orgsrc and delsource then
  1669. reference_release(list,source);
  1670. { load the address of dest into dst.base }
  1671. if (count > 4) or
  1672. not issimpleref(dest) or
  1673. ((dest.index <> NR_NO) and
  1674. ((dest.offset + longint(len)) > high(smallint))) then
  1675. begin
  1676. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1677. a_loadaddr_ref_reg(list,dest,dst.base);
  1678. orgdst := false;
  1679. end
  1680. else
  1681. begin
  1682. dst := dest;
  1683. orgdst := true;
  1684. end;
  1685. {$ifndef ppc603}
  1686. if count > 4 then
  1687. { generate a loop }
  1688. begin
  1689. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1690. { have to be set to 8. I put an Inc there so debugging may be }
  1691. { easier (should offset be different from zero here, it will be }
  1692. { easy to notice in the generated assembler }
  1693. inc(dst.offset,8);
  1694. inc(src.offset,8);
  1695. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1696. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1697. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1698. a_load_const_reg(list,OS_32,count,countreg);
  1699. { explicitely allocate R_0 since it can be used safely here }
  1700. { (for holding date that's being copied) }
  1701. a_reg_alloc(list,NR_F0);
  1702. objectlibrary.getlabel(lab);
  1703. a_label(list, lab);
  1704. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1705. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1706. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1707. a_jmp(list,A_BC,C_NE,0,lab);
  1708. rg[R_INTREGISTER].ungetregister(list,countreg);
  1709. a_reg_dealloc(list,NR_F0);
  1710. len := len mod 8;
  1711. end;
  1712. count := len div 8;
  1713. if count > 0 then
  1714. { unrolled loop }
  1715. begin
  1716. a_reg_alloc(list,NR_F0);
  1717. for count2 := 1 to count do
  1718. begin
  1719. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1720. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1721. inc(src.offset,8);
  1722. inc(dst.offset,8);
  1723. end;
  1724. a_reg_dealloc(list,NR_F0);
  1725. len := len mod 8;
  1726. end;
  1727. if (len and 4) <> 0 then
  1728. begin
  1729. a_reg_alloc(list,NR_R0);
  1730. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1731. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1732. inc(src.offset,4);
  1733. inc(dst.offset,4);
  1734. a_reg_dealloc(list,NR_R0);
  1735. end;
  1736. {$else not ppc603}
  1737. if count > 4 then
  1738. { generate a loop }
  1739. begin
  1740. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1741. { have to be set to 4. I put an Inc there so debugging may be }
  1742. { easier (should offset be different from zero here, it will be }
  1743. { easy to notice in the generated assembler }
  1744. inc(dst.offset,4);
  1745. inc(src.offset,4);
  1746. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1747. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1748. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1749. a_load_const_reg(list,OS_32,count,countreg);
  1750. { explicitely allocate R_0 since it can be used safely here }
  1751. { (for holding date that's being copied) }
  1752. a_reg_alloc(list,NR_R0);
  1753. objectlibrary.getlabel(lab);
  1754. a_label(list, lab);
  1755. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1756. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1757. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1758. a_jmp(list,A_BC,C_NE,0,lab);
  1759. rg[R_INTREGISTER].ungetregister(list,countreg);
  1760. a_reg_dealloc(list,NR_R0);
  1761. len := len mod 4;
  1762. end;
  1763. count := len div 4;
  1764. if count > 0 then
  1765. { unrolled loop }
  1766. begin
  1767. a_reg_alloc(list,NR_R0);
  1768. for count2 := 1 to count do
  1769. begin
  1770. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1771. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1772. inc(src.offset,4);
  1773. inc(dst.offset,4);
  1774. end;
  1775. a_reg_dealloc(list,NR_R0);
  1776. len := len mod 4;
  1777. end;
  1778. {$endif not ppc603}
  1779. { copy the leftovers }
  1780. if (len and 2) <> 0 then
  1781. begin
  1782. a_reg_alloc(list,NR_R0);
  1783. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1784. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1785. inc(src.offset,2);
  1786. inc(dst.offset,2);
  1787. a_reg_dealloc(list,NR_R0);
  1788. end;
  1789. if (len and 1) <> 0 then
  1790. begin
  1791. a_reg_alloc(list,NR_R0);
  1792. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1793. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1794. a_reg_dealloc(list,NR_R0);
  1795. end;
  1796. if orgsrc then
  1797. begin
  1798. if delsource then
  1799. reference_release(list,source);
  1800. end
  1801. else
  1802. rg[R_INTREGISTER].ungetregister(list,src.base);
  1803. if not orgdst then
  1804. rg[R_INTREGISTER].ungetregister(list,dst.base);
  1805. if delsource then
  1806. tg.ungetiftemp(list,source);
  1807. end;
  1808. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1809. var
  1810. hl : tasmlabel;
  1811. begin
  1812. if not(cs_check_overflow in aktlocalswitches) then
  1813. exit;
  1814. objectlibrary.getlabel(hl);
  1815. if not ((def.deftype=pointerdef) or
  1816. ((def.deftype=orddef) and
  1817. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1818. bool8bit,bool16bit,bool32bit]))) then
  1819. begin
  1820. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1821. a_jmp(list,A_BC,C_NO,7,hl)
  1822. end
  1823. else
  1824. a_jmp_cond(list,OC_AE,hl);
  1825. a_call_name(list,'FPC_OVERFLOW');
  1826. a_label(list,hl);
  1827. end;
  1828. {***************** This is private property, keep out! :) *****************}
  1829. function tcgppc.issimpleref(const ref: treference): boolean;
  1830. begin
  1831. if (ref.base = NR_NO) and
  1832. (ref.index <> NR_NO) then
  1833. internalerror(200208101);
  1834. result :=
  1835. not(assigned(ref.symbol)) and
  1836. (((ref.index = NR_NO) and
  1837. (ref.offset >= low(smallint)) and
  1838. (ref.offset <= high(smallint))) or
  1839. ((ref.index <> NR_NO) and
  1840. (ref.offset = 0)));
  1841. end;
  1842. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1843. var
  1844. tmpreg: tregister;
  1845. orgindex: tregister;
  1846. begin
  1847. result := false;
  1848. if (ref.base = NR_NO) then
  1849. begin
  1850. ref.base := ref.index;
  1851. ref.base := NR_NO;
  1852. end;
  1853. if (ref.base <> NR_NO) then
  1854. begin
  1855. if (ref.index <> NR_NO) and
  1856. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1857. begin
  1858. result := true;
  1859. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1860. list.concat(taicpu.op_reg_reg_reg(
  1861. A_ADD,tmpreg,ref.base,ref.index));
  1862. ref.index := NR_NO;
  1863. ref.base := tmpreg;
  1864. end
  1865. end
  1866. else
  1867. if ref.index <> NR_NO then
  1868. internalerror(200208102);
  1869. end;
  1870. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1871. { that's the case, we can use rlwinm to do an AND operation }
  1872. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1873. var
  1874. temp : longint;
  1875. testbit : aint;
  1876. compare: boolean;
  1877. begin
  1878. get_rlwi_const := false;
  1879. if (a = 0) or (a = -1) then
  1880. exit;
  1881. { start with the lowest bit }
  1882. testbit := 1;
  1883. { check its value }
  1884. compare := boolean(a and testbit);
  1885. { find out how long the run of bits with this value is }
  1886. { (it's impossible that all bits are 1 or 0, because in that case }
  1887. { this function wouldn't have been called) }
  1888. l1 := 31;
  1889. while (((a and testbit) <> 0) = compare) do
  1890. begin
  1891. testbit := testbit shl 1;
  1892. dec(l1);
  1893. end;
  1894. { check the length of the run of bits that comes next }
  1895. compare := not compare;
  1896. l2 := l1;
  1897. while (((a and testbit) <> 0) = compare) and
  1898. (l2 >= 0) do
  1899. begin
  1900. testbit := testbit shl 1;
  1901. dec(l2);
  1902. end;
  1903. { and finally the check whether the rest of the bits all have the }
  1904. { same value }
  1905. compare := not compare;
  1906. temp := l2;
  1907. if temp >= 0 then
  1908. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1909. exit;
  1910. { we have done "not(not(compare))", so compare is back to its }
  1911. { initial value. If the lowest bit was 0, a is of the form }
  1912. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1913. { because l2 now contains the position of the last zero of the }
  1914. { first run instead of that of the first 1) so switch l1 and l2 }
  1915. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1916. if not compare then
  1917. begin
  1918. temp := l1;
  1919. l1 := l2+1;
  1920. l2 := temp;
  1921. end
  1922. else
  1923. { otherwise, l1 currently contains the position of the last }
  1924. { zero instead of that of the first 1 of the second run -> +1 }
  1925. inc(l1);
  1926. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1927. l1 := l1 and 31;
  1928. l2 := l2 and 31;
  1929. get_rlwi_const := true;
  1930. end;
  1931. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1932. ref: treference);
  1933. var
  1934. tmpreg: tregister;
  1935. tmpref: treference;
  1936. largeOffset: Boolean;
  1937. begin
  1938. tmpreg := NR_NO;
  1939. if target_info.system = system_powerpc_macos then
  1940. begin
  1941. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1942. high(smallint)-low(smallint));
  1943. if assigned(ref.symbol) then
  1944. begin {Load symbol's value}
  1945. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1946. reference_reset(tmpref);
  1947. tmpref.symbol := ref.symbol;
  1948. tmpref.base := NR_RTOC;
  1949. if macos_direct_globals then
  1950. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1951. else
  1952. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1953. end;
  1954. if largeOffset then
  1955. begin {Add hi part of offset}
  1956. reference_reset(tmpref);
  1957. if Smallint(Lo(ref.offset)) < 0 then
  1958. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1959. else
  1960. tmpref.offset := Hi(ref.offset);
  1961. if (tmpreg <> NR_NO) then
  1962. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1963. else
  1964. begin
  1965. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1966. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1967. end;
  1968. end;
  1969. if (tmpreg <> NR_NO) then
  1970. begin
  1971. {Add content of base register}
  1972. if ref.base <> NR_NO then
  1973. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1974. ref.base,tmpreg));
  1975. {Make ref ready to be used by op}
  1976. ref.symbol:= nil;
  1977. ref.base:= tmpreg;
  1978. if largeOffset then
  1979. ref.offset := Smallint(Lo(ref.offset));
  1980. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1981. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1982. end
  1983. else
  1984. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1985. end
  1986. else {if target_info.system <> system_powerpc_macos}
  1987. begin
  1988. if assigned(ref.symbol) or
  1989. (cardinal(ref.offset-low(smallint)) >
  1990. high(smallint)-low(smallint)) then
  1991. begin
  1992. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1993. reference_reset(tmpref);
  1994. tmpref.symbol := ref.symbol;
  1995. tmpref.relsymbol := ref.relsymbol;
  1996. tmpref.offset := ref.offset;
  1997. tmpref.refaddr := addr_hi;
  1998. if ref.base <> NR_NO then
  1999. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  2000. ref.base,tmpref))
  2001. else
  2002. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  2003. ref.base := tmpreg;
  2004. ref.refaddr := addr_lo;
  2005. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2006. end
  2007. else
  2008. list.concat(taicpu.op_reg_ref(op,reg,ref));
  2009. end;
  2010. if (tmpreg <> NR_NO) then
  2011. rg[R_INTREGISTER].ungetregister(list,tmpreg);
  2012. end;
  2013. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  2014. crval: longint; l: tasmlabel);
  2015. var
  2016. p: taicpu;
  2017. begin
  2018. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name,AB_EXTERNAL,AT_FUNCTION));
  2019. if op <> A_B then
  2020. create_cond_norm(c,crval,p.condition);
  2021. p.is_jmp := true;
  2022. list.concat(p)
  2023. end;
  2024. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2025. begin
  2026. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2027. end;
  2028. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);
  2029. begin
  2030. a_op64_const_reg_reg(list,op,value,reg,reg);
  2031. end;
  2032. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2033. begin
  2034. case op of
  2035. OP_AND,OP_OR,OP_XOR:
  2036. begin
  2037. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2038. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2039. end;
  2040. OP_ADD:
  2041. begin
  2042. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2043. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2044. end;
  2045. OP_SUB:
  2046. begin
  2047. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2048. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2049. end;
  2050. else
  2051. internalerror(2002072801);
  2052. end;
  2053. end;
  2054. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);
  2055. const
  2056. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2057. (A_SUBIC,A_SUBC,A_ADDME));
  2058. var
  2059. tmpreg: tregister;
  2060. tmpreg64: tregister64;
  2061. issub: boolean;
  2062. begin
  2063. case op of
  2064. OP_AND,OP_OR,OP_XOR:
  2065. begin
  2066. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  2067. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2068. regdst.reghi);
  2069. end;
  2070. OP_ADD, OP_SUB:
  2071. begin
  2072. if (value < 0) then
  2073. begin
  2074. if op = OP_ADD then
  2075. op := OP_SUB
  2076. else
  2077. op := OP_ADD;
  2078. value := -value;
  2079. end;
  2080. if (longint(value) <> 0) then
  2081. begin
  2082. issub := op = OP_SUB;
  2083. if (value > 0) and
  2084. (value-ord(issub) <= 32767) then
  2085. begin
  2086. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2087. regdst.reglo,regsrc.reglo,longint(value)));
  2088. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2089. regdst.reghi,regsrc.reghi));
  2090. end
  2091. else if ((value shr 32) = 0) then
  2092. begin
  2093. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2094. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2095. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2096. regdst.reglo,regsrc.reglo,tmpreg));
  2097. tcgppc(cg).rg[R_INTREGISTER].ungetregister(list,tmpreg);
  2098. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2099. regdst.reghi,regsrc.reghi));
  2100. end
  2101. else
  2102. begin
  2103. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2104. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2105. a_load64_const_reg(list,value,tmpreg64);
  2106. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2107. tcgppc(cg).rg[R_INTREGISTER].ungetregister(list,tmpreg64.reglo);
  2108. tcgppc(cg).rg[R_INTREGISTER].ungetregister(list,tmpreg64.reghi);
  2109. end
  2110. end
  2111. else
  2112. begin
  2113. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2114. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2115. regdst.reghi);
  2116. end;
  2117. end;
  2118. else
  2119. internalerror(2002072802);
  2120. end;
  2121. end;
  2122. begin
  2123. cg := tcgppc.create;
  2124. cg64 :=tcg64fppc.create;
  2125. end.
  2126. {
  2127. $Log$
  2128. Revision 1.174 2004-07-01 18:00:00 jonas
  2129. * fixed several errors due to aword -> aint change
  2130. Revision 1.173 2004/06/20 08:55:32 florian
  2131. * logs truncated
  2132. Revision 1.172 2004/06/17 16:55:46 peter
  2133. * powerpc compiles again
  2134. Revision 1.171 2004/06/02 17:18:10 jonas
  2135. * parameters passed on the stack now also work as register variables
  2136. Revision 1.170 2004/05/31 18:08:41 jonas
  2137. * changed calling of external procedures to be the same as under gcc
  2138. (don't worry about all the generated stubs, they're optimized away
  2139. by the linker)
  2140. -> side effect: no need anymore to use special declarations for
  2141. external C functions under Darwin compared to other platforms
  2142. (it's still necessary for variables though)
  2143. Revision 1.169 2004/04/04 17:50:36 olle
  2144. * macos: fixed large offsets in references
  2145. Revision 1.168 2004/03/06 21:37:45 florian
  2146. * fixed ppc compilation
  2147. }