cgcpu.pas 91 KB

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