cgcpu.pas 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,symtype,symdef,
  22. cgbase,cgobj,cgppc,
  23. aasmbase,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcgppcgen)
  28. procedure init_register_allocators;override;
  29. procedure done_register_allocators;override;
  30. { passing parameters, per default the parameter is pushed }
  31. { nr gives the number of the parameter (enumerated from }
  32. { left to right), this allows to move the parameter to }
  33. { register, if the cpu supports register calling }
  34. { conventions }
  35. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);override;
  36. procedure a_call_name(list : TAsmList;const s : string);override;
  37. procedure a_call_reg(list : TAsmList;reg: tregister); override;
  38. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  39. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  40. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  41. size: tcgsize; a: aint; src, dst: tregister); override;
  42. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  43. size: tcgsize; src1, src2, dst: tregister); override;
  44. { move instructions }
  45. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);override;
  46. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  47. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  48. procedure a_load_subsetreg_reg(list : TAsmList; subsetsize: tcgsize;
  49. tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister); override;
  50. procedure a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tcgsize; const fromsreg, tosreg: tsubsetregister); override;
  51. { comparison operations }
  52. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  53. l : tasmlabel);override;
  54. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  55. procedure a_jmp_name(list : TAsmList;const s : string); override;
  56. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  59. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  60. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  61. procedure g_save_standard_registers(list:TAsmList); override;
  62. procedure g_restore_standard_registers(list:TAsmList); override;
  63. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  64. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  65. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  66. { that's the case, we can use rlwinm to do an AND operation }
  67. function get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  68. protected
  69. procedure a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt); override;
  70. private
  71. (* NOT IN USE: *)
  72. procedure g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  73. (* NOT IN USE: *)
  74. procedure g_return_from_proc_mac(list : TAsmList;parasize : aint);
  75. { clear out potential overflow bits from 8 or 16 bit operations }
  76. { the upper 24/16 bits of a register after an operation }
  77. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  78. { Make sure ref is a valid reference for the PowerPC and sets the }
  79. { base to the value of the index if (base = R_NO). }
  80. { Returns true if the reference contained a base, index and an }
  81. { offset or symbol, in which case the base will have been changed }
  82. { to a tempreg (which has to be freed by the caller) containing }
  83. { the sum of part of the original reference }
  84. function fixref(list: TAsmList; var ref: treference): boolean; override;
  85. { returns whether a reference can be used immediately in a powerpc }
  86. { instruction }
  87. function issimpleref(const ref: treference): boolean;
  88. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  89. procedure a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  90. ref: treference); override;
  91. function save_regs(list : TAsmList):longint;
  92. procedure restore_regs(list : TAsmList);
  93. end;
  94. tcg64fppc = class(tcg64f32)
  95. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  96. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  97. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  98. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  99. end;
  100. const
  101. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDI,A_ANDI_,A_DIVWU,
  102. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  103. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  104. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_MR,A_ADDIS,A_ANDIS_,
  105. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  106. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  107. implementation
  108. uses
  109. globals,verbose,systems,cutils,
  110. symconst,symsym,fmodule,
  111. rgobj,tgobj,cpupi,procinfo,paramgr;
  112. procedure tcgppc.init_register_allocators;
  113. begin
  114. inherited init_register_allocators;
  115. if target_info.system=system_powerpc_darwin then
  116. begin
  117. {
  118. if pi_needs_got in current_procinfo.flags then
  119. begin
  120. current_procinfo.got:=NR_R31;
  121. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  122. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  123. RS_R9,RS_R10,RS_R11,RS_R12,RS_R30,RS_R29,
  124. RS_R28,RS_R27,RS_R26,RS_R25,RS_R24,RS_R23,RS_R22,
  125. RS_R21,RS_R20,RS_R19,RS_R18,RS_R17,RS_R16,RS_R15,
  126. RS_R14,RS_R13],first_int_imreg,[]);
  127. end
  128. else}
  129. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  130. [RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  131. RS_R9,RS_R10,RS_R11,RS_R12,RS_R31,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. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  144. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,RS_F8,RS_F9,
  145. RS_F10,RS_F11,RS_F12,RS_F13,RS_F31,RS_F30,RS_F29,RS_F28,RS_F27,
  146. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18,
  147. RS_F17,RS_F16,RS_F15,RS_F14],first_fpu_imreg,[]);
  148. {$warning FIX ME}
  149. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  150. [RS_M0,RS_M1,RS_M2],first_mm_imreg,[]);
  151. end;
  152. procedure tcgppc.done_register_allocators;
  153. begin
  154. rg[R_INTREGISTER].free;
  155. rg[R_FPUREGISTER].free;
  156. rg[R_MMREGISTER].free;
  157. inherited done_register_allocators;
  158. end;
  159. procedure tcgppc.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : tcgpara);
  160. var
  161. tmpref, ref: treference;
  162. location: pcgparalocation;
  163. sizeleft: aint;
  164. begin
  165. location := paraloc.location;
  166. tmpref := r;
  167. sizeleft := paraloc.intsize;
  168. while assigned(location) do
  169. begin
  170. case location^.loc of
  171. LOC_REGISTER,LOC_CREGISTER:
  172. begin
  173. {$ifndef cpu64bit}
  174. if (sizeleft <> 3) then
  175. begin
  176. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  177. end
  178. else
  179. begin
  180. a_load_ref_reg(list,OS_16,OS_16,tmpref,location^.register);
  181. a_reg_alloc(list,NR_R0);
  182. inc(tmpref.offset,2);
  183. a_load_ref_reg(list,OS_8,OS_8,tmpref,newreg(R_INTREGISTER,RS_R0,R_SUBNONE));
  184. a_op_const_reg(list,OP_SHL,OS_INT,16,location^.register);
  185. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location^.register,newreg(R_INTREGISTER,RS_R0,R_SUBNONE),8,16,31-8));
  186. a_reg_dealloc(list,NR_R0);
  187. dec(tmpref.offset,2);
  188. end;
  189. {$else not cpu64bit}
  190. {$error add 64 bit support for non power of 2 loads in a_param_ref}
  191. {$endif not cpu64bit}
  192. end;
  193. LOC_REFERENCE:
  194. begin
  195. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  196. g_concatcopy(list,tmpref,ref,sizeleft);
  197. if assigned(location^.next) then
  198. internalerror(2005010710);
  199. end;
  200. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  201. case location^.size of
  202. OS_F32, OS_F64:
  203. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  204. else
  205. internalerror(2002072801);
  206. end;
  207. LOC_VOID:
  208. begin
  209. // nothing to do
  210. end;
  211. else
  212. internalerror(2002081103);
  213. end;
  214. inc(tmpref.offset,tcgsize2size[location^.size]);
  215. dec(sizeleft,tcgsize2size[location^.size]);
  216. location := location^.next;
  217. end;
  218. end;
  219. { calling a procedure by name }
  220. procedure tcgppc.a_call_name(list : TAsmList;const s : string);
  221. begin
  222. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  223. if it is a cross-TOC call. If so, it also replaces the NOP
  224. with some restore code.}
  225. if (target_info.system <> system_powerpc_darwin) then
  226. begin
  227. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s)));
  228. if target_info.system=system_powerpc_macos then
  229. list.concat(taicpu.op_none(A_NOP));
  230. end
  231. else
  232. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s)));
  233. {
  234. the compiler does not properly set this flag anymore in pass 1, and
  235. for now we only need it after pass 2 (I hope) (JM)
  236. if not(pi_do_call in current_procinfo.flags) then
  237. internalerror(2003060703);
  238. }
  239. include(current_procinfo.flags,pi_do_call);
  240. end;
  241. { calling a procedure by address }
  242. procedure tcgppc.a_call_reg(list : TAsmList;reg: tregister);
  243. var
  244. tmpreg : tregister;
  245. tmpref : treference;
  246. begin
  247. if target_info.system=system_powerpc_macos then
  248. begin
  249. {Generate instruction to load the procedure address from
  250. the transition vector.}
  251. //TODO: Support cross-TOC calls.
  252. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  253. reference_reset(tmpref);
  254. tmpref.offset := 0;
  255. //tmpref.symaddr := refs_full;
  256. tmpref.base:= reg;
  257. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  258. end
  259. else
  260. tmpreg:=reg;
  261. inherited a_call_reg(list,tmpreg);
  262. end;
  263. {********************** load instructions ********************}
  264. procedure tcgppc.a_load_const_reg(list : TAsmList; size: TCGSize; a : aint; reg : TRegister);
  265. begin
  266. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  267. internalerror(2002090902);
  268. if (a >= low(smallint)) and
  269. (a <= high(smallint)) then
  270. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  271. else if ((a and $ffff) <> 0) then
  272. begin
  273. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  274. if ((a shr 16) <> 0) or
  275. (smallint(a and $ffff) < 0) then
  276. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  277. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  278. end
  279. else
  280. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  281. end;
  282. procedure tcgppc.a_load_ref_reg(list : TAsmList; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  283. const
  284. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  285. { indexed? updating?}
  286. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  287. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  288. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  289. { 64bit stuff should be handled separately }
  290. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  291. { 128bit stuff too }
  292. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  293. { there's no load-byte-with-sign-extend :( }
  294. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  295. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  296. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  297. var
  298. op: tasmop;
  299. ref2: treference;
  300. begin
  301. { TODO: optimize/take into consideration fromsize/tosize. Will }
  302. { probably only matter for OS_S8 loads though }
  303. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  304. internalerror(2002090902);
  305. ref2 := ref;
  306. fixref(list,ref2);
  307. { the caller is expected to have adjusted the reference already }
  308. { in this case }
  309. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  310. fromsize := tosize;
  311. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  312. a_load_store(list,op,reg,ref2);
  313. { sign extend shortint if necessary, since there is no }
  314. { load instruction that does that automatically (JM) }
  315. if fromsize = OS_S8 then
  316. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  317. end;
  318. procedure tcgppc.a_load_reg_reg(list : TAsmList;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  319. var
  320. instr: taicpu;
  321. begin
  322. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  323. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  324. (fromsize <> tosize)) or
  325. { needs to mask out the sign in the top 16 bits }
  326. ((fromsize = OS_S8) and
  327. (tosize = OS_16)) then
  328. case tosize of
  329. OS_8:
  330. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  331. reg2,reg1,0,31-8+1,31);
  332. OS_S8:
  333. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  334. OS_16:
  335. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  336. reg2,reg1,0,31-16+1,31);
  337. OS_S16:
  338. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  339. OS_32,OS_S32:
  340. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  341. else internalerror(2002090901);
  342. end
  343. else
  344. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  345. list.concat(instr);
  346. rg[R_INTREGISTER].add_move_instruction(instr);
  347. end;
  348. procedure tcgppc.a_load_subsetreg_reg(list : TAsmList; subsetsize, tosize: tcgsize; const sreg: tsubsetregister; destreg: tregister);
  349. begin
  350. if (sreg.bitlen <> sizeof(aint)*8) then
  351. begin
  352. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,destreg,
  353. sreg.subsetreg,(32-sreg.startbit) and 31,32-sreg.bitlen,31));
  354. { types with a negative lower bound are always a base type (8, 16, 32 bits) }
  355. if ((sreg.bitlen mod 8) = 0) then
  356. begin
  357. a_load_reg_reg(list,tcgsize2unsigned[subsetsize],subsetsize,destreg,destreg);
  358. a_load_reg_reg(list,subsetsize,tosize,destreg,destreg);
  359. end;
  360. end
  361. else
  362. a_load_reg_reg(list,subsetsize,tosize,sreg.subsetreg,destreg);
  363. end;
  364. procedure tcgppc.a_load_regconst_subsetreg_intern(list : TAsmList; fromsize, subsetsize: tcgsize; fromreg: tregister; const sreg: tsubsetregister; slopt: tsubsetloadopt);
  365. begin
  366. if (slopt in [SL_SETZERO,SL_SETMAX]) then
  367. inherited a_load_regconst_subsetreg_intern(list,fromsize,subsetsize,fromreg,sreg,slopt)
  368. else if (sreg.bitlen <> sizeof(aint) * 8) then
  369. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,sreg.subsetreg,fromreg,
  370. sreg.startbit,32-sreg.startbit-sreg.bitlen,31-sreg.startbit))
  371. else
  372. a_load_reg_reg(list,fromsize,subsetsize,fromreg,sreg.subsetreg);
  373. end;
  374. procedure tcgppc.a_load_subsetreg_subsetreg(list: TAsmlist; fromsubsetsize, tosubsetsize: tcgsize; const fromsreg, tosreg: tsubsetregister);
  375. begin
  376. if (fromsreg.bitlen >= tosreg.bitlen) then
  377. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,tosreg.subsetreg, fromsreg.subsetreg,
  378. (tosreg.startbit-fromsreg.startbit) and 31,
  379. 32-tosreg.startbit-tosreg.bitlen,31-tosreg.startbit))
  380. else
  381. inherited a_load_subsetreg_subsetreg(list,fromsubsetsize,tosubsetsize,fromsreg,tosreg);
  382. end;
  383. procedure tcgppc.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  384. begin
  385. a_op_const_reg_reg(list,op,size,a,reg,reg);
  386. end;
  387. procedure tcgppc.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  388. begin
  389. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  390. end;
  391. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  392. const
  393. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  394. begin
  395. if (op in overflowops) and
  396. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  397. a_load_reg_reg(list,OS_32,size,dst,dst);
  398. end;
  399. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  400. size: tcgsize; a: aint; src, dst: tregister);
  401. var
  402. l1,l2: longint;
  403. oplo, ophi: tasmop;
  404. scratchreg: tregister;
  405. useReg, gotrlwi: boolean;
  406. procedure do_lo_hi;
  407. begin
  408. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  409. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  410. end;
  411. begin
  412. if (op = OP_MOVE) then
  413. internalerror(2006031401);
  414. if op = OP_SUB then
  415. begin
  416. a_op_const_reg_reg(list,OP_ADD,size,-a,src,dst);
  417. exit;
  418. end;
  419. ophi := TOpCG2AsmOpConstHi[op];
  420. oplo := TOpCG2AsmOpConstLo[op];
  421. gotrlwi := get_rlwi_const(a,l1,l2);
  422. if (op in [OP_AND,OP_OR,OP_XOR]) then
  423. begin
  424. if (a = 0) then
  425. begin
  426. if op = OP_AND then
  427. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  428. else
  429. a_load_reg_reg(list,size,size,src,dst);
  430. exit;
  431. end
  432. else if (a = -1) then
  433. begin
  434. case op of
  435. OP_OR:
  436. case size of
  437. OS_8, OS_S8:
  438. list.concat(taicpu.op_reg_const(A_LI,dst,255));
  439. OS_16, OS_S16:
  440. a_load_const_reg(list,OS_16,65535,dst);
  441. else
  442. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  443. end;
  444. OP_XOR:
  445. case size of
  446. OS_8, OS_S8:
  447. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,255));
  448. OS_16, OS_S16:
  449. list.concat(taicpu.op_reg_reg_const(A_XORI,dst,src,65535));
  450. else
  451. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  452. end;
  453. OP_AND:
  454. a_load_reg_reg(list,size,size,src,dst);
  455. end;
  456. exit;
  457. end
  458. else if (aword(a) <= high(word)) and
  459. ((op <> OP_AND) or
  460. not gotrlwi) then
  461. begin
  462. if ((size = OS_8) and
  463. (byte(a) <> a)) or
  464. ((size = OS_S8) and
  465. (shortint(a) <> a)) then
  466. internalerror(200604142);
  467. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  468. { and/or/xor -> cannot overflow in high 16 bits }
  469. exit;
  470. end;
  471. { all basic constant instructions also have a shifted form that }
  472. { works only on the highest 16bits, so if lo(a) is 0, we can }
  473. { use that one }
  474. if (word(a) = 0) and
  475. (not(op = OP_AND) or
  476. not gotrlwi) then
  477. begin
  478. if (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  479. internalerror(200604141);
  480. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  481. exit;
  482. end;
  483. end
  484. else if (op = OP_ADD) then
  485. if a = 0 then
  486. begin
  487. a_load_reg_reg(list,size,size,src,dst);
  488. exit
  489. end
  490. else if (a >= low(smallint)) and
  491. (a <= high(smallint)) then
  492. begin
  493. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  494. maybeadjustresult(list,op,size,dst);
  495. exit;
  496. end;
  497. { otherwise, the instructions we can generate depend on the }
  498. { operation }
  499. useReg := false;
  500. case op of
  501. OP_DIV,OP_IDIV:
  502. if (a = 0) then
  503. internalerror(200208103)
  504. else if (a = 1) then
  505. begin
  506. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  507. exit
  508. end
  509. else if ispowerof2(a,l1) then
  510. begin
  511. case op of
  512. OP_DIV:
  513. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  514. OP_IDIV:
  515. begin
  516. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  517. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  518. end;
  519. end;
  520. exit;
  521. end
  522. else
  523. usereg := true;
  524. OP_IMUL, OP_MUL:
  525. if (a = 0) then
  526. begin
  527. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  528. exit
  529. end
  530. else if (a = 1) then
  531. begin
  532. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  533. exit
  534. end
  535. else if ispowerof2(a,l1) then
  536. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  537. else if (longint(a) >= low(smallint)) and
  538. (longint(a) <= high(smallint)) then
  539. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  540. else
  541. usereg := true;
  542. OP_ADD:
  543. begin
  544. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  545. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  546. smallint((a shr 16) + ord(smallint(a) < 0))));
  547. end;
  548. OP_OR:
  549. { try to use rlwimi }
  550. if gotrlwi and
  551. (src = dst) then
  552. begin
  553. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  554. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  555. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  556. scratchreg,0,l1,l2));
  557. end
  558. else
  559. do_lo_hi;
  560. OP_AND:
  561. { try to use rlwinm }
  562. if gotrlwi then
  563. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  564. src,0,l1,l2))
  565. else
  566. useReg := true;
  567. OP_XOR:
  568. do_lo_hi;
  569. OP_SHL,OP_SHR,OP_SAR:
  570. begin
  571. if (a and 31) <> 0 Then
  572. list.concat(taicpu.op_reg_reg_const(
  573. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  574. else
  575. a_load_reg_reg(list,size,size,src,dst);
  576. if (a shr 5) <> 0 then
  577. internalError(68991);
  578. end
  579. else
  580. internalerror(200109091);
  581. end;
  582. { if all else failed, load the constant in a register and then }
  583. { perform the operation }
  584. if useReg then
  585. begin
  586. scratchreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  587. a_load_const_reg(list,OS_32,a,scratchreg);
  588. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  589. end;
  590. maybeadjustresult(list,op,size,dst);
  591. end;
  592. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  593. size: tcgsize; src1, src2, dst: tregister);
  594. const
  595. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  596. (A_NONE,A_MR,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  597. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  598. begin
  599. if (op = OP_MOVE) then
  600. internalerror(2006031402);
  601. case op of
  602. OP_NEG,OP_NOT:
  603. begin
  604. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,src1));
  605. if (op = OP_NOT) and
  606. not(size in [OS_32,OS_S32]) then
  607. { zero/sign extend result again }
  608. a_load_reg_reg(list,OS_32,size,dst,dst);
  609. end;
  610. else
  611. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  612. end;
  613. maybeadjustresult(list,op,size,dst);
  614. end;
  615. {*************** compare instructructions ****************}
  616. procedure tcgppc.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  617. l : tasmlabel);
  618. var
  619. scratch_register: TRegister;
  620. signed: boolean;
  621. begin
  622. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE,OC_EQ,OC_NE];
  623. { in the following case, we generate more efficient code when }
  624. { signed is false }
  625. if (cmp_op in [OC_EQ,OC_NE]) and
  626. (aword(a) >= $8000) and
  627. (aword(a) <= $ffff) then
  628. signed := false;
  629. if signed then
  630. if (a >= low(smallint)) and (a <= high(smallint)) Then
  631. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,a))
  632. else
  633. begin
  634. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  635. a_load_const_reg(list,OS_32,a,scratch_register);
  636. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  637. end
  638. else
  639. if (aword(a) <= $ffff) then
  640. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,aword(a)))
  641. else
  642. begin
  643. scratch_register := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  644. a_load_const_reg(list,OS_32,a,scratch_register);
  645. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  646. end;
  647. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  648. end;
  649. procedure tcgppc.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  650. reg1,reg2 : tregister;l : tasmlabel);
  651. var
  652. op: tasmop;
  653. begin
  654. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  655. op := A_CMPW
  656. else
  657. op := A_CMPLW;
  658. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  659. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  660. end;
  661. procedure tcgppc.a_jmp_name(list : TAsmList;const s : string);
  662. var
  663. p : taicpu;
  664. begin
  665. if (target_info.system = system_powerpc_darwin) then
  666. p := taicpu.op_sym(A_B,get_darwin_call_stub(s))
  667. else
  668. p := taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  669. p.is_jmp := true;
  670. list.concat(p)
  671. end;
  672. procedure tcgppc.a_jmp_always(list : TAsmList;l: tasmlabel);
  673. begin
  674. a_jmp(list,A_B,C_None,0,l);
  675. end;
  676. procedure tcgppc.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  677. var
  678. c: tasmcond;
  679. begin
  680. c := flags_to_cond(f);
  681. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  682. end;
  683. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  684. var
  685. testbit: byte;
  686. bitvalue: boolean;
  687. begin
  688. { get the bit to extract from the conditional register + its }
  689. { requested value (0 or 1) }
  690. testbit := ((f.cr-RS_CR0) * 4);
  691. case f.flag of
  692. F_EQ,F_NE:
  693. begin
  694. inc(testbit,2);
  695. bitvalue := f.flag = F_EQ;
  696. end;
  697. F_LT,F_GE:
  698. begin
  699. bitvalue := f.flag = F_LT;
  700. end;
  701. F_GT,F_LE:
  702. begin
  703. inc(testbit);
  704. bitvalue := f.flag = F_GT;
  705. end;
  706. else
  707. internalerror(200112261);
  708. end;
  709. { load the conditional register in the destination reg }
  710. list.concat(taicpu.op_reg(A_MFCR,reg));
  711. { we will move the bit that has to be tested to bit 0 by rotating }
  712. { left }
  713. testbit := (testbit + 1) and 31;
  714. { extract bit }
  715. list.concat(taicpu.op_reg_reg_const_const_const(
  716. A_RLWINM,reg,reg,testbit,31,31));
  717. { if we need the inverse, xor with 1 }
  718. if not bitvalue then
  719. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  720. end;
  721. (*
  722. procedure tcgppc.g_cond2reg(list: TAsmList; const f: TAsmCond; reg: TRegister);
  723. var
  724. testbit: byte;
  725. bitvalue: boolean;
  726. begin
  727. { get the bit to extract from the conditional register + its }
  728. { requested value (0 or 1) }
  729. case f.simple of
  730. false:
  731. begin
  732. { we don't generate this in the compiler }
  733. internalerror(200109062);
  734. end;
  735. true:
  736. case f.cond of
  737. C_None:
  738. internalerror(200109063);
  739. C_LT..C_NU:
  740. begin
  741. testbit := (ord(f.cr) - ord(R_CR0))*4;
  742. inc(testbit,AsmCondFlag2BI[f.cond]);
  743. bitvalue := AsmCondFlagTF[f.cond];
  744. end;
  745. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  746. begin
  747. testbit := f.crbit
  748. bitvalue := AsmCondFlagTF[f.cond];
  749. end;
  750. else
  751. internalerror(200109064);
  752. end;
  753. end;
  754. { load the conditional register in the destination reg }
  755. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  756. { we will move the bit that has to be tested to bit 31 -> rotate }
  757. { left by bitpos+1 (remember, this is big-endian!) }
  758. if bitpos <> 31 then
  759. inc(bitpos)
  760. else
  761. bitpos := 0;
  762. { extract bit }
  763. list.concat(taicpu.op_reg_reg_const_const_const(
  764. A_RLWINM,reg,reg,bitpos,31,31));
  765. { if we need the inverse, xor with 1 }
  766. if not bitvalue then
  767. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  768. end;
  769. *)
  770. { *********** entry/exit code and address loading ************ }
  771. procedure tcgppc.g_save_standard_registers(list:TAsmList);
  772. begin
  773. { this work is done in g_proc_entry }
  774. end;
  775. procedure tcgppc.g_restore_standard_registers(list:TAsmList);
  776. begin
  777. { this work is done in g_proc_exit }
  778. end;
  779. procedure tcgppc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  780. { generated the entry code of a procedure/function. Note: localsize is the }
  781. { sum of the size necessary for local variables and the maximum possible }
  782. { combined size of ALL the parameters of a procedure called by the current }
  783. { one. }
  784. { This procedure may be called before, as well as after g_return_from_proc }
  785. { is called. NOTE registers are not to be allocated through the register }
  786. { allocator here, because the register colouring has already occured !! }
  787. var regcounter,firstregfpu,firstregint: TSuperRegister;
  788. href : treference;
  789. usesfpr,usesgpr,gotgot : boolean;
  790. cond : tasmcond;
  791. instr : taicpu;
  792. begin
  793. { CR and LR only have to be saved in case they are modified by the current }
  794. { procedure, but currently this isn't checked, so save them always }
  795. { following is the entry code as described in "Altivec Programming }
  796. { Interface Manual", bar the saving of AltiVec registers }
  797. a_reg_alloc(list,NR_STACK_POINTER_REG);
  798. usesgpr := false;
  799. usesfpr := false;
  800. if not(po_assembler in current_procinfo.procdef.procoptions) then
  801. begin
  802. { save link register? }
  803. if (pi_do_call in current_procinfo.flags) or
  804. ([cs_lineinfo,cs_debuginfo,cs_profile] * current_settings.moduleswitches <> []) then
  805. begin
  806. a_reg_alloc(list,NR_R0);
  807. { save return address... }
  808. { warning: if this is no longer done via r0, or if r0 is }
  809. { added to the usable registers, adapt tcgppcgen.g_profilecode }
  810. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  811. { ... in caller's frame }
  812. case target_info.abi of
  813. abi_powerpc_aix:
  814. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  815. abi_powerpc_sysv:
  816. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  817. end;
  818. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  819. if not(cs_profile in current_settings.moduleswitches) then
  820. a_reg_dealloc(list,NR_R0);
  821. end;
  822. (*
  823. { save the CR if necessary in callers frame. }
  824. if target_info.abi = abi_powerpc_aix then
  825. if false then { Not needed at the moment. }
  826. begin
  827. a_reg_alloc(list,NR_R0);
  828. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  829. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  830. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  831. a_reg_dealloc(list,NR_R0);
  832. end;
  833. *)
  834. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  835. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  836. usesgpr := firstregint <> 32;
  837. usesfpr := firstregfpu <> 32;
  838. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  839. begin
  840. a_reg_alloc(list,NR_R12);
  841. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  842. end;
  843. end;
  844. { no GOT pointer loaded yet }
  845. gotgot:=false;
  846. if usesfpr then
  847. begin
  848. { save floating-point registers
  849. if (cs_create_pic in current_settings.moduleswitches) and not(usesgpr) then
  850. begin
  851. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g'));
  852. gotgot:=true;
  853. end
  854. else
  855. a_call_name(current_asmdata.RefAsmSymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)));
  856. }
  857. reference_reset_base(href,NR_R1,-8);
  858. for regcounter:=firstregfpu to RS_F31 do
  859. begin
  860. a_loadfpu_reg_ref(list,OS_F64,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  861. dec(href.offset,8);
  862. end;
  863. { compute start of gpr save area }
  864. inc(href.offset,4);
  865. end
  866. else
  867. { compute start of gpr save area }
  868. reference_reset_base(href,NR_R1,-4);
  869. { save gprs and fetch GOT pointer }
  870. if usesgpr then
  871. begin
  872. {
  873. if cs_create_pic in current_settings.moduleswitches then
  874. begin
  875. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g'));
  876. gotgot:=true;
  877. end
  878. else
  879. a_call_name(current_asmdata.RefAsmSymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)))
  880. }
  881. if (firstregint <= RS_R22) or
  882. ((cs_opt_size in current_settings.optimizerswitches) and
  883. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  884. (firstregint <= RS_R29)) then
  885. begin
  886. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  887. list.concat(taicpu.op_reg_ref(A_STMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  888. end
  889. else
  890. for regcounter:=firstregint to RS_R31 do
  891. begin
  892. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter,R_SUBNONE),href);
  893. dec(href.offset,4);
  894. end;
  895. end;
  896. { done in ncgutil because it may only be released after the parameters }
  897. { have been moved to their final resting place }
  898. { if (tppcprocinfo(current_procinfo).needs_frame_pointer) then }
  899. { a_reg_dealloc(list,NR_R12); }
  900. { if we didn't get the GOT pointer till now, we've to calculate it now }
  901. (*
  902. if not(gotgot) and (pi_needs_got in current_procinfo.flags) then
  903. case target_info.system of
  904. system_powerpc_darwin:
  905. begin
  906. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  907. fillchar(cond,sizeof(cond),0);
  908. cond.simple:=false;
  909. cond.bo:=20;
  910. cond.bi:=31;
  911. instr:=taicpu.op_sym(A_BCL,current_procinfo.CurrGOTLabel);
  912. instr.setcondition(cond);
  913. list.concat(instr);
  914. a_label(list,current_procinfo.CurrGOTLabel);
  915. list.concat(taicpu.op_reg_reg(A_MFSPR,current_procinfo.got,NR_LR));
  916. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_LR,NR_R0));
  917. end;
  918. else
  919. begin
  920. a_reg_alloc(list,NR_R31);
  921. { place GOT ptr in r31 }
  922. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  923. end;
  924. end;
  925. *)
  926. if (not nostackframe) and
  927. (localsize <> 0) then
  928. begin
  929. if (localsize <= high(smallint)) then
  930. begin
  931. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  932. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  933. end
  934. else
  935. begin
  936. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  937. { can't use getregisterint here, the register colouring }
  938. { is already done when we get here }
  939. href.index := NR_R11;
  940. a_reg_alloc(list,href.index);
  941. a_load_const_reg(list,OS_S32,-localsize,href.index);
  942. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  943. a_reg_dealloc(list,href.index);
  944. end;
  945. end;
  946. { save the CR if necessary ( !!! never done currently ) }
  947. { still need to find out where this has to be done for SystemV
  948. a_reg_alloc(list,R_0);
  949. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  950. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  951. new_reference(STACK_POINTER_REG,LA_CR)));
  952. a_reg_dealloc(list,R_0);
  953. }
  954. { now comes the AltiVec context save, not yet implemented !!! }
  955. end;
  956. procedure tcgppc.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  957. { This procedure may be called before, as well as after g_stackframe_entry }
  958. { is called. NOTE registers are not to be allocated through the register }
  959. { allocator here, because the register colouring has already occured !! }
  960. var
  961. regcounter,firstregfpu,firstregint: TsuperRegister;
  962. href : treference;
  963. usesfpr,usesgpr,genret : boolean;
  964. localsize: aint;
  965. begin
  966. { AltiVec context restore, not yet implemented !!! }
  967. usesfpr:=false;
  968. usesgpr:=false;
  969. if not (po_assembler in current_procinfo.procdef.procoptions) then
  970. begin
  971. firstregfpu := tppcprocinfo(current_procinfo).get_first_save_fpu_reg;
  972. firstregint := tppcprocinfo(current_procinfo).get_first_save_int_reg;
  973. usesgpr := firstregint <> 32;
  974. usesfpr := firstregfpu <> 32;
  975. end;
  976. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  977. { adjust r1 }
  978. { (register allocator is no longer valid at this time and an add of 0 }
  979. { is translated into a move, which is then registered with the register }
  980. { allocator, causing a crash }
  981. if (not nostackframe) and
  982. (localsize <> 0) then
  983. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  984. { no return (blr) generated yet }
  985. genret:=true;
  986. if usesfpr then
  987. begin
  988. reference_reset_base(href,NR_R1,-8);
  989. for regcounter := firstregfpu to RS_F31 do
  990. begin
  991. a_loadfpu_ref_reg(list,OS_F64,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  992. dec(href.offset,8);
  993. end;
  994. inc(href.offset,4);
  995. end
  996. else
  997. reference_reset_base(href,NR_R1,-4);
  998. if (usesgpr) then
  999. begin
  1000. if (firstregint <= RS_R22) or
  1001. ((cs_opt_size in current_settings.optimizerswitches) and
  1002. { with RS_R30 it's also already smaller, but too big a speed trade-off to make }
  1003. (firstregint <= RS_R29)) then
  1004. begin
  1005. dec(href.offset,(RS_R31-firstregint)*sizeof(aint));
  1006. list.concat(taicpu.op_reg_ref(A_LMW,newreg(R_INTREGISTER,firstregint,R_SUBNONE),href));
  1007. end
  1008. else
  1009. for regcounter:=firstregint to RS_R31 do
  1010. begin
  1011. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter,R_SUBNONE));
  1012. dec(href.offset,4);
  1013. end;
  1014. end;
  1015. (*
  1016. { restore fprs and return }
  1017. if usesfpr then
  1018. begin
  1019. { address of fpr save area to r11 }
  1020. r:=NR_R12;
  1021. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1022. {
  1023. if (pi_do_call in current_procinfo.flags) then
  1024. a_call_name(current_asmdata.RefAsmSymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_x'))
  1025. else
  1026. { leaf node => lr haven't to be restored }
  1027. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+'_l');
  1028. genret:=false;
  1029. }
  1030. end;
  1031. *)
  1032. { if we didn't generate the return code, we've to do it now }
  1033. if genret then
  1034. begin
  1035. { load link register? }
  1036. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1037. begin
  1038. if (pi_do_call in current_procinfo.flags) then
  1039. begin
  1040. case target_info.abi of
  1041. abi_powerpc_aix:
  1042. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1043. abi_powerpc_sysv:
  1044. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1045. end;
  1046. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1047. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1048. end;
  1049. (*
  1050. { restore the CR if necessary from callers frame}
  1051. if target_info.abi = abi_powerpc_aix then
  1052. if false then { Not needed at the moment. }
  1053. begin
  1054. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1055. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1056. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1057. a_reg_dealloc(list,NR_R0);
  1058. end;
  1059. *)
  1060. end;
  1061. list.concat(taicpu.op_none(A_BLR));
  1062. end;
  1063. end;
  1064. function tcgppc.save_regs(list : TAsmList):longint;
  1065. {Generates code which saves used non-volatile registers in
  1066. the save area right below the address the stackpointer point to.
  1067. Returns the actual used save area size.}
  1068. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1069. usesfpr,usesgpr: boolean;
  1070. href : treference;
  1071. offset: aint;
  1072. regcounter2, firstfpureg: Tsuperregister;
  1073. begin
  1074. usesfpr:=false;
  1075. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1076. begin
  1077. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1078. case target_info.abi of
  1079. abi_powerpc_aix:
  1080. firstfpureg := RS_F14;
  1081. abi_powerpc_sysv:
  1082. firstfpureg := RS_F9;
  1083. else
  1084. internalerror(2003122903);
  1085. end;
  1086. for regcounter:=firstfpureg to RS_F31 do
  1087. begin
  1088. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1089. begin
  1090. usesfpr:=true;
  1091. firstregfpu:=regcounter;
  1092. break;
  1093. end;
  1094. end;
  1095. end;
  1096. usesgpr:=false;
  1097. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1098. for regcounter2:=RS_R13 to RS_R31 do
  1099. begin
  1100. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1101. begin
  1102. usesgpr:=true;
  1103. firstreggpr:=regcounter2;
  1104. break;
  1105. end;
  1106. end;
  1107. offset:= 0;
  1108. { save floating-point registers }
  1109. if usesfpr then
  1110. for regcounter := firstregfpu to RS_F31 do
  1111. begin
  1112. offset:= offset - 8;
  1113. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1114. list.concat(taicpu.op_reg_ref(A_STFD, tregister(regcounter), href));
  1115. end;
  1116. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1117. { save gprs in gpr save area }
  1118. if usesgpr then
  1119. if firstreggpr < RS_R30 then
  1120. begin
  1121. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1122. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1123. list.concat(taicpu.op_reg_ref(A_STMW,tregister(firstreggpr),href));
  1124. {STMW stores multiple registers}
  1125. end
  1126. else
  1127. begin
  1128. for regcounter := firstreggpr to RS_R31 do
  1129. begin
  1130. offset:= offset - 4;
  1131. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1132. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1133. end;
  1134. end;
  1135. { now comes the AltiVec context save, not yet implemented !!! }
  1136. save_regs:= -offset;
  1137. end;
  1138. procedure tcgppc.restore_regs(list : TAsmList);
  1139. {Generates code which restores used non-volatile registers from
  1140. the save area right below the address the stackpointer point to.}
  1141. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1142. usesfpr,usesgpr: boolean;
  1143. href : treference;
  1144. offset: integer;
  1145. regcounter2, firstfpureg: Tsuperregister;
  1146. begin
  1147. usesfpr:=false;
  1148. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1149. begin
  1150. { FIXME: has to be R_F14 instad of R_F8 for SYSV-64bit }
  1151. case target_info.abi of
  1152. abi_powerpc_aix:
  1153. firstfpureg := RS_F14;
  1154. abi_powerpc_sysv:
  1155. firstfpureg := RS_F9;
  1156. else
  1157. internalerror(2003122903);
  1158. end;
  1159. for regcounter:=firstfpureg to RS_F31 do
  1160. begin
  1161. if regcounter in rg[R_FPUREGISTER].used_in_proc then
  1162. begin
  1163. usesfpr:=true;
  1164. firstregfpu:=regcounter;
  1165. break;
  1166. end;
  1167. end;
  1168. end;
  1169. usesgpr:=false;
  1170. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1171. for regcounter2:=RS_R13 to RS_R31 do
  1172. begin
  1173. if regcounter2 in rg[R_INTREGISTER].used_in_proc then
  1174. begin
  1175. usesgpr:=true;
  1176. firstreggpr:=regcounter2;
  1177. break;
  1178. end;
  1179. end;
  1180. offset:= 0;
  1181. { restore fp registers }
  1182. if usesfpr then
  1183. for regcounter := firstregfpu to RS_F31 do
  1184. begin
  1185. offset:= offset - 8;
  1186. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1187. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1188. end;
  1189. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1190. { restore gprs }
  1191. if usesgpr then
  1192. if firstreggpr < RS_R30 then
  1193. begin
  1194. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1195. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1196. list.concat(taicpu.op_reg_ref(A_LMW,tregister(firstreggpr),href));
  1197. {LMW loads multiple registers}
  1198. end
  1199. else
  1200. begin
  1201. for regcounter := firstreggpr to RS_R31 do
  1202. begin
  1203. offset:= offset - 4;
  1204. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1205. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1206. end;
  1207. end;
  1208. { now comes the AltiVec context restore, not yet implemented !!! }
  1209. end;
  1210. procedure tcgppc.g_stackframe_entry_mac(list : TAsmList;localsize : longint);
  1211. (* NOT IN USE *)
  1212. { generated the entry code of a procedure/function. Note: localsize is the }
  1213. { sum of the size necessary for local variables and the maximum possible }
  1214. { combined size of ALL the parameters of a procedure called by the current }
  1215. { one }
  1216. const
  1217. macosLinkageAreaSize = 24;
  1218. var
  1219. href : treference;
  1220. registerSaveAreaSize : longint;
  1221. begin
  1222. if (localsize mod 8) <> 0 then
  1223. internalerror(58991);
  1224. { CR and LR only have to be saved in case they are modified by the current }
  1225. { procedure, but currently this isn't checked, so save them always }
  1226. { following is the entry code as described in "Altivec Programming }
  1227. { Interface Manual", bar the saving of AltiVec registers }
  1228. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1229. a_reg_alloc(list,NR_R0);
  1230. { save return address in callers frame}
  1231. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1232. { ... in caller's frame }
  1233. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1234. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1235. a_reg_dealloc(list,NR_R0);
  1236. { save non-volatile registers in callers frame}
  1237. registerSaveAreaSize:= save_regs(list);
  1238. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1239. a_reg_alloc(list,NR_R0);
  1240. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1241. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1242. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1243. a_reg_dealloc(list,NR_R0);
  1244. (*
  1245. { save pointer to incoming arguments }
  1246. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1247. *)
  1248. (*
  1249. a_reg_alloc(list,R_12);
  1250. { 0 or 8 based on SP alignment }
  1251. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1252. R_12,STACK_POINTER_REG,0,28,28));
  1253. { add in stack length }
  1254. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1255. -localsize));
  1256. { establish new alignment }
  1257. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1258. a_reg_dealloc(list,R_12);
  1259. *)
  1260. { allocate stack frame }
  1261. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1262. inc(localsize,tg.lasttemp);
  1263. localsize:=align(localsize,16);
  1264. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1265. if (localsize <> 0) then
  1266. begin
  1267. if (localsize <= high(smallint)) then
  1268. begin
  1269. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1270. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1271. end
  1272. else
  1273. begin
  1274. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1275. href.index := NR_R11;
  1276. a_reg_alloc(list,href.index);
  1277. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1278. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1279. a_reg_dealloc(list,href.index);
  1280. end;
  1281. end;
  1282. end;
  1283. procedure tcgppc.g_return_from_proc_mac(list : TAsmList;parasize : aint);
  1284. (* NOT IN USE *)
  1285. var
  1286. href : treference;
  1287. begin
  1288. a_reg_alloc(list,NR_R0);
  1289. { restore stack pointer }
  1290. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1291. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1292. (*
  1293. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1294. *)
  1295. { restore the CR if necessary from callers frame
  1296. ( !!! always done currently ) }
  1297. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1298. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1299. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1300. a_reg_dealloc(list,NR_R0);
  1301. (*
  1302. { restore return address from callers frame }
  1303. reference_reset_base(href,STACK_POINTER_REG,8);
  1304. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1305. *)
  1306. { restore non-volatile registers from callers frame }
  1307. restore_regs(list);
  1308. (*
  1309. { return to caller }
  1310. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1311. list.concat(taicpu.op_none(A_BLR));
  1312. *)
  1313. { restore return address from callers frame }
  1314. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1315. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1316. { return to caller }
  1317. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1318. list.concat(taicpu.op_none(A_BLR));
  1319. end;
  1320. procedure tcgppc.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1321. var
  1322. ref2, tmpref: treference;
  1323. begin
  1324. ref2 := ref;
  1325. fixref(list,ref2);
  1326. if assigned(ref2.symbol) then
  1327. begin
  1328. if target_info.system = system_powerpc_macos then
  1329. begin
  1330. if macos_direct_globals then
  1331. begin
  1332. reference_reset(tmpref);
  1333. tmpref.offset := ref2.offset;
  1334. tmpref.symbol := ref2.symbol;
  1335. tmpref.base := NR_NO;
  1336. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1337. end
  1338. else
  1339. begin
  1340. reference_reset(tmpref);
  1341. tmpref.symbol := ref2.symbol;
  1342. tmpref.offset := 0;
  1343. tmpref.base := NR_RTOC;
  1344. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1345. if ref2.offset <> 0 then
  1346. begin
  1347. reference_reset(tmpref);
  1348. tmpref.offset := ref2.offset;
  1349. tmpref.base:= r;
  1350. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1351. end;
  1352. end;
  1353. if ref2.base <> NR_NO then
  1354. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1355. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1356. end
  1357. else
  1358. begin
  1359. { add the symbol's value to the base of the reference, and if the }
  1360. { reference doesn't have a base, create one }
  1361. reference_reset(tmpref);
  1362. tmpref.offset := ref2.offset;
  1363. tmpref.symbol := ref2.symbol;
  1364. tmpref.relsymbol := ref2.relsymbol;
  1365. tmpref.refaddr := addr_hi;
  1366. if ref2.base<> NR_NO then
  1367. begin
  1368. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1369. ref2.base,tmpref));
  1370. end
  1371. else
  1372. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1373. tmpref.base := NR_NO;
  1374. tmpref.refaddr := addr_lo;
  1375. { can be folded with one of the next instructions by the }
  1376. { optimizer probably }
  1377. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1378. end
  1379. end
  1380. else if ref2.offset <> 0 Then
  1381. if ref2.base <> NR_NO then
  1382. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  1383. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1384. { occurs, so now only ref.offset has to be loaded }
  1385. else
  1386. a_load_const_reg(list,OS_32,ref2.offset,r)
  1387. else if ref2.index <> NR_NO Then
  1388. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1389. else if (ref2.base <> NR_NO) and
  1390. (r <> ref2.base) then
  1391. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  1392. else
  1393. list.concat(taicpu.op_reg_const(A_LI,r,0));
  1394. end;
  1395. { ************* concatcopy ************ }
  1396. {$ifndef ppc603}
  1397. const
  1398. maxmoveunit = 8;
  1399. {$else ppc603}
  1400. const
  1401. maxmoveunit = 4;
  1402. {$endif ppc603}
  1403. procedure tcgppc.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1404. var
  1405. countreg: TRegister;
  1406. src, dst: TReference;
  1407. lab: tasmlabel;
  1408. count, count2: aint;
  1409. size: tcgsize;
  1410. copyreg: tregister;
  1411. begin
  1412. {$ifdef extdebug}
  1413. if len > high(longint) then
  1414. internalerror(2002072704);
  1415. {$endif extdebug}
  1416. if (references_equal(source,dest)) then
  1417. exit;
  1418. { make sure short loads are handled as optimally as possible }
  1419. if (len <= maxmoveunit) and
  1420. (byte(len) in [1,2,4,8]) then
  1421. begin
  1422. if len < 8 then
  1423. begin
  1424. size := int_cgsize(len);
  1425. a_load_ref_ref(list,size,size,source,dest);
  1426. end
  1427. else
  1428. begin
  1429. copyreg := getfpuregister(list,OS_F64);
  1430. a_loadfpu_ref_reg(list,OS_F64,OS_F64,source,copyreg);
  1431. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dest);
  1432. end;
  1433. exit;
  1434. end;
  1435. count := len div maxmoveunit;
  1436. reference_reset(src);
  1437. reference_reset(dst);
  1438. { load the address of source into src.base }
  1439. if (count > 4) or
  1440. not issimpleref(source) or
  1441. ((source.index <> NR_NO) and
  1442. ((source.offset + longint(len)) > high(smallint))) then
  1443. begin
  1444. src.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1445. a_loadaddr_ref_reg(list,source,src.base);
  1446. end
  1447. else
  1448. begin
  1449. src := source;
  1450. end;
  1451. { load the address of dest into dst.base }
  1452. if (count > 4) or
  1453. not issimpleref(dest) or
  1454. ((dest.index <> NR_NO) and
  1455. ((dest.offset + longint(len)) > high(smallint))) then
  1456. begin
  1457. dst.base := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1458. a_loadaddr_ref_reg(list,dest,dst.base);
  1459. end
  1460. else
  1461. begin
  1462. dst := dest;
  1463. end;
  1464. {$ifndef ppc603}
  1465. if count > 4 then
  1466. { generate a loop }
  1467. begin
  1468. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1469. { have to be set to 8. I put an Inc there so debugging may be }
  1470. { easier (should offset be different from zero here, it will be }
  1471. { easy to notice in the generated assembler }
  1472. inc(dst.offset,8);
  1473. inc(src.offset,8);
  1474. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1475. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1476. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1477. a_load_const_reg(list,OS_32,count,countreg);
  1478. copyreg := getfpuregister(list,OS_F64);
  1479. a_reg_sync(list,copyreg);
  1480. current_asmdata.getjumplabel(lab);
  1481. a_label(list, lab);
  1482. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1483. list.concat(taicpu.op_reg_ref(A_LFDU,copyreg,src));
  1484. list.concat(taicpu.op_reg_ref(A_STFDU,copyreg,dst));
  1485. a_jmp(list,A_BC,C_NE,0,lab);
  1486. a_reg_sync(list,copyreg);
  1487. len := len mod 8;
  1488. end;
  1489. count := len div 8;
  1490. if count > 0 then
  1491. { unrolled loop }
  1492. begin
  1493. copyreg := getfpuregister(list,OS_F64);
  1494. for count2 := 1 to count do
  1495. begin
  1496. a_loadfpu_ref_reg(list,OS_F64,OS_F64,src,copyreg);
  1497. a_loadfpu_reg_ref(list,OS_F64,OS_F64,copyreg,dst);
  1498. inc(src.offset,8);
  1499. inc(dst.offset,8);
  1500. end;
  1501. len := len mod 8;
  1502. end;
  1503. if (len and 4) <> 0 then
  1504. begin
  1505. a_reg_alloc(list,NR_R0);
  1506. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1507. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1508. inc(src.offset,4);
  1509. inc(dst.offset,4);
  1510. a_reg_dealloc(list,NR_R0);
  1511. end;
  1512. {$else not ppc603}
  1513. if count > 4 then
  1514. { generate a loop }
  1515. begin
  1516. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1517. { have to be set to 4. I put an Inc there so debugging may be }
  1518. { easier (should offset be different from zero here, it will be }
  1519. { easy to notice in the generated assembler }
  1520. inc(dst.offset,4);
  1521. inc(src.offset,4);
  1522. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1523. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1524. countreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1525. a_load_const_reg(list,OS_32,count,countreg);
  1526. { explicitely allocate R_0 since it can be used safely here }
  1527. { (for holding date that's being copied) }
  1528. a_reg_alloc(list,NR_R0);
  1529. current_asmdata.getjumplabel(lab);
  1530. a_label(list, lab);
  1531. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1532. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1533. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1534. a_jmp(list,A_BC,C_NE,0,lab);
  1535. a_reg_dealloc(list,NR_R0);
  1536. len := len mod 4;
  1537. end;
  1538. count := len div 4;
  1539. if count > 0 then
  1540. { unrolled loop }
  1541. begin
  1542. a_reg_alloc(list,NR_R0);
  1543. for count2 := 1 to count do
  1544. begin
  1545. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1546. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1547. inc(src.offset,4);
  1548. inc(dst.offset,4);
  1549. end;
  1550. a_reg_dealloc(list,NR_R0);
  1551. len := len mod 4;
  1552. end;
  1553. {$endif not ppc603}
  1554. { copy the leftovers }
  1555. if (len and 2) <> 0 then
  1556. begin
  1557. a_reg_alloc(list,NR_R0);
  1558. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1559. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1560. inc(src.offset,2);
  1561. inc(dst.offset,2);
  1562. a_reg_dealloc(list,NR_R0);
  1563. end;
  1564. if (len and 1) <> 0 then
  1565. begin
  1566. a_reg_alloc(list,NR_R0);
  1567. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1568. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1569. a_reg_dealloc(list,NR_R0);
  1570. end;
  1571. end;
  1572. {***************** This is private property, keep out! :) *****************}
  1573. function tcgppc.issimpleref(const ref: treference): boolean;
  1574. begin
  1575. if (ref.base = NR_NO) and
  1576. (ref.index <> NR_NO) then
  1577. internalerror(200208101);
  1578. result :=
  1579. not(assigned(ref.symbol)) and
  1580. (((ref.index = NR_NO) and
  1581. (ref.offset >= low(smallint)) and
  1582. (ref.offset <= high(smallint))) or
  1583. ((ref.index <> NR_NO) and
  1584. (ref.offset = 0)));
  1585. end;
  1586. function tcgppc.fixref(list: TAsmList; var ref: treference): boolean;
  1587. var
  1588. tmpreg: tregister;
  1589. begin
  1590. result := false;
  1591. if (target_info.system = system_powerpc_darwin) and
  1592. assigned(ref.symbol) and
  1593. (ref.symbol.bind = AB_EXTERNAL) then
  1594. begin
  1595. tmpreg := g_indirect_sym_load(list,ref.symbol.name);
  1596. if (ref.base = NR_NO) then
  1597. ref.base := tmpreg
  1598. else if (ref.index = NR_NO) then
  1599. ref.index := tmpreg
  1600. else
  1601. begin
  1602. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1603. ref.base := tmpreg;
  1604. end;
  1605. ref.symbol := nil;
  1606. end;
  1607. if (ref.base = NR_NO) then
  1608. begin
  1609. ref.base := ref.index;
  1610. ref.index := NR_NO;
  1611. end;
  1612. if (ref.base <> NR_NO) then
  1613. begin
  1614. if (ref.index <> NR_NO) and
  1615. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1616. begin
  1617. result := true;
  1618. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1619. list.concat(taicpu.op_reg_reg_reg(
  1620. A_ADD,tmpreg,ref.base,ref.index));
  1621. ref.index := NR_NO;
  1622. ref.base := tmpreg;
  1623. end
  1624. end
  1625. else
  1626. if ref.index <> NR_NO then
  1627. internalerror(200208102);
  1628. end;
  1629. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1630. { that's the case, we can use rlwinm to do an AND operation }
  1631. function tcgppc.get_rlwi_const(a: aint; var l1, l2: longint): boolean;
  1632. var
  1633. temp : longint;
  1634. testbit : aint;
  1635. compare: boolean;
  1636. begin
  1637. get_rlwi_const := false;
  1638. if (a = 0) or (a = -1) then
  1639. exit;
  1640. { start with the lowest bit }
  1641. testbit := 1;
  1642. { check its value }
  1643. compare := boolean(a and testbit);
  1644. { find out how long the run of bits with this value is }
  1645. { (it's impossible that all bits are 1 or 0, because in that case }
  1646. { this function wouldn't have been called) }
  1647. l1 := 31;
  1648. while (((a and testbit) <> 0) = compare) do
  1649. begin
  1650. testbit := testbit shl 1;
  1651. dec(l1);
  1652. end;
  1653. { check the length of the run of bits that comes next }
  1654. compare := not compare;
  1655. l2 := l1;
  1656. while (((a and testbit) <> 0) = compare) and
  1657. (l2 >= 0) do
  1658. begin
  1659. testbit := testbit shl 1;
  1660. dec(l2);
  1661. end;
  1662. { and finally the check whether the rest of the bits all have the }
  1663. { same value }
  1664. compare := not compare;
  1665. temp := l2;
  1666. if temp >= 0 then
  1667. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1668. exit;
  1669. { we have done "not(not(compare))", so compare is back to its }
  1670. { initial value. If the lowest bit was 0, a is of the form }
  1671. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1672. { because l2 now contains the position of the last zero of the }
  1673. { first run instead of that of the first 1) so switch l1 and l2 }
  1674. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1675. if not compare then
  1676. begin
  1677. temp := l1;
  1678. l1 := l2+1;
  1679. l2 := temp;
  1680. end
  1681. else
  1682. { otherwise, l1 currently contains the position of the last }
  1683. { zero instead of that of the first 1 of the second run -> +1 }
  1684. inc(l1);
  1685. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1686. l1 := l1 and 31;
  1687. l2 := l2 and 31;
  1688. get_rlwi_const := true;
  1689. end;
  1690. procedure tcgppc.a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  1691. ref: treference);
  1692. var
  1693. tmpreg: tregister;
  1694. tmpref: treference;
  1695. largeOffset: Boolean;
  1696. begin
  1697. tmpreg := NR_NO;
  1698. if target_info.system = system_powerpc_macos then
  1699. begin
  1700. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1701. high(smallint)-low(smallint));
  1702. if assigned(ref.symbol) then
  1703. begin {Load symbol's value}
  1704. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1705. reference_reset(tmpref);
  1706. tmpref.symbol := ref.symbol;
  1707. tmpref.base := NR_RTOC;
  1708. if macos_direct_globals then
  1709. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1710. else
  1711. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1712. end;
  1713. if largeOffset then
  1714. begin {Add hi part of offset}
  1715. reference_reset(tmpref);
  1716. if Smallint(Lo(ref.offset)) < 0 then
  1717. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  1718. else
  1719. tmpref.offset := Hi(ref.offset);
  1720. if (tmpreg <> NR_NO) then
  1721. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  1722. else
  1723. begin
  1724. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1725. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1726. end;
  1727. end;
  1728. if (tmpreg <> NR_NO) then
  1729. begin
  1730. {Add content of base register}
  1731. if ref.base <> NR_NO then
  1732. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1733. ref.base,tmpreg));
  1734. {Make ref ready to be used by op}
  1735. ref.symbol:= nil;
  1736. ref.base:= tmpreg;
  1737. if largeOffset then
  1738. ref.offset := Smallint(Lo(ref.offset));
  1739. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1740. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1741. end
  1742. else
  1743. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1744. end
  1745. else {if target_info.system <> system_powerpc_macos}
  1746. begin
  1747. if assigned(ref.symbol) or
  1748. (cardinal(ref.offset-low(smallint)) >
  1749. high(smallint)-low(smallint)) then
  1750. begin
  1751. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1752. reference_reset(tmpref);
  1753. tmpref.symbol := ref.symbol;
  1754. tmpref.relsymbol := ref.relsymbol;
  1755. tmpref.offset := ref.offset;
  1756. tmpref.refaddr := addr_hi;
  1757. if ref.base <> NR_NO then
  1758. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1759. ref.base,tmpref))
  1760. else
  1761. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1762. ref.base := tmpreg;
  1763. ref.refaddr := addr_lo;
  1764. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1765. end
  1766. else
  1767. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1768. end;
  1769. end;
  1770. procedure tcg64fppc.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1771. begin
  1772. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1773. end;
  1774. procedure tcg64fppc.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1775. begin
  1776. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1777. end;
  1778. procedure tcg64fppc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1779. begin
  1780. case op of
  1781. OP_AND,OP_OR,OP_XOR:
  1782. begin
  1783. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1784. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1785. end;
  1786. OP_ADD:
  1787. begin
  1788. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  1789. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1790. end;
  1791. OP_SUB:
  1792. begin
  1793. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  1794. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1795. end;
  1796. else
  1797. internalerror(2002072801);
  1798. end;
  1799. end;
  1800. procedure tcg64fppc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1801. const
  1802. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  1803. (A_SUBIC,A_SUBC,A_ADDME));
  1804. var
  1805. tmpreg: tregister;
  1806. tmpreg64: tregister64;
  1807. issub: boolean;
  1808. begin
  1809. case op of
  1810. OP_AND,OP_OR,OP_XOR:
  1811. begin
  1812. cg.a_op_const_reg_reg(list,op,OS_32,aint(value),regsrc.reglo,regdst.reglo);
  1813. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1814. regdst.reghi);
  1815. end;
  1816. OP_ADD, OP_SUB:
  1817. begin
  1818. if (value < 0) and
  1819. (value <> low(value)) then
  1820. begin
  1821. if op = OP_ADD then
  1822. op := OP_SUB
  1823. else
  1824. op := OP_ADD;
  1825. value := -value;
  1826. end;
  1827. if (longint(value) <> 0) then
  1828. begin
  1829. issub := op = OP_SUB;
  1830. if (value > 0) and
  1831. (value-ord(issub) <= 32767) then
  1832. begin
  1833. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  1834. regdst.reglo,regsrc.reglo,longint(value)));
  1835. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1836. regdst.reghi,regsrc.reghi));
  1837. end
  1838. else if ((value shr 32) = 0) then
  1839. begin
  1840. tmpreg := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1841. cg.a_load_const_reg(list,OS_32,aint(value),tmpreg);
  1842. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  1843. regdst.reglo,regsrc.reglo,tmpreg));
  1844. list.concat(taicpu.op_reg_reg(ops[issub,3],
  1845. regdst.reghi,regsrc.reghi));
  1846. end
  1847. else
  1848. begin
  1849. tmpreg64.reglo := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1850. tmpreg64.reghi := tcgppc(cg).rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  1851. a_load64_const_reg(list,value,tmpreg64);
  1852. a_op64_reg_reg_reg(list,op,size,tmpreg64,regsrc,regdst);
  1853. end
  1854. end
  1855. else
  1856. begin
  1857. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  1858. cg.a_op_const_reg_reg(list,op,OS_32,aint(value shr 32),regsrc.reghi,
  1859. regdst.reghi);
  1860. end;
  1861. end;
  1862. else
  1863. internalerror(2002072802);
  1864. end;
  1865. end;
  1866. begin
  1867. cg := tcgppc.create;
  1868. cg64 :=tcg64fppc.create;
  1869. end.