cgcpu.pas 91 KB

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