cgcpu.pas 91 KB

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