cgcpu.pas 95 KB

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