cgcpu.pas 91 KB

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