cgcpu.pas 90 KB

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