cgcpu.pas 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  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. hp: tparaitem;
  872. cond : tasmcond;
  873. instr : taicpu;
  874. size: tcgsize;
  875. begin
  876. { CR and LR only have to be saved in case they are modified by the current }
  877. { procedure, but currently this isn't checked, so save them always }
  878. { following is the entry code as described in "Altivec Programming }
  879. { Interface Manual", bar the saving of AltiVec registers }
  880. a_reg_alloc(list,NR_STACK_POINTER_REG);
  881. a_reg_alloc(list,NR_R0);
  882. usesfpr:=false;
  883. if not (po_assembler in current_procinfo.procdef.procoptions) then
  884. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  885. case target_info.abi of
  886. abi_powerpc_aix:
  887. firstfpureg := RS_F14;
  888. abi_powerpc_sysv:
  889. firstfpureg := RS_F9;
  890. else
  891. internalerror(2003122903);
  892. end;
  893. for regcounter:=firstfpureg to RS_F31 do
  894. begin
  895. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  896. begin
  897. usesfpr:= true;
  898. firstregfpu:=regcounter;
  899. break;
  900. end;
  901. end;
  902. usesgpr:=false;
  903. if not (po_assembler in current_procinfo.procdef.procoptions) then
  904. for regcounter2:=RS_R13 to RS_R31 do
  905. begin
  906. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  907. begin
  908. usesgpr:=true;
  909. firstreggpr:=regcounter2;
  910. break;
  911. end;
  912. end;
  913. { save link register? }
  914. if not (po_assembler in current_procinfo.procdef.procoptions) then
  915. if (pi_do_call in current_procinfo.flags) then
  916. begin
  917. { save return address... }
  918. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  919. { ... in caller's frame }
  920. case target_info.abi of
  921. abi_powerpc_aix:
  922. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  923. abi_powerpc_sysv:
  924. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  925. end;
  926. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  927. a_reg_dealloc(list,NR_R0);
  928. end;
  929. { save the CR if necessary in callers frame. }
  930. if not (po_assembler in current_procinfo.procdef.procoptions) then
  931. if target_info.abi = abi_powerpc_aix then
  932. if false then { Not needed at the moment. }
  933. begin
  934. a_reg_alloc(list,NR_R0);
  935. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  936. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  937. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  938. a_reg_dealloc(list,NR_R0);
  939. end;
  940. { !!! always allocate space for all registers for now !!! }
  941. if not (po_assembler in current_procinfo.procdef.procoptions) then
  942. { if usesfpr or usesgpr then }
  943. begin
  944. a_reg_alloc(list,NR_R12);
  945. { save end of fpr save area }
  946. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  947. end;
  948. if (not nostackframe) and
  949. (localsize <> 0) then
  950. begin
  951. if (localsize <= high(smallint)) then
  952. begin
  953. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  954. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  955. end
  956. else
  957. begin
  958. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  959. { can't use getregisterint here, the register colouring }
  960. { is already done when we get here }
  961. href.index := NR_R11;
  962. a_reg_alloc(list,href.index);
  963. a_load_const_reg(list,OS_S32,-localsize,href.index);
  964. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  965. a_reg_dealloc(list,href.index);
  966. end;
  967. end;
  968. { no GOT pointer loaded yet }
  969. gotgot:=false;
  970. if usesfpr then
  971. begin
  972. { save floating-point registers
  973. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  974. begin
  975. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  976. gotgot:=true;
  977. end
  978. else
  979. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14),AB_EXTERNAL,AT_FUNCTION));
  980. }
  981. reference_reset_base(href,NR_R12,-8);
  982. for regcounter:=firstregfpu to RS_F31 do
  983. begin
  984. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  985. begin
  986. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  987. dec(href.offset,8);
  988. end;
  989. end;
  990. { compute end of gpr save area }
  991. a_op_const_reg(list,OP_ADD,OS_ADDR,href.offset+8,NR_R12);
  992. end;
  993. { save gprs and fetch GOT pointer }
  994. if usesgpr then
  995. begin
  996. {
  997. if cs_create_pic in aktmoduleswitches then
  998. begin
  999. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g',AB_EXTERNAL,AT_FUNCTION));
  1000. gotgot:=true;
  1001. end
  1002. else
  1003. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14),AB_EXTERNAL,AT_FUNCTION))
  1004. }
  1005. reference_reset_base(href,NR_R12,-4);
  1006. for regcounter2:=RS_R13 to RS_R31 do
  1007. begin
  1008. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1009. begin
  1010. usesgpr:=true;
  1011. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  1012. dec(href.offset,4);
  1013. end;
  1014. end;
  1015. {
  1016. r.enum:=R_INTREGISTER;
  1017. r.:=;
  1018. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  1019. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1020. }
  1021. end;
  1022. if assigned(current_procinfo.procdef.parast) then
  1023. begin
  1024. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1025. begin
  1026. { copy memory parameters to local parast }
  1027. hp:=tparaitem(current_procinfo.procdef.para.first);
  1028. while assigned(hp) do
  1029. begin
  1030. if (hp.paraloc[calleeside].location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1031. begin
  1032. if assigned(hp.paraloc[callerside].location^.next) then
  1033. internalerror(2004091210);
  1034. case tabstractnormalvarsym(hp.parasym).localloc.loc of
  1035. LOC_REFERENCE:
  1036. begin
  1037. reference_reset_base(href,tabstractnormalvarsym(hp.parasym).localloc.reference.base,
  1038. tabstractnormalvarsym(hp.parasym).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. hp := tparaitem(hp.next);
  1085. end;
  1086. end;
  1087. end;
  1088. if usesfpr or usesgpr then
  1089. a_reg_dealloc(list,NR_R12);
  1090. { if we didn't get the GOT pointer till now, we've to calculate it now }
  1091. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  1092. case target_info.system of
  1093. system_powerpc_darwin:
  1094. begin
  1095. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1096. fillchar(cond,sizeof(cond),0);
  1097. cond.simple:=false;
  1098. cond.bo:=20;
  1099. cond.bi:=31;
  1100. instr:=taicpu.op_sym(A_BCL,current_procinfo.gotlabel);
  1101. instr.setcondition(cond);
  1102. list.concat(instr);
  1103. a_label(list,current_procinfo.gotlabel);
  1104. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  1105. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  1106. end;
  1107. else
  1108. begin
  1109. a_reg_alloc(list,NR_R31);
  1110. { place GOT ptr in r31 }
  1111. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  1112. end;
  1113. end;
  1114. { save the CR if necessary ( !!! always done currently ) }
  1115. { still need to find out where this has to be done for SystemV
  1116. a_reg_alloc(list,R_0);
  1117. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  1118. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  1119. new_reference(STACK_POINTER_REG,LA_CR)));
  1120. a_reg_dealloc(list,R_0); }
  1121. { now comes the AltiVec context save, not yet implemented !!! }
  1122. end;
  1123. procedure tcgppc.g_proc_exit(list : taasmoutput;parasize : longint;nostackframe:boolean);
  1124. { This procedure may be called before, as well as after g_stackframe_entry }
  1125. { is called. NOTE registers are not to be allocated through the register }
  1126. { allocator here, because the register colouring has already occured !! }
  1127. var
  1128. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  1129. href : treference;
  1130. usesfpr,usesgpr,genret : boolean;
  1131. regcounter2, firstfpureg:Tsuperregister;
  1132. localsize: aint;
  1133. begin
  1134. { AltiVec context restore, not yet implemented !!! }
  1135. usesfpr:=false;
  1136. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1137. begin
  1138. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1139. case target_info.abi of
  1140. abi_powerpc_aix:
  1141. firstfpureg := RS_F14;
  1142. abi_powerpc_sysv:
  1143. firstfpureg := RS_F9;
  1144. else
  1145. internalerror(2003122903);
  1146. end;
  1147. for regcounter:=firstfpureg to RS_F31 do
  1148. begin
  1149. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1150. begin
  1151. usesfpr:=true;
  1152. firstregfpu:=regcounter;
  1153. break;
  1154. end;
  1155. end;
  1156. end;
  1157. usesgpr:=false;
  1158. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1159. for regcounter2:=RS_R13 to RS_R31 do
  1160. begin
  1161. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1162. begin
  1163. usesgpr:=true;
  1164. firstreggpr:=regcounter2;
  1165. break;
  1166. end;
  1167. end;
  1168. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1169. { no return (blr) generated yet }
  1170. genret:=true;
  1171. if usesgpr or usesfpr then
  1172. begin
  1173. { address of gpr save area to r11 }
  1174. { (register allocator is no longer valid at this time and an add of 0 }
  1175. { is translated into a move, which is then registered with the register }
  1176. { allocator, causing a crash }
  1177. if (localsize <> 0) then
  1178. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12)
  1179. else
  1180. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  1181. if usesfpr then
  1182. begin
  1183. reference_reset_base(href,NR_R12,-8);
  1184. for regcounter := firstregfpu to RS_F31 do
  1185. begin
  1186. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1187. begin
  1188. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1189. dec(href.offset,8);
  1190. end;
  1191. end;
  1192. inc(href.offset,4);
  1193. end
  1194. else
  1195. reference_reset_base(href,NR_R12,-4);
  1196. for regcounter2:=RS_R13 to RS_R31 do
  1197. begin
  1198. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1199. begin
  1200. usesgpr:=true;
  1201. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1202. dec(href.offset,4);
  1203. end;
  1204. end;
  1205. (*
  1206. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1207. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1208. *)
  1209. end;
  1210. (*
  1211. { restore fprs and return }
  1212. if usesfpr then
  1213. begin
  1214. { address of fpr save area to r11 }
  1215. r:=NR_R12;
  1216. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1217. {
  1218. if (pi_do_call in current_procinfo.flags) then
  1219. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1220. '_x',AB_EXTERNAL,AT_FUNCTION))
  1221. else
  1222. { leaf node => lr haven't to be restored }
  1223. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1224. '_l');
  1225. genret:=false;
  1226. }
  1227. end;
  1228. *)
  1229. { if we didn't generate the return code, we've to do it now }
  1230. if genret then
  1231. begin
  1232. { adjust r1 }
  1233. { (register allocator is no longer valid at this time and an add of 0 }
  1234. { is translated into a move, which is then registered with the register }
  1235. { allocator, causing a crash }
  1236. if (not nostackframe) and
  1237. (localsize <> 0) then
  1238. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1239. { load link register? }
  1240. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1241. begin
  1242. if (pi_do_call in current_procinfo.flags) then
  1243. begin
  1244. case target_info.abi of
  1245. abi_powerpc_aix:
  1246. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1247. abi_powerpc_sysv:
  1248. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1249. end;
  1250. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1251. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1252. end;
  1253. { restore the CR if necessary from callers frame}
  1254. if target_info.abi = abi_powerpc_aix then
  1255. if false then { Not needed at the moment. }
  1256. begin
  1257. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1258. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1259. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1260. a_reg_dealloc(list,NR_R0);
  1261. end;
  1262. end;
  1263. list.concat(taicpu.op_none(A_BLR));
  1264. end;
  1265. end;
  1266. function tcgppc.save_regs(list : taasmoutput):longint;
  1267. {Generates code which saves used non-volatile registers in
  1268. the save area right below the address the stackpointer point to.
  1269. Returns the actual used save area size.}
  1270. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1271. usesfpr,usesgpr: boolean;
  1272. href : treference;
  1273. offset: aint;
  1274. regcounter2, firstfpureg: Tsuperregister;
  1275. begin
  1276. usesfpr:=false;
  1277. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1278. begin
  1279. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1280. case target_info.abi of
  1281. abi_powerpc_aix:
  1282. firstfpureg := RS_F14;
  1283. abi_powerpc_sysv:
  1284. firstfpureg := RS_F9;
  1285. else
  1286. internalerror(2003122903);
  1287. end;
  1288. for regcounter:=firstfpureg to RS_F31 do
  1289. begin
  1290. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1291. begin
  1292. usesfpr:=true;
  1293. firstregfpu:=regcounter;
  1294. break;
  1295. end;
  1296. end;
  1297. end;
  1298. usesgpr:=false;
  1299. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1300. for regcounter2:=RS_R13 to RS_R31 do
  1301. begin
  1302. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1303. begin
  1304. usesgpr:=true;
  1305. firstreggpr:=regcounter2;
  1306. break;
  1307. end;
  1308. end;
  1309. offset:= 0;
  1310. { save floating-point registers }
  1311. if usesfpr then
  1312. for regcounter := firstregfpu to RS_F31 do
  1313. begin
  1314. offset:= offset - 8;
  1315. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1316. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1317. end;
  1318. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1319. { save gprs in gpr save area }
  1320. if usesgpr then
  1321. if firstreggpr < RS_R30 then
  1322. begin
  1323. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1324. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1325. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1326. {STMW stores multiple registers}
  1327. end
  1328. else
  1329. begin
  1330. for regcounter := firstreggpr to RS_R31 do
  1331. begin
  1332. offset:= offset - 4;
  1333. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1334. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1335. end;
  1336. end;
  1337. { now comes the AltiVec context save, not yet implemented !!! }
  1338. save_regs:= -offset;
  1339. end;
  1340. procedure tcgppc.restore_regs(list : taasmoutput);
  1341. {Generates code which restores used non-volatile registers from
  1342. the save area right below the address the stackpointer point to.}
  1343. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1344. usesfpr,usesgpr: boolean;
  1345. href : treference;
  1346. offset: integer;
  1347. regcounter2, firstfpureg: Tsuperregister;
  1348. begin
  1349. usesfpr:=false;
  1350. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1351. begin
  1352. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1353. case target_info.abi of
  1354. abi_powerpc_aix:
  1355. firstfpureg := RS_F14;
  1356. abi_powerpc_sysv:
  1357. firstfpureg := RS_F9;
  1358. else
  1359. internalerror(2003122903);
  1360. end;
  1361. for regcounter:=firstfpureg to RS_F31 do
  1362. begin
  1363. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1364. begin
  1365. usesfpr:=true;
  1366. firstregfpu:=regcounter;
  1367. break;
  1368. end;
  1369. end;
  1370. end;
  1371. usesgpr:=false;
  1372. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1373. for regcounter2:=RS_R13 to RS_R31 do
  1374. begin
  1375. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1376. begin
  1377. usesgpr:=true;
  1378. firstreggpr:=regcounter2;
  1379. break;
  1380. end;
  1381. end;
  1382. offset:= 0;
  1383. { restore fp registers }
  1384. if usesfpr then
  1385. for regcounter := firstregfpu to RS_F31 do
  1386. begin
  1387. offset:= offset - 8;
  1388. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1389. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1390. end;
  1391. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1392. { restore gprs }
  1393. if usesgpr then
  1394. if firstreggpr < RS_R30 then
  1395. begin
  1396. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1397. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1398. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1399. {LMW loads multiple registers}
  1400. end
  1401. else
  1402. begin
  1403. for regcounter := firstreggpr to RS_R31 do
  1404. begin
  1405. offset:= offset - 4;
  1406. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1407. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1408. end;
  1409. end;
  1410. { now comes the AltiVec context restore, not yet implemented !!! }
  1411. end;
  1412. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1413. (* NOT IN USE *)
  1414. { generated the entry code of a procedure/function. Note: localsize is the }
  1415. { sum of the size necessary for local variables and the maximum possible }
  1416. { combined size of ALL the parameters of a procedure called by the current }
  1417. { one }
  1418. const
  1419. macosLinkageAreaSize = 24;
  1420. var regcounter: TRegister;
  1421. href : treference;
  1422. registerSaveAreaSize : longint;
  1423. begin
  1424. if (localsize mod 8) <> 0 then
  1425. internalerror(58991);
  1426. { CR and LR only have to be saved in case they are modified by the current }
  1427. { procedure, but currently this isn't checked, so save them always }
  1428. { following is the entry code as described in "Altivec Programming }
  1429. { Interface Manual", bar the saving of AltiVec registers }
  1430. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1431. a_reg_alloc(list,NR_R0);
  1432. { save return address in callers frame}
  1433. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1434. { ... in caller's frame }
  1435. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1436. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1437. a_reg_dealloc(list,NR_R0);
  1438. { save non-volatile registers in callers frame}
  1439. registerSaveAreaSize:= save_regs(list);
  1440. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1441. a_reg_alloc(list,NR_R0);
  1442. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1443. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1444. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1445. a_reg_dealloc(list,NR_R0);
  1446. (*
  1447. { save pointer to incoming arguments }
  1448. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1449. *)
  1450. (*
  1451. a_reg_alloc(list,R_12);
  1452. { 0 or 8 based on SP alignment }
  1453. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1454. R_12,STACK_POINTER_REG,0,28,28));
  1455. { add in stack length }
  1456. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1457. -localsize));
  1458. { establish new alignment }
  1459. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1460. a_reg_dealloc(list,R_12);
  1461. *)
  1462. { allocate stack frame }
  1463. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1464. inc(localsize,tg.lasttemp);
  1465. localsize:=align(localsize,16);
  1466. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1467. if (localsize <> 0) then
  1468. begin
  1469. if (localsize <= high(smallint)) then
  1470. begin
  1471. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1472. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1473. end
  1474. else
  1475. begin
  1476. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1477. href.index := NR_R11;
  1478. a_reg_alloc(list,href.index);
  1479. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1480. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1481. a_reg_dealloc(list,href.index);
  1482. end;
  1483. end;
  1484. end;
  1485. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aint);
  1486. (* NOT IN USE *)
  1487. var
  1488. href : treference;
  1489. begin
  1490. a_reg_alloc(list,NR_R0);
  1491. { restore stack pointer }
  1492. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1493. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1494. (*
  1495. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1496. *)
  1497. { restore the CR if necessary from callers frame
  1498. ( !!! always done currently ) }
  1499. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1500. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1501. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1502. a_reg_dealloc(list,NR_R0);
  1503. (*
  1504. { restore return address from callers frame }
  1505. reference_reset_base(href,STACK_POINTER_REG,8);
  1506. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1507. *)
  1508. { restore non-volatile registers from callers frame }
  1509. restore_regs(list);
  1510. (*
  1511. { return to caller }
  1512. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1513. list.concat(taicpu.op_none(A_BLR));
  1514. *)
  1515. { restore return address from callers frame }
  1516. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1517. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1518. { return to caller }
  1519. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1520. list.concat(taicpu.op_none(A_BLR));
  1521. end;
  1522. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1523. var
  1524. ref2, tmpref: treference;
  1525. tmpreg:Tregister;
  1526. begin
  1527. ref2 := ref;
  1528. fixref(list,ref2);
  1529. if assigned(ref2.symbol) then
  1530. begin
  1531. if target_info.system = system_powerpc_macos then
  1532. begin
  1533. if macos_direct_globals then
  1534. begin
  1535. reference_reset(tmpref);
  1536. tmpref.offset := ref2.offset;
  1537. tmpref.symbol := ref2.symbol;
  1538. tmpref.base := NR_NO;
  1539. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1540. end
  1541. else
  1542. begin
  1543. reference_reset(tmpref);
  1544. tmpref.symbol := ref2.symbol;
  1545. tmpref.offset := 0;
  1546. tmpref.base := NR_RTOC;
  1547. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1548. if ref2.offset <> 0 then
  1549. begin
  1550. reference_reset(tmpref);
  1551. tmpref.offset := ref2.offset;
  1552. tmpref.base:= r;
  1553. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1554. end;
  1555. end;
  1556. if ref2.base <> NR_NO then
  1557. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1558. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1559. end
  1560. else
  1561. begin
  1562. { add the symbol's value to the base of the reference, and if the }
  1563. { reference doesn't have a base, create one }
  1564. reference_reset(tmpref);
  1565. tmpref.offset := ref2.offset;
  1566. tmpref.symbol := ref2.symbol;
  1567. tmpref.relsymbol := ref2.relsymbol;
  1568. tmpref.refaddr := addr_hi;
  1569. if ref2.base<> NR_NO then
  1570. begin
  1571. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1572. ref2.base,tmpref));
  1573. end
  1574. else
  1575. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1576. tmpref.base := NR_NO;
  1577. tmpref.refaddr := addr_lo;
  1578. { can be folded with one of the next instructions by the }
  1579. { optimizer probably }
  1580. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1581. end
  1582. end
  1583. else if ref2.offset <> 0 Then
  1584. if ref2.base <> NR_NO then
  1585. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1586. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1587. { occurs, so now only ref.offset has to be loaded }
  1588. else
  1589. a_load_const_reg(list,OS_32,ref2.offset,r)
  1590. else if ref.index <> NR_NO Then
  1591. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1592. else if (ref2.base <> NR_NO) and
  1593. (r <> ref2.base) then
  1594. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1595. else
  1596. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1597. end;
  1598. { ************* concatcopy ************ }
  1599. {$ifndef ppc603}
  1600. const
  1601. maxmoveunit = 8;
  1602. {$else ppc603}
  1603. const
  1604. maxmoveunit = 4;
  1605. {$endif ppc603}
  1606. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);
  1607. var
  1608. countreg: TRegister;
  1609. src, dst: TReference;
  1610. lab: tasmlabel;
  1611. count, count2: aint;
  1612. orgsrc, orgdst: boolean;
  1613. size: tcgsize;
  1614. begin
  1615. {$ifdef extdebug}
  1616. if len > high(longint) then
  1617. internalerror(2002072704);
  1618. {$endif extdebug}
  1619. { make sure short loads are handled as optimally as possible }
  1620. if (len <= maxmoveunit) and
  1621. (byte(len) in [1,2,4,8]) then
  1622. begin
  1623. if len < 8 then
  1624. begin
  1625. size := int_cgsize(len);
  1626. a_load_ref_ref(list,size,size,source,dest);
  1627. end
  1628. else
  1629. begin
  1630. a_reg_alloc(list,NR_F0);
  1631. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1632. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1633. a_reg_dealloc(list,NR_F0);
  1634. end;
  1635. exit;
  1636. end;
  1637. count := len div maxmoveunit;
  1638. reference_reset(src);
  1639. reference_reset(dst);
  1640. { load the address of source into src.base }
  1641. if (count > 4) or
  1642. not issimpleref(source) or
  1643. ((source.index <> NR_NO) and
  1644. ((source.offset + longint(len)) > high(smallint))) then
  1645. begin
  1646. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1647. a_loadaddr_ref_reg(list,source,src.base);
  1648. orgsrc := false;
  1649. end
  1650. else
  1651. begin
  1652. src := source;
  1653. orgsrc := true;
  1654. end;
  1655. { load the address of dest into dst.base }
  1656. if (count > 4) or
  1657. not issimpleref(dest) or
  1658. ((dest.index <> NR_NO) and
  1659. ((dest.offset + longint(len)) > high(smallint))) then
  1660. begin
  1661. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1662. a_loadaddr_ref_reg(list,dest,dst.base);
  1663. orgdst := false;
  1664. end
  1665. else
  1666. begin
  1667. dst := dest;
  1668. orgdst := true;
  1669. end;
  1670. {$ifndef ppc603}
  1671. if count > 4 then
  1672. { generate a loop }
  1673. begin
  1674. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1675. { have to be set to 8. I put an Inc there so debugging may be }
  1676. { easier (should offset be different from zero here, it will be }
  1677. { easy to notice in the generated assembler }
  1678. inc(dst.offset,8);
  1679. inc(src.offset,8);
  1680. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1681. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1682. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1683. a_load_const_reg(list,OS_32,count,countreg);
  1684. { explicitely allocate R_0 since it can be used safely here }
  1685. { (for holding date that's being copied) }
  1686. a_reg_alloc(list,NR_F0);
  1687. objectlibrary.getlabel(lab);
  1688. a_label(list, lab);
  1689. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1690. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1691. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1692. a_jmp(list,A_BC,C_NE,0,lab);
  1693. a_reg_dealloc(list,NR_F0);
  1694. len := len mod 8;
  1695. end;
  1696. count := len div 8;
  1697. if count > 0 then
  1698. { unrolled loop }
  1699. begin
  1700. a_reg_alloc(list,NR_F0);
  1701. for count2 := 1 to count do
  1702. begin
  1703. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1704. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1705. inc(src.offset,8);
  1706. inc(dst.offset,8);
  1707. end;
  1708. a_reg_dealloc(list,NR_F0);
  1709. len := len mod 8;
  1710. end;
  1711. if (len and 4) <> 0 then
  1712. begin
  1713. a_reg_alloc(list,NR_R0);
  1714. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1715. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1716. inc(src.offset,4);
  1717. inc(dst.offset,4);
  1718. a_reg_dealloc(list,NR_R0);
  1719. end;
  1720. {$else not ppc603}
  1721. if count > 4 then
  1722. { generate a loop }
  1723. begin
  1724. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1725. { have to be set to 4. I put an Inc there so debugging may be }
  1726. { easier (should offset be different from zero here, it will be }
  1727. { easy to notice in the generated assembler }
  1728. inc(dst.offset,4);
  1729. inc(src.offset,4);
  1730. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1731. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1732. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1733. a_load_const_reg(list,OS_32,count,countreg);
  1734. { explicitely allocate R_0 since it can be used safely here }
  1735. { (for holding date that's being copied) }
  1736. a_reg_alloc(list,NR_R0);
  1737. objectlibrary.getlabel(lab);
  1738. a_label(list, lab);
  1739. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1740. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1741. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1742. a_jmp(list,A_BC,C_NE,0,lab);
  1743. a_reg_dealloc(list,NR_R0);
  1744. len := len mod 4;
  1745. end;
  1746. count := len div 4;
  1747. if count > 0 then
  1748. { unrolled loop }
  1749. begin
  1750. a_reg_alloc(list,NR_R0);
  1751. for count2 := 1 to count do
  1752. begin
  1753. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1754. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1755. inc(src.offset,4);
  1756. inc(dst.offset,4);
  1757. end;
  1758. a_reg_dealloc(list,NR_R0);
  1759. len := len mod 4;
  1760. end;
  1761. {$endif not ppc603}
  1762. { copy the leftovers }
  1763. if (len and 2) <> 0 then
  1764. begin
  1765. a_reg_alloc(list,NR_R0);
  1766. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1767. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1768. inc(src.offset,2);
  1769. inc(dst.offset,2);
  1770. a_reg_dealloc(list,NR_R0);
  1771. end;
  1772. if (len and 1) <> 0 then
  1773. begin
  1774. a_reg_alloc(list,NR_R0);
  1775. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1776. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1777. a_reg_dealloc(list,NR_R0);
  1778. end;
  1779. end;
  1780. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1781. var
  1782. hl : tasmlabel;
  1783. begin
  1784. if not(cs_check_overflow in aktlocalswitches) then
  1785. exit;
  1786. objectlibrary.getlabel(hl);
  1787. if not ((def.deftype=pointerdef) or
  1788. ((def.deftype=orddef) and
  1789. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1790. bool8bit,bool16bit,bool32bit]))) then
  1791. begin
  1792. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1793. a_jmp(list,A_BC,C_NO,7,hl)
  1794. end
  1795. else
  1796. a_jmp_cond(list,OC_AE,hl);
  1797. a_call_name(list,'FPC_OVERFLOW');
  1798. a_label(list,hl);
  1799. end;
  1800. {***************** This is private property, keep out! :) *****************}
  1801. function tcgppc.issimpleref(const ref: treference): boolean;
  1802. begin
  1803. if (ref.base = NR_NO) and
  1804. (ref.index <> NR_NO) then
  1805. internalerror(200208101);
  1806. result :=
  1807. not(assigned(ref.symbol)) and
  1808. (((ref.index = NR_NO) and
  1809. (ref.offset >= low(smallint)) and
  1810. (ref.offset <= high(smallint))) or
  1811. ((ref.index <> NR_NO) and
  1812. (ref.offset = 0)));
  1813. end;
  1814. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1815. var
  1816. tmpreg: tregister;
  1817. orgindex: tregister;
  1818. begin
  1819. result := false;
  1820. if (ref.base = NR_NO) then
  1821. begin
  1822. ref.base := ref.index;
  1823. ref.base := NR_NO;
  1824. end;
  1825. if (ref.base <> NR_NO) then
  1826. begin
  1827. if (ref.index <> NR_NO) and
  1828. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1829. begin
  1830. result := true;
  1831. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1832. list.concat(taicpu.op_reg_reg_reg(
  1833. A_ADD,tmpreg,ref.base,ref.index));
  1834. ref.index := NR_NO;
  1835. ref.base := tmpreg;
  1836. end
  1837. end
  1838. else
  1839. if ref.index <> NR_NO then
  1840. internalerror(200208102);
  1841. end;
  1842. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1843. { that's the case, we can use rlwinm to do an AND operation }
  1844. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1845. var
  1846. temp : longint;
  1847. testbit : aint;
  1848. compare: boolean;
  1849. begin
  1850. get_rlwi_const := false;
  1851. if (a = 0) or (a = -1) then
  1852. exit;
  1853. { start with the lowest bit }
  1854. testbit := 1;
  1855. { check its value }
  1856. compare := boolean(a and testbit);
  1857. { find out how long the run of bits with this value is }
  1858. { (it's impossible that all bits are 1 or 0, because in that case }
  1859. { this function wouldn't have been called) }
  1860. l1 := 31;
  1861. while (((a and testbit) <> 0) = compare) do
  1862. begin
  1863. testbit := testbit shl 1;
  1864. dec(l1);
  1865. end;
  1866. { check the length of the run of bits that comes next }
  1867. compare := not compare;
  1868. l2 := l1;
  1869. while (((a and testbit) <> 0) = compare) and
  1870. (l2 >= 0) do
  1871. begin
  1872. testbit := testbit shl 1;
  1873. dec(l2);
  1874. end;
  1875. { and finally the check whether the rest of the bits all have the }
  1876. { same value }
  1877. compare := not compare;
  1878. temp := l2;
  1879. if temp >= 0 then
  1880. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1881. exit;
  1882. { we have done "not(not(compare))", so compare is back to its }
  1883. { initial value. If the lowest bit was 0, a is of the form }
  1884. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1885. { because l2 now contains the position of the last zero of the }
  1886. { first run instead of that of the first 1) so switch l1 and l2 }
  1887. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1888. if not compare then
  1889. begin
  1890. temp := l1;
  1891. l1 := l2+1;
  1892. l2 := temp;
  1893. end
  1894. else
  1895. { otherwise, l1 currently contains the position of the last }
  1896. { zero instead of that of the first 1 of the second run -> +1 }
  1897. inc(l1);
  1898. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1899. l1 := l1 and 31;
  1900. l2 := l2 and 31;
  1901. get_rlwi_const := true;
  1902. end;
  1903. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1904. ref: treference);
  1905. var
  1906. tmpreg: tregister;
  1907. tmpref: treference;
  1908. largeOffset: Boolean;
  1909. begin
  1910. tmpreg := NR_NO;
  1911. if target_info.system = system_powerpc_macos then
  1912. begin
  1913. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1914. high(smallint)-low(smallint));
  1915. if assigned(ref.symbol) then
  1916. begin {Load symbol's value}
  1917. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1918. reference_reset(tmpref);
  1919. tmpref.symbol := ref.symbol;
  1920. tmpref.base := NR_RTOC;
  1921. if macos_direct_globals then
  1922. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1923. else
  1924. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1925. end;
  1926. if largeOffset then
  1927. begin {Add hi part of offset}
  1928. reference_reset(tmpref);
  1929. if Smallint(Lo(ref.offset)) < 0 then
  1930. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1931. else
  1932. tmpref.offset := Hi(ref.offset);
  1933. if (tmpreg <> NR_NO) then
  1934. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1935. else
  1936. begin
  1937. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1938. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1939. end;
  1940. end;
  1941. if (tmpreg <> NR_NO) then
  1942. begin
  1943. {Add content of base register}
  1944. if ref.base <> NR_NO then
  1945. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1946. ref.base,tmpreg));
  1947. {Make ref ready to be used by op}
  1948. ref.symbol:= nil;
  1949. ref.base:= tmpreg;
  1950. if largeOffset then
  1951. ref.offset := Smallint(Lo(ref.offset));
  1952. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1953. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1954. end
  1955. else
  1956. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1957. end
  1958. else {if target_info.system <> system_powerpc_macos}
  1959. begin
  1960. if assigned(ref.symbol) or
  1961. (cardinal(ref.offset-low(smallint)) >
  1962. high(smallint)-low(smallint)) then
  1963. begin
  1964. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1965. reference_reset(tmpref);
  1966. tmpref.symbol := ref.symbol;
  1967. tmpref.relsymbol := ref.relsymbol;
  1968. tmpref.offset := ref.offset;
  1969. tmpref.refaddr := addr_hi;
  1970. if ref.base <> NR_NO then
  1971. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1972. ref.base,tmpref))
  1973. else
  1974. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1975. ref.base := tmpreg;
  1976. ref.refaddr := addr_lo;
  1977. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1978. end
  1979. else
  1980. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1981. end;
  1982. end;
  1983. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1984. crval: longint; l: tasmlabel);
  1985. var
  1986. p: taicpu;
  1987. begin
  1988. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name,AB_EXTERNAL,AT_FUNCTION));
  1989. if op <> A_B then
  1990. create_cond_norm(c,crval,p.condition);
  1991. p.is_jmp := true;
  1992. list.concat(p)
  1993. end;
  1994. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  1995. begin
  1996. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  1997. end;
  1998. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : int64;reg : tregister64);
  1999. begin
  2000. a_op64_const_reg_reg(list,op,value,reg,reg);
  2001. end;
  2002. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2003. begin
  2004. case op of
  2005. OP_AND,OP_OR,OP_XOR:
  2006. begin
  2007. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2008. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2009. end;
  2010. OP_ADD:
  2011. begin
  2012. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2013. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2014. end;
  2015. OP_SUB:
  2016. begin
  2017. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2018. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2019. end;
  2020. else
  2021. internalerror(2002072801);
  2022. end;
  2023. end;
  2024. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : int64;regsrc,regdst : tregister64);
  2025. const
  2026. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2027. (A_SUBIC,A_SUBC,A_ADDME));
  2028. var
  2029. tmpreg: tregister;
  2030. tmpreg64: tregister64;
  2031. issub: boolean;
  2032. begin
  2033. case op of
  2034. OP_AND,OP_OR,OP_XOR:
  2035. begin
  2036. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  2037. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2038. regdst.reghi);
  2039. end;
  2040. OP_ADD, OP_SUB:
  2041. begin
  2042. if (value < 0) then
  2043. begin
  2044. if op = OP_ADD then
  2045. op := OP_SUB
  2046. else
  2047. op := OP_ADD;
  2048. value := -value;
  2049. end;
  2050. if (longint(value) <> 0) then
  2051. begin
  2052. issub := op = OP_SUB;
  2053. if (value > 0) and
  2054. (value-ord(issub) <= 32767) then
  2055. begin
  2056. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2057. regdst.reglo,regsrc.reglo,longint(value)));
  2058. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2059. regdst.reghi,regsrc.reghi));
  2060. end
  2061. else if ((value shr 32) = 0) then
  2062. begin
  2063. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2064. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2065. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2066. regdst.reglo,regsrc.reglo,tmpreg));
  2067. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2068. regdst.reghi,regsrc.reghi));
  2069. end
  2070. else
  2071. begin
  2072. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2073. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  2074. a_load64_const_reg(list,value,tmpreg64);
  2075. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2076. end
  2077. end
  2078. else
  2079. begin
  2080. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2081. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  2082. regdst.reghi);
  2083. end;
  2084. end;
  2085. else
  2086. internalerror(2002072802);
  2087. end;
  2088. end;
  2089. begin
  2090. cg := tcgppc.create;
  2091. cg64 :=tcg64fppc.create;
  2092. end.
  2093. {
  2094. $Log$
  2095. Revision 1.185 2004-11-11 19:31:33 peter
  2096. * fixed compile of powerpc,sparc,arm
  2097. Revision 1.184 2004/10/31 21:45:03 peter
  2098. * generic tlocation
  2099. * move tlocation to cgutils
  2100. Revision 1.183 2004/10/26 18:21:29 jonas
  2101. + empty g_save_standard_registers/g_restore_standard_registers overrides
  2102. (their work was/is done by g_proc_entry/g_proc_exit, and the generic
  2103. version saves the registers in the wrong place)
  2104. Revision 1.182 2004/10/24 20:01:08 peter
  2105. * remove saveregister calling convention
  2106. Revision 1.181 2004/10/24 11:53:45 peter
  2107. * fixed compilation with removed loadref
  2108. Revision 1.180 2004/10/20 07:32:42 jonas
  2109. + support for nostackframe directive
  2110. Revision 1.179 2004/10/11 07:13:14 jonas
  2111. * include pi_do_call if we generate a call instead of internalerroring
  2112. (workaround)
  2113. Revision 1.178 2004/09/25 14:23:54 peter
  2114. * ungetregister is now only used for cpuregisters, renamed to
  2115. ungetcpuregister
  2116. * renamed (get|unget)explicitregister(s) to ..cpuregister
  2117. * removed location-release/reference_release
  2118. Revision 1.177 2004/09/21 17:25:12 peter
  2119. * paraloc branch merged
  2120. Revision 1.176.4.2 2004/09/18 20:21:08 jonas
  2121. * fixed ppc, but still needs fix in tgobj
  2122. Revision 1.176.4.1 2004/09/10 11:10:08 florian
  2123. * first part of ppc fixes
  2124. Revision 1.176 2004/07/17 14:48:20 jonas
  2125. * fixed op_const_reg_reg for (OP_ADD,0,reg1,reg2)
  2126. Revision 1.175 2004/07/09 21:45:24 jonas
  2127. * fixed passing of fpu paras on the stack
  2128. * fixed number of fpu parameters passed in registers
  2129. * skip corresponding integer registers when using an fpu register for a
  2130. parameter under the AIX abi
  2131. Revision 1.174 2004/07/01 18:00:00 jonas
  2132. * fixed several errors due to aword -> aint change
  2133. Revision 1.173 2004/06/20 08:55:32 florian
  2134. * logs truncated
  2135. Revision 1.172 2004/06/17 16:55:46 peter
  2136. * powerpc compiles again
  2137. Revision 1.171 2004/06/02 17:18:10 jonas
  2138. * parameters passed on the stack now also work as register variables
  2139. Revision 1.170 2004/05/31 18:08:41 jonas
  2140. * changed calling of external procedures to be the same as under gcc
  2141. (don't worry about all the generated stubs, they're optimized away
  2142. by the linker)
  2143. -> side effect: no need anymore to use special declarations for
  2144. external C functions under Darwin compared to other platforms
  2145. (it's still necessary for variables though)
  2146. Revision 1.169 2004/04/04 17:50:36 olle
  2147. * macos: fixed large offsets in references
  2148. Revision 1.168 2004/03/06 21:37:45 florian
  2149. * fixed ppc compilation
  2150. }