cgcpu.pas 90 KB

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