cgcpu.pas 94 KB

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