cgcpu.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. cgbase,cgobj,
  24. aasmbase,aasmcpu,aasmtai,
  25. cpubase,cpuinfo,node,cg64f32,rgcpu;
  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 : aword;const locpara : tparalocation);override;
  36. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  37. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);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: AWord; 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: aword; 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 : aword;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 : aword;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_always(list : taasmoutput;l: tasmlabel); override;
  60. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  61. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  62. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);override;
  63. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  64. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  65. procedure g_restore_frame_pointer(list : taasmoutput);override;
  66. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  67. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  68. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  69. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  70. { that's the case, we can use rlwinm to do an AND operation }
  71. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  72. procedure g_save_standard_registers(list:Taasmoutput);override;
  73. procedure g_restore_standard_registers(list:Taasmoutput);override;
  74. procedure g_save_all_registers(list : taasmoutput);override;
  75. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  76. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  77. private
  78. (* NOT IN USE: *)
  79. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  80. (* NOT IN USE: *)
  81. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  82. { Make sure ref is a valid reference for the PowerPC and sets the }
  83. { base to the value of the index if (base = R_NO). }
  84. { Returns true if the reference contained a base, index and an }
  85. { offset or symbol, in which case the base will have been changed }
  86. { to a tempreg (which has to be freed by the caller) containing }
  87. { the sum of part of the original reference }
  88. function fixref(list: taasmoutput; var ref: treference): boolean;
  89. { returns whether a reference can be used immediately in a powerpc }
  90. { instruction }
  91. function issimpleref(const ref: treference): boolean;
  92. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  93. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  94. ref: treference);
  95. { creates the correct branch instruction for a given combination }
  96. { of asmcondflags and destination addressing mode }
  97. procedure a_jmp(list: taasmoutput; op: tasmop;
  98. c: tasmcondflag; crval: longint; l: tasmlabel);
  99. end;
  100. tcg64fppc = class(tcg64f32)
  101. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  102. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  103. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  104. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  105. end;
  106. const
  107. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  108. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  109. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  110. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  111. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  112. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  113. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  114. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  115. implementation
  116. uses
  117. globtype,globals,verbose,systems,cutils,
  118. symconst,symdef,symsym,
  119. rgobj,tgobj,cpupi,procinfo;
  120. procedure tcgppc.init_register_allocators;
  121. begin
  122. rgfpu:=trgcpu.create(29,R_INTREGISTER,R_SUBWHOLE,chr(ord(RS_R3))+chr(ord(RS_R4))+chr(ord(RS_R5))+chr(ord(RS_R6))+chr(ord(RS_R7))+chr(ord(RS_R8))+
  123. chr(ord(RS_R9))+chr(ord(RS_R10))+chr(ord(RS_R11))+chr(ord(RS_R12))+chr(ord(RS_R31))+chr(ord(RS_R30))+chr(ord(RS_R29))+
  124. chr(ord(RS_R28))+chr(ord(RS_R27))+chr(ord(RS_R26))+chr(ord(RS_R25))+chr(ord(RS_R24))+chr(ord(RS_R23))+chr(ord(RS_R22))+
  125. chr(ord(RS_R21))+chr(ord(RS_R20))+chr(ord(RS_R19))+chr(ord(RS_R18))+chr(ord(RS_R17))+chr(ord(RS_R16))+chr(ord(RS_R15))+
  126. chr(ord(RS_R14))+chr(ord(RS_R13)),first_int_imreg,[]);
  127. {$warning FIX ME}
  128. rgfpu:=trgcpu.create(6,R_INTREGISTER,R_SUBWHOLE,#0#1#2#3#4#5,first_fpu_imreg,[]);
  129. rgmm:=trgcpu.create(0,R_MMXREGISTER,R_SUBNONE,'',first_mm_imreg,[]);
  130. end;
  131. procedure tcgppc.done_register_allocators;
  132. begin
  133. rgint.free;
  134. rgmm.free;
  135. rgfpu.free;
  136. end;
  137. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  138. var
  139. ref: treference;
  140. begin
  141. case locpara.loc of
  142. LOC_REGISTER,LOC_CREGISTER:
  143. a_load_const_reg(list,size,a,locpara.register);
  144. LOC_REFERENCE:
  145. begin
  146. reference_reset(ref);
  147. ref.base:=locpara.reference.index;
  148. ref.offset:=locpara.reference.offset;
  149. a_load_const_ref(list,size,a,ref);
  150. end;
  151. else
  152. internalerror(2002081101);
  153. end;
  154. end;
  155. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  156. var
  157. ref: treference;
  158. tmpreg: tregister;
  159. begin
  160. case locpara.loc of
  161. LOC_REGISTER,LOC_CREGISTER:
  162. a_load_ref_reg(list,size,size,r,locpara.register);
  163. LOC_REFERENCE:
  164. begin
  165. reference_reset(ref);
  166. ref.base:=locpara.reference.index;
  167. ref.offset:=locpara.reference.offset;
  168. tmpreg := rgint.getregister(list,R_SUBWHOLE);
  169. a_load_ref_reg(list,size,size,r,tmpreg);
  170. a_load_reg_ref(list,size,size,tmpreg,ref);
  171. rgint.ungetregister(list,tmpreg);
  172. end;
  173. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  174. case size of
  175. OS_F32, OS_F64:
  176. a_loadfpu_ref_reg(list,size,r,locpara.register);
  177. else
  178. internalerror(2002072801);
  179. end;
  180. else
  181. internalerror(2002081103);
  182. end;
  183. end;
  184. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  185. var
  186. ref: treference;
  187. tmpreg: tregister;
  188. begin
  189. case locpara.loc of
  190. LOC_REGISTER,LOC_CREGISTER:
  191. a_loadaddr_ref_reg(list,r,locpara.register);
  192. LOC_REFERENCE:
  193. begin
  194. reference_reset(ref);
  195. ref.base := locpara.reference.index;
  196. ref.offset := locpara.reference.offset;
  197. tmpreg := rgint.getregister(list,R_SUBWHOLE);
  198. a_loadaddr_ref_reg(list,r,tmpreg);
  199. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  200. rgint.ungetregister(list,tmpreg);
  201. end;
  202. else
  203. internalerror(2002080701);
  204. end;
  205. end;
  206. { calling a procedure by name }
  207. procedure tcgppc.a_call_name(list : taasmoutput;const s : string);
  208. var
  209. href : treference;
  210. begin
  211. { MacOS: The linker on MacOS (PPCLink) inserts a call to glue code,
  212. if it is a cross-TOC call. If so, it also replaces the NOP
  213. with some restore code.}
  214. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  215. if target_info.system=system_powerpc_macos then
  216. list.concat(taicpu.op_none(A_NOP));
  217. if not(pi_do_call in current_procinfo.flags) then
  218. internalerror(2003060703);
  219. end;
  220. { calling a procedure by address }
  221. procedure tcgppc.a_call_reg(list : taasmoutput;reg: tregister);
  222. var
  223. tmpreg : tregister;
  224. tmpref : treference;
  225. begin
  226. if target_info.system=system_powerpc_macos then
  227. begin
  228. {Generate instruction to load the procedure address from
  229. the transition vector.}
  230. //TODO: Support cross-TOC calls.
  231. tmpreg := rgint.getregister(list,R_SUBWHOLE);
  232. reference_reset(tmpref);
  233. tmpref.offset := 0;
  234. //tmpref.symaddr := refs_full;
  235. tmpref.base:= reg;
  236. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  237. list.concat(taicpu.op_reg(A_MTCTR,tmpreg));
  238. rgint.ungetregister(list,tmpreg);
  239. end
  240. else
  241. list.concat(taicpu.op_reg(A_MTCTR,reg));
  242. list.concat(taicpu.op_none(A_BCTRL));
  243. //if target_info.system=system_powerpc_macos then
  244. // //NOP is not needed here.
  245. // list.concat(taicpu.op_none(A_NOP));
  246. if not(pi_do_call in current_procinfo.flags) then
  247. internalerror(2003060704);
  248. //list.concat(tai_comment.create(strpnew('***** a_call_reg')));
  249. end;
  250. {********************** load instructions ********************}
  251. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  252. begin
  253. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  254. internalerror(2002090902);
  255. if (longint(a) >= low(smallint)) and
  256. (longint(a) <= high(smallint)) then
  257. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a)))
  258. else if ((a and $ffff) <> 0) then
  259. begin
  260. list.concat(taicpu.op_reg_const(A_LI,reg,smallint(a and $ffff)));
  261. if ((a shr 16) <> 0) or
  262. (smallint(a and $ffff) < 0) then
  263. list.concat(taicpu.op_reg_reg_const(A_ADDIS,reg,reg,
  264. smallint((a shr 16)+ord(smallint(a and $ffff) < 0))))
  265. end
  266. else
  267. list.concat(taicpu.op_reg_const(A_LIS,reg,smallint(a shr 16)));
  268. end;
  269. procedure tcgppc.a_load_reg_ref(list : taasmoutput; fromsize, tosize: TCGSize; reg : tregister;const ref : treference);
  270. const
  271. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  272. { indexed? updating?}
  273. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  274. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  275. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  276. var
  277. op: TAsmOp;
  278. ref2: TReference;
  279. freereg: boolean;
  280. begin
  281. ref2 := ref;
  282. freereg := fixref(list,ref2);
  283. if tosize in [OS_S8..OS_S16] then
  284. { storing is the same for signed and unsigned values }
  285. tosize := tcgsize(ord(tosize)-(ord(OS_S8)-ord(OS_8)));
  286. { 64 bit stuff should be handled separately }
  287. if tosize in [OS_64,OS_S64] then
  288. internalerror(200109236);
  289. op := storeinstr[tcgsize2unsigned[tosize],ref2.index<>NR_NO,false];
  290. a_load_store(list,op,reg,ref2);
  291. if freereg then
  292. rgint.ungetregister(list,ref2.base);
  293. End;
  294. procedure tcgppc.a_load_ref_reg(list : taasmoutput; fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  295. const
  296. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  297. { indexed? updating?}
  298. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  299. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  300. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  301. { 64bit stuff should be handled separately }
  302. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  303. { there's no load-byte-with-sign-extend :( }
  304. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  305. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  306. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  307. var
  308. op: tasmop;
  309. tmpreg: tregister;
  310. ref2, tmpref: treference;
  311. freereg: boolean;
  312. begin
  313. { TODO: optimize/take into consideration fromsize/tosize. Will }
  314. { probably only matter for OS_S8 loads though }
  315. if not(fromsize in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  316. internalerror(2002090902);
  317. ref2 := ref;
  318. freereg := fixref(list,ref2);
  319. op := loadinstr[fromsize,ref2.index<>NR_NO,false];
  320. a_load_store(list,op,reg,ref2);
  321. if freereg then
  322. rgint.ungetregister(list,ref2.base);
  323. { sign extend shortint if necessary, since there is no }
  324. { load instruction that does that automatically (JM) }
  325. if fromsize = OS_S8 then
  326. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  327. end;
  328. procedure tcgppc.a_load_reg_reg(list : taasmoutput;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  329. var
  330. instr: taicpu;
  331. begin
  332. if (reg1<>reg2) or
  333. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  334. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  335. (tosize <> fromsize) and
  336. not(fromsize in [OS_32,OS_S32])) then
  337. begin
  338. case tosize of
  339. OS_8:
  340. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  341. reg2,reg1,0,31-8+1,31);
  342. OS_S8:
  343. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  344. OS_16:
  345. instr := taicpu.op_reg_reg_const_const_const(A_RLWINM,
  346. reg2,reg1,0,31-16+1,31);
  347. OS_S16:
  348. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  349. OS_32,OS_S32:
  350. instr := taicpu.op_reg_reg(A_MR,reg2,reg1);
  351. else internalerror(2002090901);
  352. end;
  353. list.concat(instr);
  354. rgint.add_move_instruction(instr);
  355. end;
  356. end;
  357. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  358. begin
  359. list.concat(taicpu.op_reg_reg(A_FMR,reg2,reg1));
  360. end;
  361. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  362. const
  363. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  364. { indexed? updating?}
  365. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  366. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  367. var
  368. op: tasmop;
  369. ref2: treference;
  370. freereg: boolean;
  371. begin
  372. { several functions call this procedure with OS_32 or OS_64 }
  373. { so this makes life easier (FK) }
  374. case size of
  375. OS_32,OS_F32:
  376. size:=OS_F32;
  377. OS_64,OS_F64,OS_C64:
  378. size:=OS_F64;
  379. else
  380. internalerror(200201121);
  381. end;
  382. ref2 := ref;
  383. freereg := fixref(list,ref2);
  384. op := fpuloadinstr[size,ref2.index <> NR_NO,false];
  385. a_load_store(list,op,reg,ref2);
  386. if freereg then
  387. rgint.ungetregister(list,ref2.base);
  388. end;
  389. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  390. const
  391. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  392. { indexed? updating?}
  393. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  394. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  395. var
  396. op: tasmop;
  397. ref2: treference;
  398. freereg: boolean;
  399. begin
  400. if not(size in [OS_F32,OS_F64]) then
  401. internalerror(200201122);
  402. ref2 := ref;
  403. freereg := fixref(list,ref2);
  404. op := fpustoreinstr[size,ref2.index <> NR_NO,false];
  405. a_load_store(list,op,reg,ref2);
  406. if freereg then
  407. rgint.ungetregister(list,ref2.base);
  408. end;
  409. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  410. begin
  411. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  412. end;
  413. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  414. begin
  415. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  416. end;
  417. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  418. size: tcgsize; a: aword; src, dst: tregister);
  419. var
  420. l1,l2: longint;
  421. oplo, ophi: tasmop;
  422. scratchreg: tregister;
  423. useReg, gotrlwi: boolean;
  424. procedure do_lo_hi;
  425. begin
  426. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  427. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,word(a shr 16)));
  428. end;
  429. begin
  430. if op = OP_SUB then
  431. begin
  432. {$ifopt q+}
  433. {$q-}
  434. {$define overflowon}
  435. {$endif}
  436. a_op_const_reg_reg(list,OP_ADD,size,aword(-longint(a)),src,dst);
  437. {$ifdef overflowon}
  438. {$q+}
  439. {$undef overflowon}
  440. {$endif}
  441. exit;
  442. end;
  443. ophi := TOpCG2AsmOpConstHi[op];
  444. oplo := TOpCG2AsmOpConstLo[op];
  445. gotrlwi := get_rlwi_const(a,l1,l2);
  446. if (op in [OP_AND,OP_OR,OP_XOR]) then
  447. begin
  448. if (a = 0) then
  449. begin
  450. if op = OP_AND then
  451. list.concat(taicpu.op_reg_const(A_LI,dst,0))
  452. else
  453. a_load_reg_reg(list,size,size,src,dst);
  454. exit;
  455. end
  456. else if (a = high(aword)) then
  457. begin
  458. case op of
  459. OP_OR:
  460. list.concat(taicpu.op_reg_const(A_LI,dst,-1));
  461. OP_XOR:
  462. list.concat(taicpu.op_reg_reg(A_NOT,dst,src));
  463. OP_AND:
  464. a_load_reg_reg(list,size,size,src,dst);
  465. end;
  466. exit;
  467. end
  468. else if (a <= high(word)) and
  469. ((op <> OP_AND) or
  470. not gotrlwi) then
  471. begin
  472. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,word(a)));
  473. exit;
  474. end;
  475. { all basic constant instructions also have a shifted form that }
  476. { works only on the highest 16bits, so if lo(a) is 0, we can }
  477. { use that one }
  478. if (word(a) = 0) and
  479. (not(op = OP_AND) or
  480. not gotrlwi) then
  481. begin
  482. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,word(a shr 16)));
  483. exit;
  484. end;
  485. end
  486. else if (op = OP_ADD) then
  487. if a = 0 then
  488. exit
  489. else if (longint(a) >= low(smallint)) and
  490. (longint(a) <= high(smallint)) then
  491. begin
  492. list.concat(taicpu.op_reg_reg_const(A_ADDI,dst,src,smallint(a)));
  493. exit;
  494. end;
  495. { otherwise, the instructions we can generate depend on the }
  496. { operation }
  497. useReg := false;
  498. case op of
  499. OP_DIV,OP_IDIV:
  500. if (a = 0) then
  501. internalerror(200208103)
  502. else if (a = 1) then
  503. begin
  504. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  505. exit
  506. end
  507. else if ispowerof2(a,l1) then
  508. begin
  509. case op of
  510. OP_DIV:
  511. list.concat(taicpu.op_reg_reg_const(A_SRWI,dst,src,l1));
  512. OP_IDIV:
  513. begin
  514. list.concat(taicpu.op_reg_reg_const(A_SRAWI,dst,src,l1));
  515. list.concat(taicpu.op_reg_reg(A_ADDZE,dst,dst));
  516. end;
  517. end;
  518. exit;
  519. end
  520. else
  521. usereg := true;
  522. OP_IMUL, OP_MUL:
  523. if (a = 0) then
  524. begin
  525. list.concat(taicpu.op_reg_const(A_LI,dst,0));
  526. exit
  527. end
  528. else if (a = 1) then
  529. begin
  530. a_load_reg_reg(list,OS_INT,OS_INT,src,dst);
  531. exit
  532. end
  533. else if ispowerof2(a,l1) then
  534. list.concat(taicpu.op_reg_reg_const(A_SLWI,dst,src,l1))
  535. else if (longint(a) >= low(smallint)) and
  536. (longint(a) <= high(smallint)) then
  537. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,smallint(a)))
  538. else
  539. usereg := true;
  540. OP_ADD:
  541. begin
  542. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,smallint(a)));
  543. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  544. smallint((a shr 16) + ord(smallint(a) < 0))));
  545. end;
  546. OP_OR:
  547. { try to use rlwimi }
  548. if gotrlwi and
  549. (src = dst) then
  550. begin
  551. scratchreg := rgint.getregister(list,R_SUBWHOLE);
  552. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  553. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  554. scratchreg,0,l1,l2));
  555. rgint.ungetregister(list,scratchreg);
  556. end
  557. else
  558. do_lo_hi;
  559. OP_AND:
  560. { try to use rlwinm }
  561. if gotrlwi then
  562. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  563. src,0,l1,l2))
  564. else
  565. useReg := true;
  566. OP_XOR:
  567. do_lo_hi;
  568. OP_SHL,OP_SHR,OP_SAR:
  569. begin
  570. if (a and 31) <> 0 Then
  571. list.concat(taicpu.op_reg_reg_const(
  572. TOpCG2AsmOpConstLo[Op],dst,src,a and 31))
  573. else
  574. a_load_reg_reg(list,size,size,src,dst);
  575. if (a shr 5) <> 0 then
  576. internalError(68991);
  577. end
  578. else
  579. internalerror(200109091);
  580. end;
  581. { if all else failed, load the constant in a register and then }
  582. { perform the operation }
  583. if useReg then
  584. begin
  585. scratchreg := rgint.getregister(list,R_SUBWHOLE);
  586. a_load_const_reg(list,OS_32,a,scratchreg);
  587. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  588. rgint.ungetregister(list,scratchreg);
  589. end;
  590. end;
  591. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  592. size: tcgsize; src1, src2, dst: tregister);
  593. const
  594. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  595. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  596. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  597. begin
  598. case op of
  599. OP_NEG,OP_NOT:
  600. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  601. else
  602. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  603. end;
  604. end;
  605. {*************** compare instructructions ****************}
  606. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  607. l : tasmlabel);
  608. var
  609. p: taicpu;
  610. scratch_register: TRegister;
  611. signed: boolean;
  612. begin
  613. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  614. { in the following case, we generate more efficient code when }
  615. { signed is true }
  616. if (cmp_op in [OC_EQ,OC_NE]) and
  617. (a > $ffff) then
  618. signed := true;
  619. if signed then
  620. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  621. list.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR0,reg,longint(a)))
  622. else
  623. begin
  624. scratch_register := rgint.getregister(list,R_SUBWHOLE);
  625. a_load_const_reg(list,OS_32,a,scratch_register);
  626. list.concat(taicpu.op_reg_reg_reg(A_CMPW,NR_CR0,reg,scratch_register));
  627. rgint.ungetregister(list,scratch_register);
  628. end
  629. else
  630. if (a <= $ffff) then
  631. list.concat(taicpu.op_reg_reg_const(A_CMPLWI,NR_CR0,reg,a))
  632. else
  633. begin
  634. scratch_register := rgint.getregister(list,R_SUBWHOLE);
  635. a_load_const_reg(list,OS_32,a,scratch_register);
  636. list.concat(taicpu.op_reg_reg_reg(A_CMPLW,NR_CR0,reg,scratch_register));
  637. rgint.ungetregister(list,scratch_register);
  638. end;
  639. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  640. end;
  641. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  642. reg1,reg2 : tregister;l : tasmlabel);
  643. var
  644. p: taicpu;
  645. op: tasmop;
  646. begin
  647. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  648. op := A_CMPW
  649. else
  650. op := A_CMPLW;
  651. list.concat(taicpu.op_reg_reg_reg(op,NR_CR0,reg2,reg1));
  652. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  653. end;
  654. procedure tcgppc.g_save_standard_registers(list:Taasmoutput);
  655. begin
  656. {$warning FIX ME}
  657. end;
  658. procedure tcgppc.g_restore_standard_registers(list:Taasmoutput);
  659. begin
  660. {$warning FIX ME}
  661. end;
  662. procedure tcgppc.g_save_all_registers(list : taasmoutput);
  663. begin
  664. {$warning FIX ME}
  665. end;
  666. procedure tcgppc.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  667. begin
  668. {$warning FIX ME}
  669. end;
  670. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  671. begin
  672. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  673. end;
  674. procedure tcgppc.a_jmp_always(list : taasmoutput;l: tasmlabel);
  675. begin
  676. a_jmp(list,A_B,C_None,0,l);
  677. end;
  678. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  679. var
  680. c: tasmcond;
  681. begin
  682. c := flags_to_cond(f);
  683. a_jmp(list,A_BC,c.cond,c.cr-RS_CR0,l);
  684. end;
  685. procedure tcgppc.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  686. var
  687. testbit: byte;
  688. bitvalue: boolean;
  689. begin
  690. { get the bit to extract from the conditional register + its }
  691. { requested value (0 or 1) }
  692. testbit := ((f.cr-RS_CR0) * 4);
  693. case f.flag of
  694. F_EQ,F_NE:
  695. begin
  696. inc(testbit,2);
  697. bitvalue := f.flag = F_EQ;
  698. end;
  699. F_LT,F_GE:
  700. begin
  701. bitvalue := f.flag = F_LT;
  702. end;
  703. F_GT,F_LE:
  704. begin
  705. inc(testbit);
  706. bitvalue := f.flag = F_GT;
  707. end;
  708. else
  709. internalerror(200112261);
  710. end;
  711. { load the conditional register in the destination reg }
  712. list.concat(taicpu.op_reg(A_MFCR,reg));
  713. { we will move the bit that has to be tested to bit 0 by rotating }
  714. { left }
  715. testbit := (testbit + 1) and 31;
  716. { extract bit }
  717. list.concat(taicpu.op_reg_reg_const_const_const(
  718. A_RLWINM,reg,reg,testbit,31,31));
  719. { if we need the inverse, xor with 1 }
  720. if not bitvalue then
  721. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  722. end;
  723. (*
  724. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  725. var
  726. testbit: byte;
  727. bitvalue: boolean;
  728. begin
  729. { get the bit to extract from the conditional register + its }
  730. { requested value (0 or 1) }
  731. case f.simple of
  732. false:
  733. begin
  734. { we don't generate this in the compiler }
  735. internalerror(200109062);
  736. end;
  737. true:
  738. case f.cond of
  739. C_None:
  740. internalerror(200109063);
  741. C_LT..C_NU:
  742. begin
  743. testbit := (ord(f.cr) - ord(R_CR0))*4;
  744. inc(testbit,AsmCondFlag2BI[f.cond]);
  745. bitvalue := AsmCondFlagTF[f.cond];
  746. end;
  747. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  748. begin
  749. testbit := f.crbit
  750. bitvalue := AsmCondFlagTF[f.cond];
  751. end;
  752. else
  753. internalerror(200109064);
  754. end;
  755. end;
  756. { load the conditional register in the destination reg }
  757. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  758. { we will move the bit that has to be tested to bit 31 -> rotate }
  759. { left by bitpos+1 (remember, this is big-endian!) }
  760. if bitpos <> 31 then
  761. inc(bitpos)
  762. else
  763. bitpos := 0;
  764. { extract bit }
  765. list.concat(taicpu.op_reg_reg_const_const_const(
  766. A_RLWINM,reg,reg,bitpos,31,31));
  767. { if we need the inverse, xor with 1 }
  768. if not bitvalue then
  769. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  770. end;
  771. *)
  772. { *********** entry/exit code and address loading ************ }
  773. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  774. { generated the entry code of a procedure/function. Note: localsize is the }
  775. { sum of the size necessary for local variables and the maximum possible }
  776. { combined size of ALL the parameters of a procedure called by the current }
  777. { one. }
  778. { This procedure may be called before, as well as after
  779. g_return_from_proc is called.}
  780. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  781. href,href2 : treference;
  782. usesfpr,usesgpr,gotgot : boolean;
  783. parastart : aword;
  784. offset : aword;
  785. // r,r2,rsp:Tregister;
  786. regcounter2: Tsuperregister;
  787. regidx : tregisterindex;
  788. hp: tparaitem;
  789. begin
  790. { CR and LR only have to be saved in case they are modified by the current }
  791. { procedure, but currently this isn't checked, so save them always }
  792. { following is the entry code as described in "Altivec Programming }
  793. { Interface Manual", bar the saving of AltiVec registers }
  794. a_reg_alloc(list,NR_STACK_POINTER_REG);
  795. a_reg_alloc(list,NR_R0);
  796. if current_procinfo.procdef.parast.symtablelevel>1 then
  797. a_reg_alloc(list,NR_R11);
  798. usesfpr:=false;
  799. if not (po_assembler in current_procinfo.procdef.procoptions) then
  800. {$warning FIXME!!}
  801. { FIXME: has to be R_F8 instad of R_F14 for SYSV abi }
  802. for regcounter:=RS_F14 to RS_F31 do
  803. begin
  804. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  805. if regidx in rgfpu.used_in_proc then
  806. begin
  807. usesfpr:= true;
  808. firstregfpu:=regcounter;
  809. break;
  810. end;
  811. end;
  812. usesgpr:=false;
  813. if not (po_assembler in current_procinfo.procdef.procoptions) then
  814. for regcounter2:=firstsaveintreg to RS_R31 do
  815. begin
  816. if regcounter2 in rgint.used_in_proc then
  817. begin
  818. usesgpr:=true;
  819. firstreggpr:=regcounter2;
  820. break;
  821. end;
  822. end;
  823. { save link register? }
  824. if not (po_assembler in current_procinfo.procdef.procoptions) then
  825. if (pi_do_call in current_procinfo.flags) then
  826. begin
  827. { save return address... }
  828. list.concat(taicpu.op_reg(A_MFLR,NR_R0));
  829. { ... in caller's frame }
  830. case target_info.abi of
  831. abi_powerpc_aix:
  832. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  833. abi_powerpc_sysv:
  834. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  835. end;
  836. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  837. a_reg_dealloc(list,NR_R0);
  838. end;
  839. { save the CR if necessary in callers frame. }
  840. if not (po_assembler in current_procinfo.procdef.procoptions) then
  841. if target_info.abi = abi_powerpc_aix then
  842. if false then { Not needed at the moment. }
  843. begin
  844. a_reg_alloc(list,NR_R0);
  845. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  846. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  847. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  848. a_reg_dealloc(list,NR_R0);
  849. end;
  850. { !!! always allocate space for all registers for now !!! }
  851. if not (po_assembler in current_procinfo.procdef.procoptions) then
  852. { if usesfpr or usesgpr then }
  853. begin
  854. a_reg_alloc(list,NR_R12);
  855. { save end of fpr save area }
  856. list.concat(taicpu.op_reg_reg(A_MR,NR_R12,NR_STACK_POINTER_REG));
  857. end;
  858. if (localsize <> 0) then
  859. begin
  860. if (localsize <= high(smallint)) then
  861. begin
  862. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  863. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  864. end
  865. else
  866. begin
  867. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  868. { can't use getregisterint here, the register colouring }
  869. { is already done when we get here }
  870. href.index := NR_R11;
  871. a_reg_alloc(list,href.index);
  872. a_load_const_reg(list,OS_S32,-localsize,href.index);
  873. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  874. a_reg_dealloc(list,href.index);
  875. end;
  876. end;
  877. { no GOT pointer loaded yet }
  878. gotgot:=false;
  879. if usesfpr then
  880. begin
  881. { save floating-point registers
  882. if (cs_create_pic in aktmoduleswitches) and not(usesgpr) then
  883. begin
  884. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+'_g');
  885. gotgot:=true;
  886. end
  887. else
  888. a_call_name(objectlibrary.newasmsymbol('_savefpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14));
  889. }
  890. reference_reset_base(href,NR_R12,-8);
  891. for regcounter:=firstregfpu to RS_F31 do
  892. begin
  893. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  894. if regidx in rgfpu.used_in_proc then
  895. begin
  896. a_loadfpu_reg_ref(list,OS_F64,newreg(R_FPUREGISTER,regcounter,R_SUBNONE),href);
  897. dec(href.offset,8);
  898. end;
  899. end;
  900. { compute end of gpr save area }
  901. a_op_const_reg(list,OP_ADD,OS_ADDR,aword(href.offset+8),NR_R12);
  902. end;
  903. { save gprs and fetch GOT pointer }
  904. if usesgpr then
  905. begin
  906. {
  907. if cs_create_pic in aktmoduleswitches then
  908. begin
  909. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14)+'_g');
  910. gotgot:=true;
  911. end
  912. else
  913. a_call_name(objectlibrary.newasmsymbol('_savegpr_'+tostr(ord(firstreggpr)-ord(R_14)+14))
  914. }
  915. reference_reset_base(href,NR_R12,-4);
  916. for regcounter2:=firstsaveintreg to RS_R31 do
  917. begin
  918. if regcounter2 in rgint.used_in_proc then
  919. begin
  920. usesgpr:=true;
  921. a_load_reg_ref(list,OS_INT,OS_INT,newreg(R_INTREGISTER,regcounter2,R_SUBNONE),href);
  922. dec(href.offset,4);
  923. end;
  924. end;
  925. {
  926. r.enum:=R_INTREGISTER;
  927. r.:=;
  928. reference_reset_base(href,NR_R12,-((NR_R31-firstreggpr) shr 8+1)*4);
  929. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  930. }
  931. end;
  932. if assigned(current_procinfo.procdef.parast) then
  933. begin
  934. if not (po_assembler in current_procinfo.procdef.procoptions) then
  935. begin
  936. { copy memory parameters to local parast }
  937. hp:=tparaitem(current_procinfo.procdef.para.first);
  938. while assigned(hp) do
  939. begin
  940. if (hp.paraloc[calleeside].loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  941. begin
  942. if tvarsym(hp.parasym).localloc.loc<>LOC_REFERENCE then
  943. internalerror(200310011);
  944. reference_reset_base(href,tvarsym(hp.parasym).localloc.reference.index,tvarsym(hp.parasym).localloc.reference.offset);
  945. reference_reset_base(href2,NR_R12,hp.paraloc[callerside].reference.offset);
  946. cg.a_load_ref_ref(list,hp.paraloc[calleeside].size,hp.paraloc[calleeside].size,href2,href);
  947. end
  948. {$ifdef dummy}
  949. else if (hp.calleeparaloc.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  950. begin
  951. rg.getexplicitregisterint(list,hp.calleeparaloc.register);
  952. end
  953. {$endif dummy}
  954. ;
  955. hp := tparaitem(hp.next);
  956. end;
  957. end;
  958. end;
  959. if usesfpr or usesgpr then
  960. a_reg_dealloc(list,NR_R12);
  961. { PIC code support, }
  962. if cs_create_pic in aktmoduleswitches then
  963. begin
  964. { if we didn't get the GOT pointer till now, we've to calculate it now }
  965. if not(gotgot) then
  966. begin
  967. {!!!!!!!!!!!!!}
  968. end;
  969. a_reg_alloc(list,NR_R31);
  970. { place GOT ptr in r31 }
  971. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R31,NR_LR));
  972. end;
  973. { save the CR if necessary ( !!! always done currently ) }
  974. { still need to find out where this has to be done for SystemV
  975. a_reg_alloc(list,R_0);
  976. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  977. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  978. new_reference(STACK_POINTER_REG,LA_CR)));
  979. a_reg_dealloc(list,R_0); }
  980. { now comes the AltiVec context save, not yet implemented !!! }
  981. { if we're in a nested procedure, we've to save R11 }
  982. if current_procinfo.procdef.parast.symtablelevel>2 then
  983. begin
  984. reference_reset_base(href,NR_STACK_POINTER_REG,PARENT_FRAMEPOINTER_OFFSET);
  985. list.concat(taicpu.op_reg_ref(A_STW,NR_R11,href));
  986. end;
  987. end;
  988. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  989. { This procedure may be called before, as well as after
  990. g_stackframe_entry is called.}
  991. var
  992. regcounter,firstregfpu,firstreggpr: TsuperRegister;
  993. href : treference;
  994. usesfpr,usesgpr,genret : boolean;
  995. regcounter2:Tsuperregister;
  996. localsize: aword;
  997. regidx : tregisterindex;
  998. begin
  999. { AltiVec context restore, not yet implemented !!! }
  1000. usesfpr:=false;
  1001. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1002. for regcounter:=RS_F14 to RS_F31 do
  1003. begin
  1004. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1005. if regidx in rgfpu.used_in_proc then
  1006. begin
  1007. usesfpr:=true;
  1008. firstregfpu:=regcounter;
  1009. break;
  1010. end;
  1011. end;
  1012. usesgpr:=false;
  1013. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1014. for regcounter2:=firstsaveintreg to RS_R31 do
  1015. begin
  1016. if regcounter2 in rgint.used_in_proc then
  1017. begin
  1018. usesgpr:=true;
  1019. firstreggpr:=regcounter2;
  1020. break;
  1021. end;
  1022. end;
  1023. localsize:= tppcprocinfo(current_procinfo).calc_stackframe_size;
  1024. { no return (blr) generated yet }
  1025. genret:=true;
  1026. if usesgpr or usesfpr then
  1027. begin
  1028. { address of gpr save area to r11 }
  1029. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,localsize,NR_STACK_POINTER_REG,NR_R12);
  1030. if usesfpr then
  1031. begin
  1032. reference_reset_base(href,NR_R12,-8);
  1033. for regcounter := firstregfpu to RS_F31 do
  1034. begin
  1035. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1036. if regidx in rgfpu.used_in_proc then
  1037. begin
  1038. a_loadfpu_ref_reg(list,OS_F64,href,newreg(R_FPUREGISTER,regcounter,R_SUBNONE));
  1039. dec(href.offset,8);
  1040. end;
  1041. end;
  1042. inc(href.offset,4);
  1043. end
  1044. else
  1045. reference_reset_base(href,NR_R12,-4);
  1046. for regcounter2:=firstsaveintreg to RS_R31 do
  1047. begin
  1048. if regcounter2 in rgint.used_in_proc then
  1049. begin
  1050. usesgpr:=true;
  1051. a_load_ref_reg(list,OS_INT,OS_INT,href,newreg(R_INTREGISTER,regcounter2,R_SUBNONE));
  1052. dec(href.offset,4);
  1053. end;
  1054. end;
  1055. (*
  1056. reference_reset_base(href,r2,-((NR_R31-ord(firstreggpr)) shr 8+1)*4);
  1057. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1058. *)
  1059. end;
  1060. (*
  1061. { restore fprs and return }
  1062. if usesfpr then
  1063. begin
  1064. { address of fpr save area to r11 }
  1065. r:=NR_R12;
  1066. list.concat(taicpu.op_reg_reg_const(A_ADDI,r,r,(ord(R_F31)-ord(firstregfpu.enum)+1)*8));
  1067. {
  1068. if (pi_do_call in current_procinfo.flags) then
  1069. a_call_name(objectlibrary.newasmsymbol('_restfpr_'+tostr(ord(firstregfpu)-ord(R_F14)+14)+
  1070. '_x')
  1071. else
  1072. { leaf node => lr haven't to be restored }
  1073. a_call_name('_restfpr_'+tostr(ord(firstregfpu.enum)-ord(R_F14)+14)+
  1074. '_l');
  1075. genret:=false;
  1076. }
  1077. end;
  1078. *)
  1079. { if we didn't generate the return code, we've to do it now }
  1080. if genret then
  1081. begin
  1082. { adjust r1 }
  1083. a_op_const_reg(list,OP_ADD,OS_ADDR,localsize,NR_R1);
  1084. { load link register? }
  1085. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1086. begin
  1087. if (pi_do_call in current_procinfo.flags) then
  1088. begin
  1089. case target_info.abi of
  1090. abi_powerpc_aix:
  1091. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_AIX);
  1092. abi_powerpc_sysv:
  1093. reference_reset_base(href,NR_STACK_POINTER_REG,LA_LR_SYSV);
  1094. end;
  1095. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1096. list.concat(taicpu.op_reg(A_MTLR,NR_R0));
  1097. end;
  1098. { restore the CR if necessary from callers frame}
  1099. if target_info.abi = abi_powerpc_aix then
  1100. if false then { Not needed at the moment. }
  1101. begin
  1102. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1103. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1104. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1105. a_reg_dealloc(list,NR_R0);
  1106. end;
  1107. end;
  1108. list.concat(taicpu.op_none(A_BLR));
  1109. end;
  1110. end;
  1111. function save_regs(list : taasmoutput):longint;
  1112. {Generates code which saves used non-volatile registers in
  1113. the save area right below the address the stackpointer point to.
  1114. Returns the actual used save area size.}
  1115. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1116. usesfpr,usesgpr: boolean;
  1117. href : treference;
  1118. offset: integer;
  1119. regcounter2: Tsuperregister;
  1120. regidx : tregisterindex;
  1121. begin
  1122. usesfpr:=false;
  1123. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1124. for regcounter:=RS_F14 to RS_F31 do
  1125. begin
  1126. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1127. if regidx in tcgppc(cg).rgfpu.used_in_proc then
  1128. begin
  1129. usesfpr:=true;
  1130. firstregfpu:=regcounter;
  1131. break;
  1132. end;
  1133. end;
  1134. usesgpr:=false;
  1135. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1136. for regcounter2:=firstsaveintreg to RS_R31 do
  1137. begin
  1138. if regcounter2 in tcgppc(cg).rgint.used_in_proc then
  1139. begin
  1140. usesgpr:=true;
  1141. firstreggpr:=regcounter2;
  1142. break;
  1143. end;
  1144. end;
  1145. offset:= 0;
  1146. { save floating-point registers }
  1147. if usesfpr then
  1148. for regcounter := firstregfpu to RS_F31 do
  1149. begin
  1150. offset:= offset - 8;
  1151. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1152. list.concat(taicpu.op_reg_ref(A_STFD, regcounter, href));
  1153. end;
  1154. (* Optimiztion in the future: a_call_name(list,'_savefXX'); *)
  1155. { save gprs in gpr save area }
  1156. if usesgpr then
  1157. if firstreggpr < RS_R30 then
  1158. begin
  1159. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1160. reference_reset_base(href,NR_STACK_POINTER_REG,offset);
  1161. list.concat(taicpu.op_reg_ref(A_STMW,firstreggpr,href));
  1162. {STMW stores multiple registers}
  1163. end
  1164. else
  1165. begin
  1166. for regcounter := firstreggpr to RS_R31 do
  1167. begin
  1168. offset:= offset - 4;
  1169. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1170. list.concat(taicpu.op_reg_ref(A_STW, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1171. end;
  1172. end;
  1173. { now comes the AltiVec context save, not yet implemented !!! }
  1174. save_regs:= -offset;
  1175. end;
  1176. procedure restore_regs(list : taasmoutput);
  1177. {Generates code which restores used non-volatile registers from
  1178. the save area right below the address the stackpointer point to.}
  1179. var regcounter,firstregfpu,firstreggpr: TSuperRegister;
  1180. usesfpr,usesgpr: boolean;
  1181. href : treference;
  1182. offset: integer;
  1183. regcounter2: Tsuperregister;
  1184. regidx : tregisterindex;
  1185. begin
  1186. usesfpr:=false;
  1187. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1188. for regcounter:=RS_F14 to RS_F31 do
  1189. begin
  1190. regidx:=findreg_by_number(newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE));
  1191. if regidx in tcgppc(cg).rgfpu.used_in_proc then
  1192. begin
  1193. usesfpr:=true;
  1194. firstregfpu:=regcounter;
  1195. break;
  1196. end;
  1197. end;
  1198. usesgpr:=false;
  1199. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1200. for regcounter2:=RS_R13 to RS_R31 do
  1201. begin
  1202. if regcounter2 in tcgppc(cg).rgint.used_in_proc then
  1203. begin
  1204. usesgpr:=true;
  1205. firstreggpr:=regcounter2;
  1206. break;
  1207. end;
  1208. end;
  1209. offset:= 0;
  1210. { restore fp registers }
  1211. if usesfpr then
  1212. for regcounter := firstregfpu to RS_F31 do
  1213. begin
  1214. offset:= offset - 8;
  1215. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1216. list.concat(taicpu.op_reg_ref(A_LFD, newreg(R_FPUREGISTER,regcounter,R_SUBWHOLE), href));
  1217. end;
  1218. (* Optimiztion in the future: a_call_name(list,'_restfXX'); *)
  1219. { restore gprs }
  1220. if usesgpr then
  1221. if firstreggpr < RS_R30 then
  1222. begin
  1223. offset:= offset - 4 * (RS_R31 - firstreggpr + 1);
  1224. reference_reset_base(href,NR_STACK_POINTER_REG,offset); //-220
  1225. list.concat(taicpu.op_reg_ref(A_LMW,firstreggpr,href));
  1226. {LMW loads multiple registers}
  1227. end
  1228. else
  1229. begin
  1230. for regcounter := firstreggpr to RS_R31 do
  1231. begin
  1232. offset:= offset - 4;
  1233. reference_reset_base(href, NR_STACK_POINTER_REG, offset);
  1234. list.concat(taicpu.op_reg_ref(A_LWZ, newreg(R_INTREGISTER,regcounter,R_SUBWHOLE), href));
  1235. end;
  1236. end;
  1237. { now comes the AltiVec context restore, not yet implemented !!! }
  1238. end;
  1239. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  1240. (* NOT IN USE *)
  1241. { generated the entry code of a procedure/function. Note: localsize is the }
  1242. { sum of the size necessary for local variables and the maximum possible }
  1243. { combined size of ALL the parameters of a procedure called by the current }
  1244. { one }
  1245. const
  1246. macosLinkageAreaSize = 24;
  1247. var regcounter: TRegister;
  1248. href : treference;
  1249. registerSaveAreaSize : longint;
  1250. begin
  1251. if (localsize mod 8) <> 0 then
  1252. internalerror(58991);
  1253. { CR and LR only have to be saved in case they are modified by the current }
  1254. { procedure, but currently this isn't checked, so save them always }
  1255. { following is the entry code as described in "Altivec Programming }
  1256. { Interface Manual", bar the saving of AltiVec registers }
  1257. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1258. a_reg_alloc(list,NR_R0);
  1259. { save return address in callers frame}
  1260. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_LR));
  1261. { ... in caller's frame }
  1262. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1263. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1264. a_reg_dealloc(list,NR_R0);
  1265. { save non-volatile registers in callers frame}
  1266. registerSaveAreaSize:= save_regs(list);
  1267. { save the CR if necessary in callers frame ( !!! always done currently ) }
  1268. a_reg_alloc(list,NR_R0);
  1269. list.concat(taicpu.op_reg_reg(A_MFSPR,NR_R0,NR_CR));
  1270. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1271. list.concat(taicpu.op_reg_ref(A_STW,NR_R0,href));
  1272. a_reg_dealloc(list,NR_R0);
  1273. (*
  1274. { save pointer to incoming arguments }
  1275. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  1276. *)
  1277. (*
  1278. a_reg_alloc(list,R_12);
  1279. { 0 or 8 based on SP alignment }
  1280. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  1281. R_12,STACK_POINTER_REG,0,28,28));
  1282. { add in stack length }
  1283. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  1284. -localsize));
  1285. { establish new alignment }
  1286. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  1287. a_reg_dealloc(list,R_12);
  1288. *)
  1289. { allocate stack frame }
  1290. localsize:= align(localsize + macosLinkageAreaSize + registerSaveAreaSize, 16);
  1291. inc(localsize,tg.lasttemp);
  1292. localsize:=align(localsize,16);
  1293. //tppcprocinfo(current_procinfo).localsize:=localsize;
  1294. if (localsize <> 0) then
  1295. begin
  1296. if (localsize <= high(smallint)) then
  1297. begin
  1298. reference_reset_base(href,NR_STACK_POINTER_REG,-localsize);
  1299. a_load_store(list,A_STWU,NR_STACK_POINTER_REG,href);
  1300. end
  1301. else
  1302. begin
  1303. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1304. href.index := NR_R11;
  1305. a_reg_alloc(list,href.index);
  1306. a_load_const_reg(list,OS_S32,-localsize,href.index);
  1307. a_load_store(list,A_STWUX,NR_STACK_POINTER_REG,href);
  1308. a_reg_dealloc(list,href.index);
  1309. end;
  1310. end;
  1311. end;
  1312. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  1313. (* NOT IN USE *)
  1314. var
  1315. href : treference;
  1316. begin
  1317. a_reg_alloc(list,NR_R0);
  1318. { restore stack pointer }
  1319. reference_reset_base(href,NR_STACK_POINTER_REG,LA_SP);
  1320. list.concat(taicpu.op_reg_ref(A_LWZ,NR_STACK_POINTER_REG,href));
  1321. (*
  1322. list.concat(taicpu.op_reg_reg_const(A_ORI,NR_STACK_POINTER_REG,R_31,0));
  1323. *)
  1324. { restore the CR if necessary from callers frame
  1325. ( !!! always done currently ) }
  1326. reference_reset_base(href,NR_STACK_POINTER_REG,LA_CR_AIX);
  1327. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1328. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_CR));
  1329. a_reg_dealloc(list,NR_R0);
  1330. (*
  1331. { restore return address from callers frame }
  1332. reference_reset_base(href,STACK_POINTER_REG,8);
  1333. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  1334. *)
  1335. { restore non-volatile registers from callers frame }
  1336. restore_regs(list);
  1337. (*
  1338. { return to caller }
  1339. list.concat(taicpu.op_reg_reg(A_MTSPR,R_0,R_LR));
  1340. list.concat(taicpu.op_none(A_BLR));
  1341. *)
  1342. { restore return address from callers frame }
  1343. reference_reset_base(href,NR_STACK_POINTER_REG,8);
  1344. list.concat(taicpu.op_reg_ref(A_LWZ,NR_R0,href));
  1345. { return to caller }
  1346. list.concat(taicpu.op_reg_reg(A_MTSPR,NR_R0,NR_LR));
  1347. list.concat(taicpu.op_none(A_BLR));
  1348. end;
  1349. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  1350. begin
  1351. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  1352. end;
  1353. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  1354. var
  1355. ref2, tmpref: treference;
  1356. freereg: boolean;
  1357. tmpreg:Tregister;
  1358. begin
  1359. ref2 := ref;
  1360. freereg := fixref(list,ref2);
  1361. if assigned(ref2.symbol) then
  1362. begin
  1363. if target_info.system = system_powerpc_macos then
  1364. begin
  1365. if macos_direct_globals then
  1366. begin
  1367. reference_reset(tmpref);
  1368. tmpref.offset := ref2.offset;
  1369. tmpref.symbol := ref2.symbol;
  1370. tmpref.base := NR_NO;
  1371. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  1372. end
  1373. else
  1374. begin
  1375. reference_reset(tmpref);
  1376. tmpref.symbol := ref2.symbol;
  1377. tmpref.offset := 0;
  1378. tmpref.base := NR_RTOC;
  1379. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  1380. if ref2.offset <> 0 then
  1381. begin
  1382. reference_reset(tmpref);
  1383. tmpref.offset := ref2.offset;
  1384. tmpref.base:= r;
  1385. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  1386. end;
  1387. end;
  1388. if ref2.base <> NR_NO then
  1389. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  1390. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  1391. end
  1392. else
  1393. begin
  1394. { add the symbol's value to the base of the reference, and if the }
  1395. { reference doesn't have a base, create one }
  1396. reference_reset(tmpref);
  1397. tmpref.offset := ref2.offset;
  1398. tmpref.symbol := ref2.symbol;
  1399. tmpref.symaddr := refs_ha;
  1400. if ref2.base<> NR_NO then
  1401. begin
  1402. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  1403. ref2.base,tmpref));
  1404. if freereg then
  1405. begin
  1406. rgint.ungetregister(list,ref2.base);
  1407. freereg := false;
  1408. end;
  1409. end
  1410. else
  1411. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  1412. tmpref.base := NR_NO;
  1413. tmpref.symaddr := refs_l;
  1414. { can be folded with one of the next instructions by the }
  1415. { optimizer probably }
  1416. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  1417. end
  1418. end
  1419. else if ref2.offset <> 0 Then
  1420. if ref2.base <> NR_NO then
  1421. a_op_const_reg_reg(list,OP_ADD,OS_32,aword(ref2.offset),ref2.base,r)
  1422. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  1423. { occurs, so now only ref.offset has to be loaded }
  1424. else
  1425. a_load_const_reg(list,OS_32,ref2.offset,r)
  1426. else if ref.index <> NR_NO Then
  1427. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  1428. else if (ref2.base <> NR_NO) and
  1429. (r <> ref2.base) then
  1430. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  1431. if freereg then
  1432. rgint.ungetregister(list,ref2.base);
  1433. end;
  1434. { ************* concatcopy ************ }
  1435. {$ifndef ppc603}
  1436. const
  1437. maxmoveunit = 8;
  1438. {$else ppc603}
  1439. const
  1440. maxmoveunit = 4;
  1441. {$endif ppc603}
  1442. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  1443. var
  1444. countreg: TRegister;
  1445. src, dst: TReference;
  1446. lab: tasmlabel;
  1447. count, count2: aword;
  1448. orgsrc, orgdst: boolean;
  1449. size: tcgsize;
  1450. begin
  1451. {$ifdef extdebug}
  1452. if len > high(longint) then
  1453. internalerror(2002072704);
  1454. {$endif extdebug}
  1455. { make sure short loads are handled as optimally as possible }
  1456. if not loadref then
  1457. if (len <= maxmoveunit) and
  1458. (byte(len) in [1,2,4,8]) then
  1459. begin
  1460. if len < 8 then
  1461. begin
  1462. size := int_cgsize(len);
  1463. a_load_ref_ref(list,size,size,source,dest);
  1464. if delsource then
  1465. begin
  1466. reference_release(list,source);
  1467. tg.ungetiftemp(list,source);
  1468. end;
  1469. end
  1470. else
  1471. begin
  1472. a_reg_alloc(list,NR_F0);
  1473. a_loadfpu_ref_reg(list,OS_F64,source,NR_F0);
  1474. if delsource then
  1475. begin
  1476. reference_release(list,source);
  1477. tg.ungetiftemp(list,source);
  1478. end;
  1479. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dest);
  1480. a_reg_dealloc(list,NR_F0);
  1481. end;
  1482. exit;
  1483. end;
  1484. count := len div maxmoveunit;
  1485. reference_reset(src);
  1486. reference_reset(dst);
  1487. { load the address of source into src.base }
  1488. if loadref then
  1489. begin
  1490. src.base := rgint.getregister(list,R_SUBWHOLE);
  1491. a_load_ref_reg(list,OS_32,OS_32,source,src.base);
  1492. orgsrc := false;
  1493. end
  1494. else if (count > 4) or
  1495. not issimpleref(source) or
  1496. ((source.index <> NR_NO) and
  1497. ((source.offset + longint(len)) > high(smallint))) then
  1498. begin
  1499. src.base := rgint.getregister(list,R_SUBWHOLE);
  1500. a_loadaddr_ref_reg(list,source,src.base);
  1501. orgsrc := false;
  1502. end
  1503. else
  1504. begin
  1505. src := source;
  1506. orgsrc := true;
  1507. end;
  1508. if not orgsrc and delsource then
  1509. reference_release(list,source);
  1510. { load the address of dest into dst.base }
  1511. if (count > 4) or
  1512. not issimpleref(dest) or
  1513. ((dest.index <> NR_NO) and
  1514. ((dest.offset + longint(len)) > high(smallint))) then
  1515. begin
  1516. dst.base := rgint.getregister(list,R_SUBWHOLE);
  1517. a_loadaddr_ref_reg(list,dest,dst.base);
  1518. orgdst := false;
  1519. end
  1520. else
  1521. begin
  1522. dst := dest;
  1523. orgdst := true;
  1524. end;
  1525. {$ifndef ppc603}
  1526. if count > 4 then
  1527. { generate a loop }
  1528. begin
  1529. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1530. { have to be set to 8. I put an Inc there so debugging may be }
  1531. { easier (should offset be different from zero here, it will be }
  1532. { easy to notice in the generated assembler }
  1533. inc(dst.offset,8);
  1534. inc(src.offset,8);
  1535. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,8));
  1536. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,8));
  1537. countreg := rgint.getregister(list,R_SUBWHOLE);
  1538. a_load_const_reg(list,OS_32,count,countreg);
  1539. { explicitely allocate R_0 since it can be used safely here }
  1540. { (for holding date that's being copied) }
  1541. a_reg_alloc(list,NR_F0);
  1542. objectlibrary.getlabel(lab);
  1543. a_label(list, lab);
  1544. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1545. list.concat(taicpu.op_reg_ref(A_LFDU,NR_F0,src));
  1546. list.concat(taicpu.op_reg_ref(A_STFDU,NR_F0,dst));
  1547. a_jmp(list,A_BC,C_NE,0,lab);
  1548. rgint.ungetregister(list,countreg);
  1549. a_reg_dealloc(list,NR_F0);
  1550. len := len mod 8;
  1551. end;
  1552. count := len div 8;
  1553. if count > 0 then
  1554. { unrolled loop }
  1555. begin
  1556. a_reg_alloc(list,NR_F0);
  1557. for count2 := 1 to count do
  1558. begin
  1559. a_loadfpu_ref_reg(list,OS_F64,src,NR_F0);
  1560. a_loadfpu_reg_ref(list,OS_F64,NR_F0,dst);
  1561. inc(src.offset,8);
  1562. inc(dst.offset,8);
  1563. end;
  1564. a_reg_dealloc(list,NR_F0);
  1565. len := len mod 8;
  1566. end;
  1567. if (len and 4) <> 0 then
  1568. begin
  1569. a_reg_alloc(list,NR_R0);
  1570. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1571. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1572. inc(src.offset,4);
  1573. inc(dst.offset,4);
  1574. a_reg_dealloc(list,NR_R0);
  1575. end;
  1576. {$else not ppc603}
  1577. if count > 4 then
  1578. { generate a loop }
  1579. begin
  1580. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1581. { have to be set to 4. I put an Inc there so debugging may be }
  1582. { easier (should offset be different from zero here, it will be }
  1583. { easy to notice in the generated assembler }
  1584. inc(dst.offset,4);
  1585. inc(src.offset,4);
  1586. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  1587. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  1588. countreg := rgint.getregister(list,R_SUBWHOLE);
  1589. a_load_const_reg(list,OS_32,count,countreg);
  1590. { explicitely allocate R_0 since it can be used safely here }
  1591. { (for holding date that's being copied) }
  1592. a_reg_alloc(list,NR_R0);
  1593. objectlibrary.getlabel(lab);
  1594. a_label(list, lab);
  1595. list.concat(taicpu.op_reg_reg_const(A_SUBIC_,countreg,countreg,1));
  1596. list.concat(taicpu.op_reg_ref(A_LWZU,NR_R0,src));
  1597. list.concat(taicpu.op_reg_ref(A_STWU,NR_R0,dst));
  1598. a_jmp(list,A_BC,C_NE,0,lab);
  1599. rgint.ungetregister(list,countreg);
  1600. a_reg_dealloc(list,NR_R0);
  1601. len := len mod 4;
  1602. end;
  1603. count := len div 4;
  1604. if count > 0 then
  1605. { unrolled loop }
  1606. begin
  1607. a_reg_alloc(list,NR_R0);
  1608. for count2 := 1 to count do
  1609. begin
  1610. a_load_ref_reg(list,OS_32,OS_32,src,NR_R0);
  1611. a_load_reg_ref(list,OS_32,OS_32,NR_R0,dst);
  1612. inc(src.offset,4);
  1613. inc(dst.offset,4);
  1614. end;
  1615. a_reg_dealloc(list,r);
  1616. len := len mod 4;
  1617. end;
  1618. {$endif not ppc603}
  1619. { copy the leftovers }
  1620. if (len and 2) <> 0 then
  1621. begin
  1622. a_reg_alloc(list,NR_R0);
  1623. a_load_ref_reg(list,OS_16,OS_16,src,NR_R0);
  1624. a_load_reg_ref(list,OS_16,OS_16,NR_R0,dst);
  1625. inc(src.offset,2);
  1626. inc(dst.offset,2);
  1627. a_reg_dealloc(list,NR_R0);
  1628. end;
  1629. if (len and 1) <> 0 then
  1630. begin
  1631. a_reg_alloc(list,NR_R0);
  1632. a_load_ref_reg(list,OS_8,OS_8,src,NR_R0);
  1633. a_load_reg_ref(list,OS_8,OS_8,NR_R0,dst);
  1634. a_reg_dealloc(list,NR_R0);
  1635. end;
  1636. if orgsrc then
  1637. begin
  1638. if delsource then
  1639. reference_release(list,source);
  1640. end
  1641. else
  1642. rgint.ungetregister(list,src.base);
  1643. if not orgdst then
  1644. rgint.ungetregister(list,dst.base);
  1645. if delsource then
  1646. tg.ungetiftemp(list,source);
  1647. end;
  1648. procedure tcgppc.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  1649. var
  1650. power,len : longint;
  1651. {$ifndef __NOWINPECOFF__}
  1652. again,ok : tasmlabel;
  1653. {$endif}
  1654. // r,r2,rsp:Tregister;
  1655. begin
  1656. {$warning !!!! FIX ME !!!!}
  1657. internalerror(200305231);
  1658. (* !!!!
  1659. lenref:=ref;
  1660. inc(lenref.offset,4);
  1661. { get stack space }
  1662. r.enum:=R_INTREGISTER;
  1663. r.number:=NR_EDI;
  1664. rsp.enum:=R_INTREGISTER;
  1665. rsp.number:=NR_ESP;
  1666. r2.enum:=R_INTREGISTER;
  1667. rg.getexplicitregisterint(list,NR_EDI);
  1668. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1669. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1670. if (elesize<>1) then
  1671. begin
  1672. if ispowerof2(elesize, power) then
  1673. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1674. else
  1675. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1676. end;
  1677. {$ifndef __NOWINPECOFF__}
  1678. { windows guards only a few pages for stack growing, }
  1679. { so we have to access every page first }
  1680. if target_info.system=system_i386_win32 then
  1681. begin
  1682. objectlibrary.getlabel(again);
  1683. objectlibrary.getlabel(ok);
  1684. a_label(list,again);
  1685. list.concat(Taicpu.op_const_reg(A_CMP,S_L,winstackpagesize,r));
  1686. a_jmp_cond(list,OC_B,ok);
  1687. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,rsp));
  1688. r2.number:=NR_EAX;
  1689. list.concat(Taicpu.op_reg(A_PUSH,S_L,r));
  1690. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize,r));
  1691. a_jmp_always(list,again);
  1692. a_label(list,ok);
  1693. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1694. rgint.ungetregister(list,r);
  1695. { now reload EDI }
  1696. rg.getexplicitregisterint(list,NR_EDI);
  1697. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,lenref,r));
  1698. list.concat(Taicpu.op_reg(A_INC,S_L,r));
  1699. if (elesize<>1) then
  1700. begin
  1701. if ispowerof2(elesize, power) then
  1702. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r))
  1703. else
  1704. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,elesize,r));
  1705. end;
  1706. end
  1707. else
  1708. {$endif __NOWINPECOFF__}
  1709. list.concat(Taicpu.op_reg_reg(A_SUB,S_L,r,rsp));
  1710. { align stack on 4 bytes }
  1711. list.concat(Taicpu.op_const_reg(A_AND,S_L,$fffffff4,rsp));
  1712. { load destination }
  1713. a_load_reg_reg(list,OS_INT,OS_INT,rsp,r);
  1714. { don't destroy the registers! }
  1715. r2.number:=NR_ECX;
  1716. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1717. r2.number:=NR_ESI;
  1718. list.concat(Taicpu.op_reg(A_PUSH,S_L,r2));
  1719. { load count }
  1720. r2.number:=NR_ECX;
  1721. a_load_ref_reg(list,OS_INT,lenref,r2);
  1722. { load source }
  1723. r2.number:=NR_ESI;
  1724. a_load_ref_reg(list,OS_INT,ref,r2);
  1725. { scheduled .... }
  1726. r2.number:=NR_ECX;
  1727. list.concat(Taicpu.op_reg(A_INC,S_L,r2));
  1728. { calculate size }
  1729. len:=elesize;
  1730. opsize:=S_B;
  1731. if (len and 3)=0 then
  1732. begin
  1733. opsize:=S_L;
  1734. len:=len shr 2;
  1735. end
  1736. else
  1737. if (len and 1)=0 then
  1738. begin
  1739. opsize:=S_W;
  1740. len:=len shr 1;
  1741. end;
  1742. if ispowerof2(len, power) then
  1743. list.concat(Taicpu.op_const_reg(A_SHL,S_L,power,r2))
  1744. else
  1745. list.concat(Taicpu.op_const_reg(A_IMUL,S_L,len,r2));
  1746. list.concat(Taicpu.op_none(A_REP,S_NO));
  1747. case opsize of
  1748. S_B : list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  1749. S_W : list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  1750. S_L : list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  1751. end;
  1752. rgint.ungetregister(list,r);
  1753. r2.number:=NR_ESI;
  1754. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1755. r2.number:=NR_ECX;
  1756. list.concat(Taicpu.op_reg(A_POP,S_L,r2));
  1757. { patch the new address }
  1758. a_load_reg_ref(list,OS_INT,rsp,ref);
  1759. !!!! *)
  1760. end;
  1761. procedure tcgppc.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  1762. var
  1763. hl : tasmlabel;
  1764. begin
  1765. if not(cs_check_overflow in aktlocalswitches) then
  1766. exit;
  1767. objectlibrary.getlabel(hl);
  1768. if not ((def.deftype=pointerdef) or
  1769. ((def.deftype=orddef) and
  1770. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1771. bool8bit,bool16bit,bool32bit]))) then
  1772. begin
  1773. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  1774. a_jmp(list,A_BC,C_OV,7,hl)
  1775. end
  1776. else
  1777. a_jmp_cond(list,OC_AE,hl);
  1778. a_call_name(list,'FPC_OVERFLOW');
  1779. a_label(list,hl);
  1780. end;
  1781. {***************** This is private property, keep out! :) *****************}
  1782. function tcgppc.issimpleref(const ref: treference): boolean;
  1783. begin
  1784. if (ref.base = NR_NO) and
  1785. (ref.index <> NR_NO) then
  1786. internalerror(200208101);
  1787. result :=
  1788. not(assigned(ref.symbol)) and
  1789. (((ref.index = NR_NO) and
  1790. (ref.offset >= low(smallint)) and
  1791. (ref.offset <= high(smallint))) or
  1792. ((ref.index <> NR_NO) and
  1793. (ref.offset = 0)));
  1794. end;
  1795. function tcgppc.fixref(list: taasmoutput; var ref: treference): boolean;
  1796. var
  1797. tmpreg: tregister;
  1798. orgindex: tregister;
  1799. freeindex: boolean;
  1800. begin
  1801. result := false;
  1802. if (ref.base = NR_NO) then
  1803. begin
  1804. ref.base := ref.index;
  1805. ref.base := NR_NO;
  1806. end;
  1807. if (ref.base <> NR_NO) then
  1808. begin
  1809. if (ref.index <> NR_NO) and
  1810. ((ref.offset <> 0) or assigned(ref.symbol)) then
  1811. begin
  1812. result := true;
  1813. { references are often freed before they are used. Since we allocate }
  1814. { a register here, we must first reallocate the index register, since }
  1815. { otherwise it may be overwritten (and it's still used afterwards) }
  1816. freeindex := false;
  1817. if (getsupreg(ref.index) >= first_int_supreg) and
  1818. (getsupreg(ref.index) in rgint.unusedregs) then
  1819. begin
  1820. rgint.getexplicitregister(list,ref.index);
  1821. orgindex := ref.index;
  1822. freeindex := true;
  1823. end;
  1824. tmpreg := rgint.getregister(list,R_SUBWHOLE);
  1825. if not assigned(ref.symbol) and
  1826. (cardinal(ref.offset-low(smallint)) <=
  1827. high(smallint)-low(smallint)) then
  1828. begin
  1829. list.concat(taicpu.op_reg_reg_const(
  1830. A_ADDI,tmpreg,ref.base,ref.offset));
  1831. ref.offset := 0;
  1832. end
  1833. else
  1834. begin
  1835. list.concat(taicpu.op_reg_reg_reg(
  1836. A_ADD,tmpreg,ref.base,ref.index));
  1837. ref.index := NR_NO;
  1838. end;
  1839. ref.base := tmpreg;
  1840. if freeindex then
  1841. rgint.ungetregister(list,orgindex);
  1842. end
  1843. end
  1844. else
  1845. if ref.index <> NR_NO then
  1846. internalerror(200208102);
  1847. end;
  1848. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  1849. { that's the case, we can use rlwinm to do an AND operation }
  1850. function tcgppc.get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  1851. var
  1852. temp : longint;
  1853. testbit : aword;
  1854. compare: boolean;
  1855. begin
  1856. get_rlwi_const := false;
  1857. if (a = 0) or (a = $ffffffff) then
  1858. exit;
  1859. { start with the lowest bit }
  1860. testbit := 1;
  1861. { check its value }
  1862. compare := boolean(a and testbit);
  1863. { find out how long the run of bits with this value is }
  1864. { (it's impossible that all bits are 1 or 0, because in that case }
  1865. { this function wouldn't have been called) }
  1866. l1 := 31;
  1867. while (((a and testbit) <> 0) = compare) do
  1868. begin
  1869. testbit := testbit shl 1;
  1870. dec(l1);
  1871. end;
  1872. { check the length of the run of bits that comes next }
  1873. compare := not compare;
  1874. l2 := l1;
  1875. while (((a and testbit) <> 0) = compare) and
  1876. (l2 >= 0) do
  1877. begin
  1878. testbit := testbit shl 1;
  1879. dec(l2);
  1880. end;
  1881. { and finally the check whether the rest of the bits all have the }
  1882. { same value }
  1883. compare := not compare;
  1884. temp := l2;
  1885. if temp >= 0 then
  1886. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  1887. exit;
  1888. { we have done "not(not(compare))", so compare is back to its }
  1889. { initial value. If the lowest bit was 0, a is of the form }
  1890. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  1891. { because l2 now contains the position of the last zero of the }
  1892. { first run instead of that of the first 1) so switch l1 and l2 }
  1893. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  1894. if not compare then
  1895. begin
  1896. temp := l1;
  1897. l1 := l2+1;
  1898. l2 := temp;
  1899. end
  1900. else
  1901. { otherwise, l1 currently contains the position of the last }
  1902. { zero instead of that of the first 1 of the second run -> +1 }
  1903. inc(l1);
  1904. { the following is the same as "if l1 = -1 then l1 := 31;" }
  1905. l1 := l1 and 31;
  1906. l2 := l2 and 31;
  1907. get_rlwi_const := true;
  1908. end;
  1909. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  1910. ref: treference);
  1911. var
  1912. tmpreg: tregister;
  1913. tmpregUsed: Boolean;
  1914. tmpref: treference;
  1915. largeOffset: Boolean;
  1916. begin
  1917. tmpreg := NR_NO;
  1918. if target_info.system = system_powerpc_macos then
  1919. begin
  1920. largeOffset:= (cardinal(ref.offset-low(smallint)) >
  1921. high(smallint)-low(smallint));
  1922. tmpreg := rgint.getregister(list,R_SUBWHOLE);
  1923. tmpregUsed:= false;
  1924. if assigned(ref.symbol) then
  1925. begin //Load symbol's value
  1926. reference_reset(tmpref);
  1927. tmpref.symbol := ref.symbol;
  1928. tmpref.base := NR_RTOC;
  1929. if macos_direct_globals then
  1930. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  1931. else
  1932. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  1933. tmpregUsed:= true;
  1934. end;
  1935. if largeOffset then
  1936. begin //Add hi part of offset
  1937. reference_reset(tmpref);
  1938. tmpref.offset := Hi(ref.offset);
  1939. if tmpregUsed then
  1940. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1941. tmpreg,tmpref))
  1942. else
  1943. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1944. tmpregUsed:= true;
  1945. end;
  1946. if tmpregUsed then
  1947. begin
  1948. //Add content of base register
  1949. if ref.base <> NR_NO then
  1950. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  1951. ref.base,tmpreg));
  1952. //Make ref ready to be used by op
  1953. ref.symbol:= nil;
  1954. ref.base:= tmpreg;
  1955. if largeOffset then
  1956. ref.offset := Lo(ref.offset);
  1957. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1958. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  1959. end
  1960. else
  1961. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1962. end
  1963. else {if target_info.system <> system_powerpc_macos}
  1964. begin
  1965. if assigned(ref.symbol) or
  1966. (cardinal(ref.offset-low(smallint)) >
  1967. high(smallint)-low(smallint)) then
  1968. begin
  1969. tmpreg := rgint.getregister(list,R_SUBWHOLE);
  1970. reference_reset(tmpref);
  1971. tmpref.symbol := ref.symbol;
  1972. tmpref.offset := ref.offset;
  1973. tmpref.symaddr := refs_ha;
  1974. if ref.base <> NR_NO then
  1975. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  1976. ref.base,tmpref))
  1977. else
  1978. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  1979. ref.base := tmpreg;
  1980. ref.symaddr := refs_l;
  1981. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1982. end
  1983. else
  1984. list.concat(taicpu.op_reg_ref(op,reg,ref));
  1985. end;
  1986. if (tmpreg <> NR_NO) then
  1987. rgint.ungetregister(list,tmpreg);
  1988. end;
  1989. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  1990. crval: longint; l: tasmlabel);
  1991. var
  1992. p: taicpu;
  1993. begin
  1994. p := taicpu.op_sym(op,objectlibrary.newasmsymbol(l.name));
  1995. if op <> A_B then
  1996. create_cond_norm(c,crval,p.condition);
  1997. p.is_jmp := true;
  1998. list.concat(p)
  1999. end;
  2000. procedure tcg64fppc.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  2001. begin
  2002. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  2003. end;
  2004. procedure tcg64fppc.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  2005. begin
  2006. a_op64_const_reg_reg(list,op,value,reg,reg);
  2007. end;
  2008. procedure tcg64fppc.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  2009. begin
  2010. case op of
  2011. OP_AND,OP_OR,OP_XOR:
  2012. begin
  2013. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2014. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2015. end;
  2016. OP_ADD:
  2017. begin
  2018. list.concat(taicpu.op_reg_reg_reg(A_ADDC,regdst.reglo,regsrc1.reglo,regsrc2.reglo));
  2019. list.concat(taicpu.op_reg_reg_reg(A_ADDE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2020. end;
  2021. OP_SUB:
  2022. begin
  2023. list.concat(taicpu.op_reg_reg_reg(A_SUBC,regdst.reglo,regsrc2.reglo,regsrc1.reglo));
  2024. list.concat(taicpu.op_reg_reg_reg(A_SUBFE,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2025. end;
  2026. else
  2027. internalerror(2002072801);
  2028. end;
  2029. end;
  2030. procedure tcg64fppc.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  2031. const
  2032. ops: array[boolean,1..3] of tasmop = ((A_ADDIC,A_ADDC,A_ADDZE),
  2033. (A_SUBIC,A_SUBC,A_ADDME));
  2034. var
  2035. tmpreg: tregister;
  2036. tmpreg64: tregister64;
  2037. issub: boolean;
  2038. begin
  2039. case op of
  2040. OP_AND,OP_OR,OP_XOR:
  2041. begin
  2042. cg.a_op_const_reg_reg(list,op,OS_32,aword(value),regsrc.reglo,regdst.reglo);
  2043. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2044. regdst.reghi);
  2045. end;
  2046. OP_ADD, OP_SUB:
  2047. begin
  2048. if (int64(value) < 0) then
  2049. begin
  2050. if op = OP_ADD then
  2051. op := OP_SUB
  2052. else
  2053. op := OP_ADD;
  2054. int64(value) := -int64(value);
  2055. end;
  2056. if (longint(value) <> 0) then
  2057. begin
  2058. issub := op = OP_SUB;
  2059. if (int64(value) > 0) and
  2060. (int64(value)-ord(issub) <= 32767) then
  2061. begin
  2062. list.concat(taicpu.op_reg_reg_const(ops[issub,1],
  2063. regdst.reglo,regsrc.reglo,longint(value)));
  2064. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2065. regdst.reghi,regsrc.reghi));
  2066. end
  2067. else if ((value shr 32) = 0) then
  2068. begin
  2069. tmpreg := tcgppc(cg).rgint.getregister(list,R_SUBWHOLE);
  2070. cg.a_load_const_reg(list,OS_32,cardinal(value),tmpreg);
  2071. list.concat(taicpu.op_reg_reg_reg(ops[issub,2],
  2072. regdst.reglo,regsrc.reglo,tmpreg));
  2073. tcgppc(cg).rgint.ungetregister(list,tmpreg);
  2074. list.concat(taicpu.op_reg_reg(ops[issub,3],
  2075. regdst.reghi,regsrc.reghi));
  2076. end
  2077. else
  2078. begin
  2079. tmpreg64.reglo := tcgppc(cg).rgint.getregister(list,R_SUBWHOLE);
  2080. tmpreg64.reghi := tcgppc(cg).rgint.getregister(list,R_SUBWHOLE);
  2081. a_load64_const_reg(list,value,tmpreg64);
  2082. a_op64_reg_reg_reg(list,op,tmpreg64,regsrc,regdst);
  2083. tcgppc(cg).rgint.ungetregister(list,tmpreg64.reglo);
  2084. tcgppc(cg).rgint.ungetregister(list,tmpreg64.reghi);
  2085. end
  2086. end
  2087. else
  2088. begin
  2089. cg.a_load_reg_reg(list,OS_INT,OS_INT,regsrc.reglo,regdst.reglo);
  2090. cg.a_op_const_reg_reg(list,op,OS_32,aword(value shr 32),regsrc.reghi,
  2091. regdst.reghi);
  2092. end;
  2093. end;
  2094. else
  2095. internalerror(2002072802);
  2096. end;
  2097. end;
  2098. begin
  2099. cg := tcgppc.create;
  2100. cg64 :=tcg64fppc.create;
  2101. end.
  2102. {
  2103. $Log$
  2104. Revision 1.128 2003-10-11 16:06:42 florian
  2105. * fixed some MMX<->SSE
  2106. * started to fix ppc, needs an overhaul
  2107. + stabs info improve for spilling, not sure if it works correctly/completly
  2108. - MMX_SUPPORT removed from Makefile.fpc
  2109. Revision 1.127 2003/10/01 20:34:49 peter
  2110. * procinfo unit contains tprocinfo
  2111. * cginfo renamed to cgbase
  2112. * moved cgmessage to verbose
  2113. * fixed ppc and sparc compiles
  2114. Revision 1.126 2003/09/14 16:37:20 jonas
  2115. * fixed some ppc problems
  2116. Revision 1.125 2003/09/03 21:04:14 peter
  2117. * some fixes for ppc
  2118. Revision 1.124 2003/09/03 19:35:24 peter
  2119. * powerpc compiles again
  2120. Revision 1.123 2003/09/03 15:55:01 peter
  2121. * NEWRA branch merged
  2122. Revision 1.122.2.1 2003/08/31 21:08:16 peter
  2123. * first batch of sparc fixes
  2124. Revision 1.122 2003/08/18 21:27:00 jonas
  2125. * some newra optimizations (eliminate lots of moves between registers)
  2126. Revision 1.121 2003/08/18 11:50:55 olle
  2127. + cleaning up in proc entry and exit, now calc_stack_frame always is used.
  2128. Revision 1.120 2003/08/17 16:59:20 jonas
  2129. * fixed regvars so they work with newra (at least for ppc)
  2130. * fixed some volatile register bugs
  2131. + -dnotranslation option for -dnewra, which causes the registers not to
  2132. be translated from virtual to normal registers. Requires support in
  2133. the assembler writer as well, which is only implemented in aggas/
  2134. agppcgas currently
  2135. Revision 1.119 2003/08/11 21:18:20 peter
  2136. * start of sparc support for newra
  2137. Revision 1.118 2003/08/08 15:50:45 olle
  2138. * merged macos entry/exit code generation into the general one.
  2139. Revision 1.117 2002/10/01 05:24:28 olle
  2140. * made a_load_store more robust and to accept large offsets and cleaned up code
  2141. Revision 1.116 2003/07/23 11:02:23 jonas
  2142. * don't use rg.getregisterint() anymore in g_stackframe_entry_*, because
  2143. the register colouring has already occurred then, use a hard-coded
  2144. register instead
  2145. Revision 1.115 2003/07/20 20:39:20 jonas
  2146. * fixed newra bug due to the fact that we sometimes need a temp reg
  2147. when loading/storing to memory (base+index+offset is not possible)
  2148. and because a reference is often freed before it is last used, this
  2149. temp register was soemtimes the same as one of the reference regs
  2150. Revision 1.114 2003/07/20 16:15:58 jonas
  2151. * fixed bug in g_concatcopy with -dnewra
  2152. Revision 1.113 2003/07/06 20:25:03 jonas
  2153. * fixed ppc compiler
  2154. Revision 1.112 2003/07/05 20:11:42 jonas
  2155. * create_paraloc_info() is now called separately for the caller and
  2156. callee info
  2157. * fixed ppc cycle
  2158. Revision 1.111 2003/07/02 22:18:04 peter
  2159. * paraloc splitted in callerparaloc,calleeparaloc
  2160. * sparc calling convention updates
  2161. Revision 1.110 2003/06/18 10:12:36 olle
  2162. * macos: fixes of loading-code
  2163. Revision 1.109 2003/06/14 22:32:43 jonas
  2164. * ppc compiles with -dnewra, haven't tried to compile anything with it
  2165. yet though
  2166. Revision 1.108 2003/06/13 21:19:31 peter
  2167. * current_procdef removed, use current_procinfo.procdef instead
  2168. Revision 1.107 2003/06/09 14:54:26 jonas
  2169. * (de)allocation of registers for parameters is now performed properly
  2170. (and checked on the ppc)
  2171. - removed obsolete allocation of all parameter registers at the start
  2172. of a procedure (and deallocation at the end)
  2173. Revision 1.106 2003/06/08 18:19:27 jonas
  2174. - removed duplicate identifier
  2175. Revision 1.105 2003/06/07 18:57:04 jonas
  2176. + added freeintparaloc
  2177. * ppc get/freeintparaloc now check whether the parameter regs are
  2178. properly allocated/deallocated (and get an extra list para)
  2179. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  2180. * fixed lot of missing pi_do_call's
  2181. Revision 1.104 2003/06/04 11:58:58 jonas
  2182. * calculate localsize also in g_return_from_proc since it's now called
  2183. before g_stackframe_entry (still have to fix macos)
  2184. * compilation fixes (cycle doesn't work yet though)
  2185. Revision 1.103 2003/06/01 21:38:06 peter
  2186. * getregisterfpu size parameter added
  2187. * op_const_reg size parameter added
  2188. * sparc updates
  2189. Revision 1.102 2003/06/01 13:42:18 jonas
  2190. * fix for bug in fixref that Peter found during the Sparc conversion
  2191. Revision 1.101 2003/05/30 18:52:10 jonas
  2192. * fixed bug with intregvars
  2193. * locapara.loc can also be LOC_CFPUREGISTER -> also fixed
  2194. rcgppc.a_param_ref, which previously got bogus size values
  2195. Revision 1.100 2003/05/29 21:17:27 jonas
  2196. * compile with -dppc603 to not use unaligned float loads in move() and
  2197. g_concatcopy, because the 603 and 604 take an exception for those
  2198. (and netbsd doesn't even handle those in the kernel). There are
  2199. still some of those left that could cause problems though (e.g.
  2200. in the set helpers)
  2201. Revision 1.99 2003/05/29 10:06:09 jonas
  2202. * also free temps in g_concatcopy if delsource is true
  2203. Revision 1.98 2003/05/28 23:58:18 jonas
  2204. * added missing initialization of rg.usedintin,byproc
  2205. * ppc now also saves/restores used fpu registers
  2206. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  2207. i386
  2208. Revision 1.97 2003/05/28 23:18:31 florian
  2209. * started to fix and clean up the sparc port
  2210. Revision 1.96 2003/05/24 11:59:42 jonas
  2211. * fixed integer typeconversion problems
  2212. Revision 1.95 2003/05/23 18:51:26 jonas
  2213. * fixed support for nested procedures and more parameters than those
  2214. which fit in registers (untested/probably not working: calling a
  2215. nested procedure from a deeper nested procedure)
  2216. Revision 1.94 2003/05/20 23:54:00 florian
  2217. + basic darwin support added
  2218. Revision 1.93 2003/05/15 22:14:42 florian
  2219. * fixed last commit, changing lastsaveintreg to r31 caused some strange problems
  2220. Revision 1.92 2003/05/15 21:37:00 florian
  2221. * sysv entry code saves r13 now as well
  2222. Revision 1.91 2003/05/15 19:39:09 florian
  2223. * fixed ppc compiler which was broken by Peter's changes
  2224. Revision 1.90 2003/05/12 18:43:50 jonas
  2225. * fixed g_concatcopy
  2226. Revision 1.89 2003/05/11 20:59:23 jonas
  2227. * fixed bug with large offsets in entrycode
  2228. Revision 1.88 2003/05/11 11:45:08 jonas
  2229. * fixed shifts
  2230. Revision 1.87 2003/05/11 11:07:33 jonas
  2231. * fixed optimizations in a_op_const_reg_reg()
  2232. Revision 1.86 2003/04/27 11:21:36 peter
  2233. * aktprocdef renamed to current_procinfo.procdef
  2234. * procinfo renamed to current_procinfo
  2235. * procinfo will now be stored in current_module so it can be
  2236. cleaned up properly
  2237. * gen_main_procsym changed to create_main_proc and release_main_proc
  2238. to also generate a tprocinfo structure
  2239. * fixed unit implicit initfinal
  2240. Revision 1.85 2003/04/26 22:56:11 jonas
  2241. * fix to a_op64_const_reg_reg
  2242. Revision 1.84 2003/04/26 16:08:41 jonas
  2243. * fixed g_flags2reg
  2244. Revision 1.83 2003/04/26 15:25:29 florian
  2245. * fixed cmp_reg_reg_reg, cmp operands were emitted in the wrong order
  2246. Revision 1.82 2003/04/25 20:55:34 florian
  2247. * stack frame calculations are now completly done using the code generator
  2248. routines instead of generating directly assembler so also large stack frames
  2249. are handle properly
  2250. Revision 1.81 2003/04/24 11:24:00 florian
  2251. * fixed several issues with nested procedures
  2252. Revision 1.80 2003/04/23 22:18:01 peter
  2253. * fixes to get rtl compiled
  2254. Revision 1.79 2003/04/23 12:35:35 florian
  2255. * fixed several issues with powerpc
  2256. + applied a patch from Jonas for nested function calls (PowerPC only)
  2257. * ...
  2258. Revision 1.78 2003/04/16 09:26:55 jonas
  2259. * assembler procedures now again get a stackframe if they have local
  2260. variables. No space is reserved for a function result however.
  2261. Also, the register parameters aren't automatically saved on the stack
  2262. anymore in assembler procedures.
  2263. Revision 1.77 2003/04/06 16:39:11 jonas
  2264. * don't generate entry/exit code for assembler procedures
  2265. Revision 1.76 2003/03/22 18:01:13 jonas
  2266. * fixed linux entry/exit code generation
  2267. Revision 1.75 2003/03/19 14:26:26 jonas
  2268. * fixed R_TOC bugs introduced by new register allocator conversion
  2269. Revision 1.74 2003/03/13 22:57:45 olle
  2270. * change in a_loadaddr_ref_reg
  2271. Revision 1.73 2003/03/12 22:43:38 jonas
  2272. * more powerpc and generic fixes related to the new register allocator
  2273. Revision 1.72 2003/03/11 21:46:24 jonas
  2274. * lots of new regallocator fixes, both in generic and ppc-specific code
  2275. (ppc compiler still can't compile the linux system unit though)
  2276. Revision 1.71 2003/02/19 22:00:16 daniel
  2277. * Code generator converted to new register notation
  2278. - Horribily outdated todo.txt removed
  2279. Revision 1.70 2003/01/13 17:17:50 olle
  2280. * changed global var access, TOC now contain pointers to globals
  2281. * fixed handling of function pointers
  2282. Revision 1.69 2003/01/09 22:00:53 florian
  2283. * fixed some PowerPC issues
  2284. Revision 1.68 2003/01/08 18:43:58 daniel
  2285. * Tregister changed into a record
  2286. Revision 1.67 2002/12/15 19:22:01 florian
  2287. * fixed some crashes and a rte 201
  2288. Revision 1.66 2002/11/28 10:55:16 olle
  2289. * macos: changing code gen for references to globals
  2290. Revision 1.65 2002/11/07 15:50:23 jonas
  2291. * fixed bctr(l) problems
  2292. Revision 1.64 2002/11/04 18:24:19 olle
  2293. * macos: globals are located in TOC and relative r2, instead of absolute
  2294. Revision 1.63 2002/10/28 22:24:28 olle
  2295. * macos entry/exit: only used registers are saved
  2296. - macos entry/exit: stackptr not saved in r31 anymore
  2297. * macos entry/exit: misc fixes
  2298. Revision 1.62 2002/10/19 23:51:48 olle
  2299. * macos stack frame size computing updated
  2300. + macos epilogue: control register now restored
  2301. * macos prologue and epilogue: fp reg now saved and restored
  2302. Revision 1.61 2002/10/19 12:50:36 olle
  2303. * reorganized prologue and epilogue routines
  2304. Revision 1.60 2002/10/02 21:49:51 florian
  2305. * all A_BL instructions replaced by calls to a_call_name
  2306. Revision 1.59 2002/10/02 13:24:58 jonas
  2307. * changed a_call_* so that no superfluous code is generated anymore
  2308. Revision 1.58 2002/09/17 18:54:06 jonas
  2309. * a_load_reg_reg() now has two size parameters: source and dest. This
  2310. allows some optimizations on architectures that don't encode the
  2311. register size in the register name.
  2312. Revision 1.57 2002/09/10 21:22:25 jonas
  2313. + added some internal errors
  2314. * fixed bug in sysv exit code
  2315. Revision 1.56 2002/09/08 20:11:56 jonas
  2316. * fixed TOpCmp2AsmCond array (some unsigned equivalents were wrong)
  2317. Revision 1.55 2002/09/08 13:03:26 jonas
  2318. * several large offset-related fixes
  2319. Revision 1.54 2002/09/07 17:54:58 florian
  2320. * first part of PowerPC fixes
  2321. Revision 1.53 2002/09/07 15:25:14 peter
  2322. * old logs removed and tabs fixed
  2323. Revision 1.52 2002/09/02 10:14:51 jonas
  2324. + a_call_reg()
  2325. * small fix in a_call_ref()
  2326. Revision 1.51 2002/09/02 06:09:02 jonas
  2327. * fixed range error
  2328. Revision 1.50 2002/09/01 21:04:49 florian
  2329. * several powerpc related stuff fixed
  2330. Revision 1.49 2002/09/01 12:09:27 peter
  2331. + a_call_reg, a_call_loc added
  2332. * removed exprasmlist references
  2333. Revision 1.48 2002/08/31 21:38:02 jonas
  2334. * fixed a_call_ref (it should load ctr, not lr)
  2335. Revision 1.47 2002/08/31 21:30:45 florian
  2336. * fixed several problems caused by Jonas' commit :)
  2337. Revision 1.46 2002/08/31 19:25:50 jonas
  2338. + implemented a_call_ref()
  2339. Revision 1.45 2002/08/18 22:16:14 florian
  2340. + the ppc gas assembler writer adds now registers aliases
  2341. to the assembler file
  2342. Revision 1.44 2002/08/17 18:23:53 florian
  2343. * some assembler writer bugs fixed
  2344. Revision 1.43 2002/08/17 09:23:49 florian
  2345. * first part of procinfo rewrite
  2346. Revision 1.42 2002/08/16 14:24:59 carl
  2347. * issameref() to test if two references are the same (then emit no opcodes)
  2348. + ret_in_reg to replace ret_in_acc
  2349. (fix some register allocation bugs at the same time)
  2350. + save_std_register now has an extra parameter which is the
  2351. usedinproc registers
  2352. Revision 1.41 2002/08/15 08:13:54 carl
  2353. - a_load_sym_ofs_reg removed
  2354. * loadvmt now calls loadaddr_ref_reg instead
  2355. Revision 1.40 2002/08/11 14:32:32 peter
  2356. * renamed current_library to objectlibrary
  2357. Revision 1.39 2002/08/11 13:24:18 peter
  2358. * saving of asmsymbols in ppu supported
  2359. * asmsymbollist global is removed and moved into a new class
  2360. tasmlibrarydata that will hold the info of a .a file which
  2361. corresponds with a single module. Added librarydata to tmodule
  2362. to keep the library info stored for the module. In the future the
  2363. objectfiles will also be stored to the tasmlibrarydata class
  2364. * all getlabel/newasmsymbol and friends are moved to the new class
  2365. Revision 1.38 2002/08/11 11:39:31 jonas
  2366. + powerpc-specific genlinearlist
  2367. Revision 1.37 2002/08/10 17:15:31 jonas
  2368. * various fixes and optimizations
  2369. Revision 1.36 2002/08/06 20:55:23 florian
  2370. * first part of ppc calling conventions fix
  2371. Revision 1.35 2002/08/06 07:12:05 jonas
  2372. * fixed bug in g_flags2reg()
  2373. * and yet more constant operation fixes :)
  2374. Revision 1.34 2002/08/05 08:58:53 jonas
  2375. * fixed compilation problems
  2376. Revision 1.33 2002/08/04 12:57:55 jonas
  2377. * more misc. fixes, mostly constant-related
  2378. }