cgcpu.pas 89 KB

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