cgcpu.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  1. {
  2. Copyright (c) 1998-2002 by the FPC team
  3. This unit implements the code generator for the 680x0
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,cgobj,globtype,
  22. aasmbase,aasmtai,aasmdata,aasmcpu,
  23. cpubase,cpuinfo,
  24. parabase,cpupara,
  25. node,symconst,symtype,symdef,
  26. cgutils,cg64f32;
  27. type
  28. tcg68k = class(tcg)
  29. procedure init_register_allocators;override;
  30. procedure done_register_allocators;override;
  31. procedure a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);override;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);override;
  33. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);override;
  34. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);override;
  35. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  36. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  37. procedure a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);override;
  38. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  39. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  40. procedure a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);override;
  41. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);override;
  42. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  43. procedure a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);override;
  44. procedure a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);override;
  45. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  46. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  47. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  48. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  49. procedure a_loadfpu_reg_cgpara(list : TAsmList; size : tcgsize;const reg : tregister;const cgpara : TCGPara); override;
  50. procedure a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);override;
  51. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister); override;
  52. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  53. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  54. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference); override;
  55. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  56. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister; l : tasmlabel);override;
  57. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel); override;
  58. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  59. procedure a_jmp_name(list : TAsmList;const s : string); override;
  60. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  61. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  62. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  63. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  64. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  65. { generates overflow checking code for a node }
  66. procedure g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef); override;
  67. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  68. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  69. procedure g_save_registers(list:TAsmList);override;
  70. procedure g_restore_registers(list:TAsmList);override;
  71. procedure g_adjust_self_value(list:TAsmList;procdef:tprocdef;ioffset:tcgint);override;
  72. { # Sign or zero extend the register to a full 32-bit value.
  73. The new value is left in the same register.
  74. }
  75. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  76. procedure sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  77. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  78. function fixref(list: TAsmList; var ref: treference; fullyresolve: boolean): boolean;
  79. function force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  80. procedure move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  81. { optimize mul with const to a sequence of shifts and subs/adds, mainly for the '000 to '030 }
  82. function optimize_const_mul_to_shift_sub_add(list: TAsmList; maxops: longint; a: tcgint; size: tcgsize; reg: TRegister): boolean;
  83. protected
  84. procedure call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  85. procedure call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  86. procedure check_register_size(size:tcgsize;reg:tregister);
  87. end;
  88. tcg64f68k = class(tcg64f32)
  89. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG; size: tcgsize; regsrc,regdst : tregister64);override;
  90. procedure a_op64_const_reg(list : TAsmList;op:TOpCG; size: tcgsize; value : int64;regdst : tregister64);override;
  91. procedure a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);override;
  92. procedure a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const ref : treference);override;
  93. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference); override;
  94. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64); override;
  95. end;
  96. { This function returns true if the reference+offset is valid.
  97. Otherwise extra code must be generated to solve the reference.
  98. On the m68k, this verifies that the reference is valid
  99. (e.g : if index register is used, then the max displacement
  100. is 256 bytes, if only base is used, then max displacement
  101. is 32K
  102. }
  103. function isvalidrefoffset(const ref: treference): boolean;
  104. function isvalidreference(const ref: treference): boolean;
  105. procedure create_codegen;
  106. implementation
  107. uses
  108. globals,verbose,systems,cutils,
  109. symsym,symtable,defutil,paramgr,procinfo,
  110. rgobj,tgobj,rgcpu,fmodule;
  111. const
  112. { opcode table lookup }
  113. topcg2tasmop: Array[topcg] of tasmop =
  114. (
  115. A_NONE,
  116. A_MOVE,
  117. A_ADD,
  118. A_AND,
  119. A_DIVU,
  120. A_DIVS,
  121. A_MULS,
  122. A_MULU,
  123. A_NEG,
  124. A_NOT,
  125. A_OR,
  126. A_ASR,
  127. A_LSL,
  128. A_LSR,
  129. A_SUB,
  130. A_EOR,
  131. A_ROL,
  132. A_ROR
  133. );
  134. { opcode with extend bits table lookup, used by 64bit cg }
  135. topcg2tasmopx: Array[topcg] of tasmop =
  136. (
  137. A_NONE,
  138. A_NONE,
  139. A_ADDX,
  140. A_NONE,
  141. A_NONE,
  142. A_NONE,
  143. A_NONE,
  144. A_NONE,
  145. A_NEGX,
  146. A_NONE,
  147. A_NONE,
  148. A_NONE,
  149. A_NONE,
  150. A_NONE,
  151. A_SUBX,
  152. A_NONE,
  153. A_NONE,
  154. A_NONE
  155. );
  156. TOpCmp2AsmCond: Array[topcmp] of TAsmCond =
  157. (
  158. C_NONE,
  159. C_EQ,
  160. C_GT,
  161. C_LT,
  162. C_GE,
  163. C_LE,
  164. C_NE,
  165. C_LS,
  166. C_CS,
  167. C_CC,
  168. C_HI
  169. );
  170. function isvalidreference(const ref: treference): boolean;
  171. begin
  172. isvalidreference:=isvalidrefoffset(ref) and
  173. { don't try to generate addressing with symbol and base reg and offset
  174. it might fail in linking stage if the symbol is more than 32k away (KB) }
  175. not (assigned(ref.symbol) and (ref.base <> NR_NO) and (ref.offset <> 0)) and
  176. { coldfire and 68000 cannot handle non-addressregs as bases }
  177. not ((current_settings.cputype in cpu_coldfire+[cpu_mc68000]) and
  178. not isaddressregister(ref.base));
  179. end;
  180. function isvalidrefoffset(const ref: treference): boolean;
  181. begin
  182. isvalidrefoffset := true;
  183. if ref.index <> NR_NO then
  184. begin
  185. // if ref.base <> NR_NO then
  186. // internalerror(2002081401);
  187. if (ref.offset < low(shortint)) or (ref.offset > high(shortint)) then
  188. isvalidrefoffset := false
  189. end
  190. else
  191. begin
  192. if (ref.offset < low(smallint)) or (ref.offset > high(smallint)) then
  193. isvalidrefoffset := false;
  194. end;
  195. end;
  196. {****************************************************************************}
  197. { TCG68K }
  198. {****************************************************************************}
  199. function use_push(const cgpara:tcgpara):boolean;
  200. begin
  201. result:=(not paramanager.use_fixed_stack) and
  202. assigned(cgpara.location) and
  203. (cgpara.location^.loc=LOC_REFERENCE) and
  204. (cgpara.location^.reference.index=NR_STACK_POINTER_REG);
  205. end;
  206. procedure tcg68k.init_register_allocators;
  207. var
  208. reg: TSuperRegister;
  209. address_regs: array of TSuperRegister;
  210. begin
  211. inherited init_register_allocators;
  212. address_regs:=nil;
  213. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  214. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7],
  215. first_int_imreg,[]);
  216. { set up the array of address registers to use }
  217. for reg:=RS_A0 to RS_A6 do
  218. begin
  219. { don't hardwire the frame pointer register, because it can vary between target OS }
  220. if (assigned(current_procinfo) and (current_procinfo.framepointer = NR_FRAME_POINTER_REG)
  221. and (reg = RS_FRAME_POINTER_REG))
  222. or ((reg = RS_PIC_OFFSET_REG) and (tf_static_reg_based in target_info.flags)) then
  223. continue;
  224. setlength(address_regs,length(address_regs)+1);
  225. address_regs[length(address_regs)-1]:=reg;
  226. end;
  227. rg[R_ADDRESSREGISTER]:=trgcpu.create(R_ADDRESSREGISTER,R_SUBWHOLE,
  228. address_regs, first_addr_imreg, []);
  229. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  230. [RS_FP0,RS_FP1,RS_FP2,RS_FP3,RS_FP4,RS_FP5,RS_FP6,RS_FP7],
  231. first_fpu_imreg,[]);
  232. end;
  233. procedure tcg68k.done_register_allocators;
  234. begin
  235. rg[R_INTREGISTER].free;
  236. rg[R_FPUREGISTER].free;
  237. rg[R_ADDRESSREGISTER].free;
  238. inherited done_register_allocators;
  239. end;
  240. procedure tcg68k.a_load_reg_cgpara(list : TAsmList;size : tcgsize;r : tregister;const cgpara : tcgpara);
  241. var
  242. pushsize : tcgsize;
  243. ref : treference;
  244. begin
  245. { it's probably necessary to port this from x86 later, or provide an m68k solution (KB) }
  246. { TODO: FIX ME! check_register_size()}
  247. // check_register_size(size,r);
  248. if use_push(cgpara) then
  249. begin
  250. cgpara.check_simple_location;
  251. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  252. pushsize:=cgpara.location^.size
  253. else
  254. pushsize:=int_cgsize(cgpara.alignment);
  255. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment, []);
  256. ref.direction := dir_dec;
  257. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[pushsize],makeregsize(list,r,pushsize),ref));
  258. end
  259. else
  260. inherited a_load_reg_cgpara(list,size,r,cgpara);
  261. end;
  262. procedure tcg68k.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const cgpara : tcgpara);
  263. var
  264. pushsize : tcgsize;
  265. ref : treference;
  266. begin
  267. if use_push(cgpara) then
  268. begin
  269. cgpara.check_simple_location;
  270. if tcgsize2size[cgpara.location^.size]>cgpara.alignment then
  271. pushsize:=cgpara.location^.size
  272. else
  273. pushsize:=int_cgsize(cgpara.alignment);
  274. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment, []);
  275. ref.direction := dir_dec;
  276. a_load_const_ref(list, pushsize, a, ref);
  277. end
  278. else
  279. inherited a_load_const_cgpara(list,size,a,cgpara);
  280. end;
  281. procedure tcg68k.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const cgpara : tcgpara);
  282. procedure pushdata(paraloc:pcgparalocation;ofs:tcgint);
  283. var
  284. pushsize : tcgsize;
  285. tmpreg : tregister;
  286. href : treference;
  287. ref : treference;
  288. begin
  289. if not assigned(paraloc) then
  290. exit;
  291. if (paraloc^.loc<>LOC_REFERENCE) or
  292. (paraloc^.reference.index<>NR_STACK_POINTER_REG) or
  293. (tcgsize2size[paraloc^.size]>sizeof(tcgint)) then
  294. internalerror(200501162);
  295. { Pushes are needed in reverse order, add the size of the
  296. current location to the offset where to load from. This
  297. prevents wrong calculations for the last location when
  298. the size is not a power of 2 }
  299. if assigned(paraloc^.next) then
  300. pushdata(paraloc^.next,ofs+tcgsize2size[paraloc^.size]);
  301. { Push the data starting at ofs }
  302. href:=r;
  303. inc(href.offset,ofs);
  304. fixref(list,href,false);
  305. if tcgsize2size[paraloc^.size]>cgpara.alignment then
  306. pushsize:=paraloc^.size
  307. else
  308. pushsize:=int_cgsize(cgpara.alignment);
  309. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, tcgsize2size[pushsize], []);
  310. ref.direction := dir_dec;
  311. a_load_ref_ref(list,int_cgsize(tcgsize2size[paraloc^.size]),pushsize,href,ref);
  312. end;
  313. var
  314. len : tcgint;
  315. ofs : tcgint;
  316. href : treference;
  317. begin
  318. { cgpara.size=OS_NO requires a copy on the stack }
  319. if use_push(cgpara) then
  320. begin
  321. { Record copy? }
  322. if (cgpara.size in [OS_NO,OS_F64]) or (size in [OS_NO,OS_F64]) then
  323. begin
  324. //list.concat(tai_comment.create(strpnew('a_load_ref_cgpara: g_concatcopy')));
  325. cgpara.check_simple_location;
  326. len:=align(cgpara.intsize,cgpara.alignment);
  327. g_stackpointer_alloc(list,len);
  328. ofs:=0;
  329. if (cgpara.intsize<cgpara.alignment) then
  330. ofs:=cgpara.alignment-cgpara.intsize;
  331. reference_reset_base(href,NR_STACK_POINTER_REG,ofs,cgpara.alignment,[]);
  332. g_concatcopy(list,r,href,cgpara.intsize);
  333. end
  334. else
  335. begin
  336. if tcgsize2size[cgpara.size]<>tcgsize2size[size] then
  337. internalerror(200501161);
  338. { We need to push the data in reverse order,
  339. therefore we use a recursive algorithm }
  340. pushdata(cgpara.location,0);
  341. end
  342. end
  343. else
  344. inherited a_load_ref_cgpara(list,size,r,cgpara);
  345. end;
  346. procedure tcg68k.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const cgpara : tcgpara);
  347. var
  348. tmpref : treference;
  349. begin
  350. { 68k always passes arguments on the stack }
  351. if use_push(cgpara) then
  352. begin
  353. //list.concat(tai_comment.create(strpnew('a_loadaddr_ref_cgpara: PEA')));
  354. cgpara.check_simple_location;
  355. tmpref:=r;
  356. fixref(list,tmpref,false);
  357. list.concat(taicpu.op_ref(A_PEA,S_NO,tmpref));
  358. end
  359. else
  360. inherited a_loadaddr_ref_cgpara(list,r,cgpara);
  361. end;
  362. function tcg68k.fixref(list: TAsmList; var ref: treference; fullyresolve: boolean): boolean;
  363. var
  364. hreg : tregister;
  365. href : treference;
  366. instr : taicpu;
  367. begin
  368. result:=false;
  369. hreg:=NR_NO;
  370. { NOTE: we don't have to fixup scaling in this function, because the memnode
  371. won't generate scaling on CPUs which don't support it }
  372. if (tf_static_reg_based in target_info.flags) and assigned(ref.symbol) and (ref.base=NR_NO) then
  373. fullyresolve:=true;
  374. { first, deal with the symbol, if we have an index or base register.
  375. in theory, the '020+ could deal with these, but it's better to avoid
  376. long displacements on most members of the 68k family anyway }
  377. if assigned(ref.symbol) and ((ref.base<>NR_NO) or (ref.index<>NR_NO)) then
  378. begin
  379. //list.concat(tai_comment.create(strpnew('fixref: symbol with base or index')));
  380. hreg:=getaddressregister(list);
  381. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment,ref.volatility);
  382. if (tf_static_reg_based in target_info.flags) and (ref.base=NR_NO) then
  383. begin
  384. if ref.symbol.typ in [AT_DATA,AT_DATA_FORCEINDIRECT,AT_DATA_NOINDIRECT] then
  385. href.base:=NR_PIC_OFFSET_REG
  386. else
  387. href.base:=NR_PC;
  388. end;
  389. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  390. ref.offset:=0;
  391. ref.symbol:=nil;
  392. { if we have unused base or index, try to use it, otherwise fold the existing base,
  393. also handle the case where the base might be a data register. }
  394. if ref.base=NR_NO then
  395. ref.base:=hreg
  396. else
  397. if (ref.index=NR_NO) and not isintregister(ref.base) then
  398. ref.index:=hreg
  399. else
  400. begin
  401. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,hreg));
  402. ref.base:=hreg;
  403. end;
  404. { at this point we have base + (optional) index * scale }
  405. end;
  406. { deal with the case if our base is a dataregister }
  407. if (ref.base<>NR_NO) and not isaddressregister(ref.base) then
  408. begin
  409. hreg:=getaddressregister(list);
  410. if isaddressregister(ref.index) and (ref.scalefactor < 2) then
  411. begin
  412. //list.concat(tai_comment.create(strpnew('fixref: base is dX, resolving with reverse regs')));
  413. reference_reset_base(href,ref.index,0,ref.alignment,ref.volatility);
  414. href.index:=ref.base;
  415. { we can fold in an 8 bit offset "for free" }
  416. if isvalue8bit(ref.offset) then
  417. begin
  418. href.offset:=ref.offset;
  419. ref.offset:=0;
  420. end;
  421. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  422. ref.base:=hreg;
  423. ref.index:=NR_NO;
  424. result:=true;
  425. end
  426. else
  427. begin
  428. //list.concat(tai_comment.create(strpnew('fixref: base is dX, can''t resolve with reverse regs')));
  429. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  430. add_move_instruction(instr);
  431. list.concat(instr);
  432. ref.base:=hreg;
  433. result:=true;
  434. end;
  435. end;
  436. { deal with large offsets on non-020+ }
  437. if not (current_settings.cputype in cpu_mc68020p) then
  438. begin
  439. if ((ref.index<>NR_NO) and not isvalue8bit(ref.offset)) or
  440. ((ref.base<>NR_NO) and not isvalue16bit(ref.offset)) then
  441. begin
  442. //list.concat(tai_comment.create(strpnew('fixref: handling large offsets')));
  443. { if we have a temp register from above, we can just add to it }
  444. if hreg=NR_NO then
  445. hreg:=getaddressregister(list);
  446. if isvalue16bit(ref.offset) then
  447. begin
  448. reference_reset_base(href,ref.base,ref.offset,ref.alignment,ref.volatility);
  449. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  450. end
  451. else
  452. begin
  453. instr:=taicpu.op_reg_reg(A_MOVE,S_L,ref.base,hreg);
  454. add_move_instruction(instr);
  455. list.concat(instr);
  456. list.concat(taicpu.op_const_reg(A_ADD,S_L,ref.offset,hreg));
  457. end;
  458. ref.offset:=0;
  459. ref.base:=hreg;
  460. result:=true;
  461. end;
  462. end;
  463. { fully resolve the reference to an address register, if we're told to do so
  464. and there's a reason to do so }
  465. if fullyresolve and
  466. ((ref.index<>NR_NO) or assigned(ref.symbol) or (ref.offset<>0)) then
  467. begin
  468. //list.concat(tai_comment.create(strpnew('fixref: fully resolve to register')));
  469. if hreg=NR_NO then
  470. hreg:=getaddressregister(list);
  471. if (tf_static_reg_based in target_info.flags) and (ref.base=NR_NO) then
  472. begin
  473. if ref.symbol.typ in [AT_DATA,AT_DATA_FORCEINDIRECT,AT_DATA_NOINDIRECT] then
  474. ref.base:=NR_PIC_OFFSET_REG
  475. else
  476. ref.base:=NR_PC;
  477. end;
  478. list.concat(taicpu.op_ref_reg(A_LEA,S_L,ref,hreg));
  479. ref.base:=hreg;
  480. ref.index:=NR_NO;
  481. ref.scalefactor:=1;
  482. ref.symbol:=nil;
  483. ref.offset:=0;
  484. result:=true;
  485. end;
  486. end;
  487. procedure tcg68k.call_rtl_mul_const_reg(list:tasmlist;size:tcgsize;a:tcgint;reg:tregister;const name:string);
  488. var
  489. paraloc1,paraloc2: tcgpara;
  490. pd : tprocdef;
  491. begin
  492. pd:=search_system_proc(name);
  493. paraloc1.init;
  494. paraloc2.init;
  495. paramanager.getintparaloc(list,pd,1,paraloc1);
  496. paramanager.getintparaloc(list,pd,2,paraloc2);
  497. a_load_const_cgpara(list,size,a,paraloc2);
  498. a_load_reg_cgpara(list,OS_32,reg,paraloc1);
  499. paramanager.freecgpara(list,paraloc2);
  500. paramanager.freecgpara(list,paraloc1);
  501. g_call(list,name);
  502. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  503. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg);
  504. paraloc2.done;
  505. paraloc1.done;
  506. end;
  507. procedure tcg68k.call_rtl_mul_reg_reg(list:tasmlist;reg1,reg2:tregister;const name:string);
  508. var
  509. paraloc1,paraloc2: tcgpara;
  510. pd : tprocdef;
  511. begin
  512. pd:=search_system_proc(name);
  513. paraloc1.init;
  514. paraloc2.init;
  515. paramanager.getintparaloc(list,pd,1,paraloc1);
  516. paramanager.getintparaloc(list,pd,2,paraloc2);
  517. a_load_reg_cgpara(list,OS_32,reg1,paraloc2);
  518. a_load_reg_cgpara(list,OS_32,reg2,paraloc1);
  519. paramanager.freecgpara(list,paraloc2);
  520. paramanager.freecgpara(list,paraloc1);
  521. g_call(list,name);
  522. cg.a_reg_alloc(list,NR_FUNCTION_RESULT_REG);
  523. cg.a_load_reg_reg(list,OS_32,OS_32,NR_FUNCTION_RESULT_REG,reg2);
  524. paraloc2.done;
  525. paraloc1.done;
  526. end;
  527. procedure tcg68k.a_call_name(list : TAsmList;const s : string; weak: boolean);
  528. var
  529. sym: tasmsymbol;
  530. const
  531. jmp_inst: array[boolean] of tasmop = ( A_JSR, A_BSR );
  532. begin
  533. if not(weak) then
  534. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  535. else
  536. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  537. list.concat(taicpu.op_sym(jmp_inst[tf_code_small in target_info.flags],S_NO,sym));
  538. end;
  539. procedure tcg68k.a_call_reg(list : TAsmList;reg: tregister);
  540. var
  541. tmpref : treference;
  542. tmpreg : tregister;
  543. instr : taicpu;
  544. begin
  545. if isaddressregister(reg) then
  546. begin
  547. { if we have an address register, we can jump to the address directly }
  548. reference_reset_base(tmpref,reg,0,4,[]);
  549. end
  550. else
  551. begin
  552. { if we have a data register, we need to move it to an address register first }
  553. tmpreg:=getaddressregister(list);
  554. reference_reset_base(tmpref,tmpreg,0,4,[]);
  555. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,tmpreg);
  556. add_move_instruction(instr);
  557. list.concat(instr);
  558. end;
  559. list.concat(taicpu.op_ref(A_JSR,S_NO,tmpref));
  560. end;
  561. procedure tcg68k.a_load_const_reg(list : TAsmList;size : tcgsize;a : tcgint;register : tregister);
  562. var
  563. opsize: topsize;
  564. begin
  565. opsize:=tcgsize2opsize[size];
  566. if isaddressregister(register) then
  567. begin
  568. { an m68k manual I have recommends SUB Ax,Ax to be used instead of CLR for address regs }
  569. { Premature optimization is the root of all evil - this code breaks spilling if the
  570. register contains a spilled regvar, eg. a Pointer which is set to nil, then random
  571. havoc happens... This is kept here for reference now, to allow fixing of the spilling
  572. later. Most of the optimizations below here could be moved to the optimizer. (KB) }
  573. {if a = 0 then
  574. list.concat(taicpu.op_reg_reg(A_SUB,S_L,register,register))
  575. else}
  576. { ISA B/C Coldfire has MOV3Q which can move -1 or 1..7 to any reg }
  577. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  578. ((longint(a) = -1) or ((longint(a) > 0) and (longint(a) < 8))) then
  579. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  580. else
  581. { MOVEA.W will sign extend the value in the dest. reg to full 32 bits
  582. (specific to Ax regs only) }
  583. if isvalue16bit(a) then
  584. list.concat(taicpu.op_const_reg(A_MOVEA,S_W,longint(a),register))
  585. else
  586. list.concat(taicpu.op_const_reg(A_MOVEA,S_L,longint(a),register));
  587. end
  588. else
  589. if a = 0 then
  590. list.concat(taicpu.op_reg(A_CLR,S_L,register))
  591. else
  592. begin
  593. { Prefer MOV3Q if applicable, it allows replacement spilling for register }
  594. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  595. ((longint(a)=-1) or ((longint(a)>0) and (longint(a)<8))) then
  596. list.concat(taicpu.op_const_reg(A_MOV3Q,S_L,longint(a),register))
  597. else if (longint(a) >= low(shortint)) and (longint(a) <= high(shortint)) then
  598. list.concat(taicpu.op_const_reg(A_MOVEQ,S_L,longint(a),register))
  599. else
  600. begin
  601. { ISA B/C Coldfire has sign extend/zero extend moves }
  602. if (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  603. (size in [OS_16, OS_8, OS_S16, OS_S8]) and
  604. ((longint(a) >= low(smallint)) and (longint(a) <= high(smallint))) then
  605. begin
  606. if size in [OS_16, OS_8] then
  607. list.concat(taicpu.op_const_reg(A_MVZ,opsize,longint(a),register))
  608. else
  609. list.concat(taicpu.op_const_reg(A_MVS,opsize,longint(a),register));
  610. end
  611. else
  612. begin
  613. { clear the register first, for unsigned and positive values, so
  614. we don't need to zero extend after }
  615. if (size in [OS_16,OS_8]) or
  616. ((size in [OS_S16,OS_S8]) and (a > 0)) then
  617. list.concat(taicpu.op_reg(A_CLR,S_L,register));
  618. list.concat(taicpu.op_const_reg(A_MOVE,opsize,longint(a),register));
  619. { only sign extend if we need to, zero extension is not necessary because the CLR.L above }
  620. if (size in [OS_S16,OS_S8]) and (a < 0) then
  621. sign_extend(list,size,register);
  622. end;
  623. end;
  624. end;
  625. end;
  626. procedure tcg68k.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  627. var
  628. hreg : tregister;
  629. href : treference;
  630. begin
  631. if needs_unaligned(ref.alignment,tosize) then
  632. begin
  633. inherited;
  634. exit;
  635. end;
  636. a:=longint(a);
  637. href:=ref;
  638. fixref(list,href,false);
  639. if (a=0) and not (current_settings.cputype = cpu_mc68000) then
  640. list.concat(taicpu.op_ref(A_CLR,tcgsize2opsize[tosize],href))
  641. else if (tcgsize2opsize[tosize]=S_L) and
  642. (current_settings.cputype in [cpu_isa_b,cpu_isa_c,cpu_cfv4e]) and
  643. ((a=-1) or ((a>0) and (a<8))) then
  644. list.concat(taicpu.op_const_ref(A_MOV3Q,S_L,a,href))
  645. { for coldfire we need to go through a temporary register if we have a
  646. offset, index or symbol given }
  647. else if (current_settings.cputype in cpu_coldfire) and
  648. (
  649. (href.offset<>0) or
  650. { TODO : check whether we really need this second condition }
  651. (href.index<>NR_NO) or
  652. assigned(href.symbol)
  653. ) then
  654. begin
  655. hreg:=getintregister(list,tosize);
  656. a_load_const_reg(list,tosize,a,hreg);
  657. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  658. end
  659. else
  660. { loading via a register is almost always faster if the value is small.
  661. (with the 68040 being the only notable exception, so maybe disable
  662. this on a '040? but the difference is minor) it also results in shorter
  663. code. (KB) }
  664. if isvalue8bit(a) and (tcgsize2opsize[tosize] = S_L) then
  665. begin
  666. hreg:=getintregister(list,OS_INT);
  667. a_load_const_reg(list,OS_INT,a,hreg); // this will use moveq et.al.
  668. list.concat(taicpu.op_reg_ref(A_MOVE,tcgsize2opsize[tosize],hreg,href));
  669. end
  670. else
  671. list.concat(taicpu.op_const_ref(A_MOVE,tcgsize2opsize[tosize],longint(a),href));
  672. end;
  673. procedure tcg68k.a_load_reg_ref(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  674. var
  675. href : treference;
  676. hreg : tregister;
  677. begin
  678. if needs_unaligned(ref.alignment,tosize) then
  679. begin
  680. //list.concat(tai_comment.create(strpnew('a_load_reg_ref calling unaligned')));
  681. a_load_reg_ref_unaligned(list,fromsize,tosize,register,ref);
  682. exit;
  683. end;
  684. href := ref;
  685. hreg := register;
  686. fixref(list,href,false);
  687. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  688. begin
  689. hreg:=getintregister(list,tosize);
  690. a_load_reg_reg(list,fromsize,tosize,register,hreg);
  691. end;
  692. { move to destination reference }
  693. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,href));
  694. end;
  695. procedure tcg68k.a_load_reg_ref_unaligned(list : TAsmList;fromsize,tosize : tcgsize;register : tregister;const ref : treference);
  696. var
  697. tmpref : treference;
  698. tmpreg,
  699. tmpreg2 : tregister;
  700. begin
  701. if not needs_unaligned(ref.alignment,tosize) then
  702. begin
  703. a_load_reg_ref(list,fromsize,tosize,register,ref);
  704. exit;
  705. end;
  706. list.concat(tai_comment.create(strpnew('a_load_reg_ref_unaligned: generating unaligned store')));
  707. tmpreg2:=getaddressregister(list);
  708. tmpref:=ref;
  709. inc(tmpref.offset,tcgsize2size[tosize]-1);
  710. a_loadaddr_ref_reg(list,tmpref,tmpreg2);
  711. reference_reset_base(tmpref,tmpreg2,0,1,ref.volatility);
  712. tmpref.direction:=dir_none;
  713. tmpreg:=getintregister(list,tosize);
  714. a_load_reg_reg(list,fromsize,tosize,register,tmpreg);
  715. case tosize of
  716. OS_16,OS_S16:
  717. begin
  718. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  719. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  720. tmpref.direction:=dir_dec;
  721. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  722. end;
  723. OS_32,OS_S32:
  724. begin
  725. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  726. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  727. tmpref.direction:=dir_dec;
  728. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  729. list.concat(taicpu.op_reg(A_SWAP,S_L,tmpreg));
  730. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  731. list.concat(taicpu.op_const_reg(A_LSR,S_W,8,tmpreg));
  732. list.concat(taicpu.op_reg_ref(A_MOVE,S_B,tmpreg,tmpref));
  733. end
  734. else
  735. internalerror(2016052201);
  736. end;
  737. end;
  738. procedure tcg68k.a_load_ref_ref(list : TAsmList;fromsize,tosize : tcgsize;const sref : treference;const dref : treference);
  739. var
  740. aref: treference;
  741. bref: treference;
  742. usetemp: boolean;
  743. hreg: TRegister;
  744. begin
  745. usetemp:=TCGSize2OpSize[fromsize]<>TCGSize2OpSize[tosize];
  746. usetemp:=usetemp or (needs_unaligned(sref.alignment,fromsize) or needs_unaligned(dref.alignment,tosize));
  747. aref := sref;
  748. bref := dref;
  749. if usetemp then
  750. begin
  751. { if we need to change the size then always use a temporary register }
  752. hreg:=getintregister(list,fromsize);
  753. if needs_unaligned(sref.alignment,fromsize) then
  754. a_load_ref_reg_unaligned(list,fromsize,tosize,sref,hreg)
  755. else
  756. begin
  757. fixref(list,aref,false);
  758. list.concat(taicpu.op_ref_reg(A_MOVE,TCGSize2OpSize[fromsize],aref,hreg));
  759. sign_extend(list,fromsize,tosize,hreg);
  760. end;
  761. if needs_unaligned(dref.alignment,tosize) then
  762. a_load_reg_ref_unaligned(list,tosize,tosize,hreg,dref)
  763. else
  764. begin
  765. { if we use a temp register, we don't need to fully resolve
  766. the dest ref, not even on coldfire }
  767. fixref(list,bref,false);
  768. list.concat(taicpu.op_reg_ref(A_MOVE,TCGSize2OpSize[tosize],hreg,bref));
  769. end;
  770. end
  771. else
  772. begin
  773. fixref(list,aref,false);
  774. fixref(list,bref,current_settings.cputype in cpu_coldfire);
  775. list.concat(taicpu.op_ref_ref(A_MOVE,TCGSize2OpSize[fromsize],aref,bref));
  776. end;
  777. end;
  778. procedure tcg68k.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  779. var
  780. instr : taicpu;
  781. hreg : tregister;
  782. opsize : topsize;
  783. begin
  784. { move to destination register }
  785. opsize:=TCGSize2OpSize[fromsize];
  786. if isaddressregister(reg2) and not (opsize in [S_L]) then
  787. begin
  788. hreg:=cg.getintregister(list,OS_ADDR);
  789. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,hreg);
  790. add_move_instruction(instr);
  791. list.concat(instr);
  792. sign_extend(list,fromsize,hreg);
  793. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg2));
  794. end
  795. else
  796. begin
  797. if not isregoverlap(reg1,reg2) then
  798. begin
  799. instr:=taicpu.op_reg_reg(A_MOVE,opsize,reg1,reg2);
  800. add_move_instruction(instr);
  801. list.concat(instr);
  802. end;
  803. sign_extend(list,fromsize,tosize,reg2);
  804. end;
  805. end;
  806. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  807. var
  808. href : treference;
  809. hreg : tregister;
  810. size : tcgsize;
  811. opsize: topsize;
  812. needsext: boolean;
  813. begin
  814. if needs_unaligned(ref.alignment,fromsize) then
  815. begin
  816. //list.concat(tai_comment.create(strpnew('a_load_ref_reg calling unaligned')));
  817. a_load_ref_reg_unaligned(list,fromsize,tosize,ref,register);
  818. exit;
  819. end;
  820. href:=ref;
  821. fixref(list,href,false);
  822. needsext:=tcgsize2size[fromsize]<tcgsize2size[tosize];
  823. if needsext then
  824. size:=fromsize
  825. else
  826. size:=tosize;
  827. opsize:=TCGSize2OpSize[size];
  828. if isaddressregister(register) and not (opsize in [S_L]) then
  829. hreg:=getintregister(list,OS_ADDR)
  830. else
  831. hreg:=register;
  832. if needsext and (CPUM68K_HAS_MVSMVZ in cpu_capabilities[current_settings.cputype]) and not (opsize in [S_L]) then
  833. begin
  834. if fromsize in [OS_S8,OS_S16] then
  835. list.concat(taicpu.op_ref_reg(A_MVS,opsize,href,hreg))
  836. else if fromsize in [OS_8,OS_16] then
  837. list.concat(taicpu.op_ref_reg(A_MVZ,opsize,href,hreg))
  838. else
  839. internalerror(2016050502);
  840. end
  841. else
  842. begin
  843. if needsext and (fromsize in [OS_8,OS_16]) then
  844. begin
  845. //list.concat(tai_comment.create(strpnew('a_load_ref_reg: zero ext')));
  846. a_load_const_reg(list,OS_32,0,hreg);
  847. needsext:=false;
  848. end;
  849. list.concat(taicpu.op_ref_reg(A_MOVE,opsize,href,hreg));
  850. if needsext then
  851. sign_extend(list,size,hreg);
  852. end;
  853. if hreg<>register then
  854. a_load_reg_reg(list,OS_ADDR,OS_ADDR,hreg,register);
  855. end;
  856. procedure tcg68k.a_load_ref_reg_unaligned(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  857. var
  858. tmpref : treference;
  859. tmpreg,
  860. tmpreg2 : tregister;
  861. begin
  862. if not needs_unaligned(ref.alignment,fromsize) then
  863. begin
  864. a_load_ref_reg(list,fromsize,tosize,ref,register);
  865. exit;
  866. end;
  867. list.concat(tai_comment.create(strpnew('a_load_ref_reg_unaligned: generating unaligned load')));
  868. tmpreg2:=getaddressregister(list);
  869. a_loadaddr_ref_reg(list,ref,tmpreg2);
  870. reference_reset_base(tmpref,tmpreg2,0,1,ref.volatility);
  871. tmpref.direction:=dir_inc;
  872. if isaddressregister(register) then
  873. tmpreg:=getintregister(list,OS_ADDR)
  874. else
  875. tmpreg:=register;
  876. case fromsize of
  877. OS_16,OS_S16:
  878. begin
  879. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  880. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  881. tmpref.direction:=dir_none;
  882. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  883. sign_extend(list,fromsize,tmpreg);
  884. end;
  885. OS_32,OS_S32:
  886. begin
  887. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  888. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  889. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  890. list.concat(taicpu.op_reg(A_SWAP,S_L,tmpreg));
  891. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  892. list.concat(taicpu.op_const_reg(A_LSL,S_W,8,tmpreg));
  893. tmpref.direction:=dir_none;
  894. list.concat(taicpu.op_ref_reg(A_MOVE,S_B,tmpref,tmpreg));
  895. end
  896. else
  897. internalerror(2016052103);
  898. end;
  899. if tmpreg<>register then
  900. a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpreg,register);
  901. end;
  902. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  903. var
  904. href : treference;
  905. hreg : tregister;
  906. begin
  907. href:=ref;
  908. fixref(list, href, false);
  909. if not isaddressregister(r) then
  910. begin
  911. hreg:=getaddressregister(list);
  912. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  913. a_load_reg_reg(list, OS_ADDR, OS_ADDR, hreg, r);
  914. end
  915. else
  916. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  917. end;
  918. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  919. var
  920. instr : taicpu;
  921. begin
  922. instr:=taicpu.op_reg_reg(A_FMOVE,fpuregopsize,reg1,reg2);
  923. add_move_instruction(instr);
  924. list.concat(instr);
  925. end;
  926. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  927. var
  928. opsize : topsize;
  929. href : treference;
  930. begin
  931. opsize := tcgsize2opsize[fromsize];
  932. href := ref;
  933. fixref(list,href,current_settings.fputype = fpu_coldfire);
  934. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  935. end;
  936. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  937. var
  938. opsize : topsize;
  939. href : treference;
  940. begin
  941. opsize := tcgsize2opsize[tosize];
  942. href := ref;
  943. fixref(list,href,current_settings.fputype = fpu_coldfire);
  944. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg,href));
  945. end;
  946. procedure tcg68k.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const reg : tregister;const cgpara : tcgpara);
  947. var
  948. ref : treference;
  949. begin
  950. if use_push(cgpara) and (current_settings.fputype in [fpu_68881,fpu_coldfire]) then
  951. begin
  952. cgpara.check_simple_location;
  953. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment, []);
  954. ref.direction := dir_dec;
  955. list.concat(taicpu.op_reg_ref(A_FMOVE,tcgsize2opsize[cgpara.location^.size],reg,ref));
  956. end
  957. else
  958. inherited a_loadfpu_reg_cgpara(list,size,reg,cgpara);
  959. end;
  960. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  961. var
  962. href, href2 : treference;
  963. freg : tregister;
  964. begin
  965. if current_settings.fputype = fpu_soft then
  966. case cgpara.location^.loc of
  967. LOC_REFERENCE,LOC_CREFERENCE:
  968. begin
  969. case size of
  970. OS_F64:
  971. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  972. OS_F32:
  973. a_load_ref_cgpara(list,size,ref,cgpara);
  974. else
  975. internalerror(2013021201);
  976. end;
  977. end;
  978. else
  979. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  980. end
  981. else
  982. if use_push(cgpara) and (current_settings.fputype in [fpu_68881,fpu_coldfire]) then
  983. begin
  984. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara copy')));
  985. cgpara.check_simple_location;
  986. reference_reset_base(href, NR_STACK_POINTER_REG, 0, cgpara.alignment, []);
  987. href.direction := dir_dec;
  988. case size of
  989. OS_F64:
  990. begin
  991. href2:=ref;
  992. inc(href2.offset,8);
  993. fixref(list,href2,true);
  994. href2.direction := dir_dec;
  995. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  996. cg.a_load_ref_ref(list,OS_32,OS_32,href2,href);
  997. end;
  998. OS_F32:
  999. cg.a_load_ref_ref(list,OS_32,OS_32,ref,href);
  1000. else
  1001. internalerror(2017052110);
  1002. end;
  1003. end
  1004. else
  1005. begin
  1006. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara inherited')));
  1007. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  1008. end;
  1009. end;
  1010. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  1011. var
  1012. scratch_reg : tregister;
  1013. scratch_reg2: tregister;
  1014. opcode : tasmop;
  1015. begin
  1016. optimize_op_const(size, op, a);
  1017. opcode := topcg2tasmop[op];
  1018. case op of
  1019. OP_NONE :
  1020. begin
  1021. { Opcode is optimized away }
  1022. end;
  1023. OP_MOVE :
  1024. begin
  1025. { Optimized, replaced with a simple load }
  1026. a_load_const_reg(list,size,a,reg);
  1027. end;
  1028. OP_ADD,
  1029. OP_SUB:
  1030. begin
  1031. { add/sub works the same way, so have it unified here }
  1032. if (a >= 1) and (a <= 8) then
  1033. if (op = OP_ADD) then
  1034. opcode:=A_ADDQ
  1035. else
  1036. opcode:=A_SUBQ;
  1037. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  1038. end;
  1039. OP_AND,
  1040. OP_OR,
  1041. OP_XOR:
  1042. begin
  1043. scratch_reg := force_to_dataregister(list, size, reg);
  1044. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1045. move_if_needed(list, size, scratch_reg, reg);
  1046. end;
  1047. OP_DIV,
  1048. OP_IDIV:
  1049. begin
  1050. internalerror(20020816);
  1051. end;
  1052. OP_MUL,
  1053. OP_IMUL:
  1054. begin
  1055. { NOTE: better have this as fast as possible on every CPU in all cases,
  1056. because the compiler uses OP_IMUL for array indexing... (KB) }
  1057. { ColdFire doesn't support MULS/MULU <imm>,dX }
  1058. if current_settings.cputype in cpu_coldfire then
  1059. begin
  1060. { move const to a register first }
  1061. scratch_reg := getintregister(list,OS_INT);
  1062. a_load_const_reg(list, size, a, scratch_reg);
  1063. { do the multiplication }
  1064. scratch_reg2 := force_to_dataregister(list, size, reg);
  1065. sign_extend(list, size, scratch_reg2);
  1066. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  1067. { move the value back to the original register }
  1068. move_if_needed(list, size, scratch_reg2, reg);
  1069. end
  1070. else
  1071. begin
  1072. if current_settings.cputype in cpu_mc68020p then
  1073. begin
  1074. { do the multiplication }
  1075. scratch_reg := force_to_dataregister(list, size, reg);
  1076. sign_extend(list, size, scratch_reg);
  1077. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1078. { move the value back to the original register }
  1079. move_if_needed(list, size, scratch_reg, reg);
  1080. end
  1081. else
  1082. { Fallback branch, plain 68000 for now }
  1083. if not optimize_const_mul_to_shift_sub_add(list, 5, a, size, reg) then
  1084. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1085. if op = OP_MUL then
  1086. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1087. else
  1088. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1089. end;
  1090. end;
  1091. OP_ROL,
  1092. OP_ROR,
  1093. OP_SAR,
  1094. OP_SHL,
  1095. OP_SHR :
  1096. begin
  1097. scratch_reg := force_to_dataregister(list, size, reg);
  1098. sign_extend(list, size, scratch_reg);
  1099. { some special cases which can generate smarter code
  1100. using the SWAP instruction }
  1101. if (a = 16) then
  1102. begin
  1103. if (op = OP_SHL) then
  1104. begin
  1105. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1106. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1107. end
  1108. else if (op = OP_SHR) then
  1109. begin
  1110. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1111. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1112. end
  1113. else if (op = OP_SAR) then
  1114. begin
  1115. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1116. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1117. end
  1118. else if (op = OP_ROR) or (op = OP_ROL) then
  1119. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1120. end
  1121. else if (a >= 1) and (a <= 8) then
  1122. begin
  1123. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1124. end
  1125. else if (a >= 9) and (a < 16) then
  1126. begin
  1127. { Use two ops instead of const -> reg + shift with reg, because
  1128. this way is the same in length and speed but has less register
  1129. pressure }
  1130. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1131. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1132. end
  1133. else
  1134. begin
  1135. { move const to a register first }
  1136. scratch_reg2 := getintregister(list,OS_INT);
  1137. a_load_const_reg(list, size, a, scratch_reg2);
  1138. { do the operation }
  1139. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1140. end;
  1141. { move the value back to the original register }
  1142. move_if_needed(list, size, scratch_reg, reg);
  1143. end;
  1144. else
  1145. internalerror(20020729);
  1146. end;
  1147. end;
  1148. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1149. var
  1150. opcode: tasmop;
  1151. opsize: topsize;
  1152. href : treference;
  1153. hreg : tregister;
  1154. begin
  1155. optimize_op_const(size, op, a);
  1156. opcode := topcg2tasmop[op];
  1157. opsize := TCGSize2OpSize[size];
  1158. { on ColdFire all arithmetic operations are only possible on 32bit }
  1159. if needs_unaligned(ref.alignment,size) or
  1160. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1161. and not (op in [OP_NONE,OP_MOVE])) then
  1162. begin
  1163. inherited;
  1164. exit;
  1165. end;
  1166. case op of
  1167. OP_NONE :
  1168. begin
  1169. { opcode was optimized away }
  1170. end;
  1171. OP_MOVE :
  1172. begin
  1173. { Optimized, replaced with a simple load }
  1174. a_load_const_ref(list,size,a,ref);
  1175. end;
  1176. OP_AND,
  1177. OP_OR,
  1178. OP_XOR :
  1179. begin
  1180. //list.concat(tai_comment.create(strpnew('a_op_const_ref: bitwise')));
  1181. hreg:=getintregister(list,size);
  1182. a_load_const_reg(list,size,a,hreg);
  1183. href:=ref;
  1184. fixref(list,href,false);
  1185. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1186. end;
  1187. OP_ADD,
  1188. OP_SUB :
  1189. begin
  1190. href:=ref;
  1191. { add/sub works the same way, so have it unified here }
  1192. if (a >= 1) and (a <= 8) then
  1193. begin
  1194. fixref(list,href,false);
  1195. if (op = OP_ADD) then
  1196. opcode:=A_ADDQ
  1197. else
  1198. opcode:=A_SUBQ;
  1199. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1200. end
  1201. else
  1202. if not(current_settings.cputype in cpu_coldfire) then
  1203. begin
  1204. fixref(list,href,false);
  1205. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1206. end
  1207. else
  1208. { on ColdFire, ADDI/SUBI cannot act on memory
  1209. so we can only go through a register }
  1210. inherited;
  1211. end;
  1212. else begin
  1213. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1214. inherited;
  1215. end;
  1216. end;
  1217. end;
  1218. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1219. var
  1220. hreg1, hreg2: tregister;
  1221. opcode : tasmop;
  1222. opsize : topsize;
  1223. begin
  1224. opcode := topcg2tasmop[op];
  1225. if current_settings.cputype in cpu_coldfire then
  1226. opsize := S_L
  1227. else
  1228. opsize := TCGSize2OpSize[size];
  1229. case op of
  1230. OP_ADD,
  1231. OP_SUB:
  1232. begin
  1233. if current_settings.cputype in cpu_coldfire then
  1234. begin
  1235. { operation only allowed only a longword }
  1236. sign_extend(list, size, src);
  1237. sign_extend(list, size, dst);
  1238. end;
  1239. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1240. end;
  1241. OP_AND,OP_OR,
  1242. OP_SAR,OP_SHL,
  1243. OP_SHR,OP_XOR:
  1244. begin
  1245. { load to data registers }
  1246. hreg1 := force_to_dataregister(list, size, src);
  1247. hreg2 := force_to_dataregister(list, size, dst);
  1248. if current_settings.cputype in cpu_coldfire then
  1249. begin
  1250. { operation only allowed only a longword }
  1251. {!***************************************
  1252. in the case of shifts, the value to
  1253. shift by, should already be valid, so
  1254. no need to sign extend the value
  1255. !
  1256. }
  1257. if op in [OP_AND,OP_OR,OP_XOR] then
  1258. sign_extend(list, size, hreg1);
  1259. sign_extend(list, size, hreg2);
  1260. end;
  1261. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1262. { move back result into destination register }
  1263. move_if_needed(list, size, hreg2, dst);
  1264. end;
  1265. OP_DIV,
  1266. OP_IDIV :
  1267. begin
  1268. internalerror(20020816);
  1269. end;
  1270. OP_MUL,
  1271. OP_IMUL:
  1272. begin
  1273. if not (CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype]) then
  1274. if op = OP_MUL then
  1275. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1276. else
  1277. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1278. else
  1279. begin
  1280. { 68020+ and ColdFire codepath, probably could be improved }
  1281. hreg1 := force_to_dataregister(list, size, src);
  1282. hreg2 := force_to_dataregister(list, size, dst);
  1283. sign_extend(list, size, hreg1);
  1284. sign_extend(list, size, hreg2);
  1285. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1286. { move back result into destination register }
  1287. move_if_needed(list, size, hreg2, dst);
  1288. end;
  1289. end;
  1290. OP_NEG,
  1291. OP_NOT :
  1292. begin
  1293. { if there are two operands, move the register,
  1294. since the operation will only be done on the result
  1295. register. }
  1296. if (src<>dst) then
  1297. a_load_reg_reg(list,size,size,src,dst);
  1298. hreg2 := force_to_dataregister(list, size, dst);
  1299. { coldfire only supports long version }
  1300. if current_settings.cputype in cpu_ColdFire then
  1301. sign_extend(list, size, hreg2);
  1302. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1303. { move back the result to the result register if needed }
  1304. move_if_needed(list, size, hreg2, dst);
  1305. end;
  1306. else
  1307. internalerror(20020729);
  1308. end;
  1309. end;
  1310. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1311. var
  1312. opcode : tasmop;
  1313. opsize : topsize;
  1314. href : treference;
  1315. hreg : tregister;
  1316. begin
  1317. opcode := topcg2tasmop[op];
  1318. opsize := TCGSize2OpSize[size];
  1319. { on ColdFire all arithmetic operations are only possible on 32bit
  1320. and addressing modes are limited }
  1321. if needs_unaligned(ref.alignment,size) or
  1322. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1323. begin
  1324. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: inherited #1')));
  1325. inherited;
  1326. exit;
  1327. end;
  1328. case op of
  1329. OP_ADD,
  1330. OP_SUB,
  1331. OP_OR,
  1332. OP_XOR,
  1333. OP_AND:
  1334. begin
  1335. //list.concat(tai_comment.create(strpnew('a_op_reg_ref: normal op')));
  1336. href:=ref;
  1337. fixref(list,href,false);
  1338. { areg -> ref arithmetic operations are impossible on 68k }
  1339. hreg:=force_to_dataregister(list,size,reg);
  1340. { add/sub works the same way, so have it unified here }
  1341. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1342. end;
  1343. else begin
  1344. //list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited #2')));
  1345. inherited;
  1346. end;
  1347. end;
  1348. end;
  1349. procedure tcg68k.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1350. var
  1351. opcode : tasmop;
  1352. opsize : topsize;
  1353. href : treference;
  1354. hreg : tregister;
  1355. begin
  1356. opcode := topcg2tasmop[op];
  1357. opsize := TCGSize2OpSize[size];
  1358. { on ColdFire all arithmetic operations are only possible on 32bit
  1359. and addressing modes are limited }
  1360. if needs_unaligned(ref.alignment,size) or
  1361. ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1362. begin
  1363. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: inherited #1')));
  1364. inherited;
  1365. exit;
  1366. end;
  1367. case op of
  1368. OP_ADD,
  1369. OP_SUB,
  1370. OP_OR,
  1371. OP_AND,
  1372. OP_MUL,
  1373. OP_IMUL:
  1374. begin
  1375. //list.concat(tai_comment.create(strpnew('a_op_ref_reg: normal op')));
  1376. href:=ref;
  1377. { Coldfire doesn't support d(Ax,Dx) for long MULx... }
  1378. fixref(list,href,(op in [OP_MUL,OP_IMUL]) and
  1379. (current_settings.cputype in cpu_coldfire));
  1380. list.concat(taicpu.op_ref_reg(opcode, opsize, href, reg));
  1381. end;
  1382. else begin
  1383. //list.concat(tai_comment.create(strpnew('a_op_ref_reg inherited #2')));
  1384. inherited;
  1385. end;
  1386. end;
  1387. end;
  1388. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1389. l : tasmlabel);
  1390. var
  1391. hregister : tregister;
  1392. instr : taicpu;
  1393. need_temp_reg : boolean;
  1394. temp_size: topsize;
  1395. begin
  1396. need_temp_reg := false;
  1397. { plain 68000 doesn't support address registers for TST }
  1398. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1399. (a = 0) and isaddressregister(reg);
  1400. { ColdFire doesn't support address registers for CMPI }
  1401. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1402. and (a <> 0) and isaddressregister(reg));
  1403. if need_temp_reg then
  1404. begin
  1405. hregister := getintregister(list,OS_INT);
  1406. temp_size := TCGSize2OpSize[size];
  1407. if temp_size < S_W then
  1408. temp_size := S_W;
  1409. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1410. add_move_instruction(instr);
  1411. list.concat(instr);
  1412. reg := hregister;
  1413. { do sign extension if size had to be modified }
  1414. if temp_size <> TCGSize2OpSize[size] then
  1415. begin
  1416. sign_extend(list, size, reg);
  1417. size:=OS_INT;
  1418. end;
  1419. end;
  1420. if a = 0 then
  1421. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1422. else
  1423. begin
  1424. { ColdFire ISA A also needs S_L for CMPI }
  1425. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1426. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1427. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1428. default. (KB) }
  1429. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]} then
  1430. begin
  1431. sign_extend(list, size, reg);
  1432. size:=OS_INT;
  1433. end;
  1434. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1435. end;
  1436. { emit the actual jump to the label }
  1437. a_jmp_cond(list,cmp_op,l);
  1438. end;
  1439. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1440. var
  1441. tmpref: treference;
  1442. begin
  1443. { optimize for usage of TST here, so ref compares against zero, which is the
  1444. most common case by far in the RTL code at least (KB) }
  1445. if not needs_unaligned(ref.alignment,size) and (a = 0) then
  1446. begin
  1447. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1448. tmpref:=ref;
  1449. fixref(list,tmpref,false);
  1450. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1451. a_jmp_cond(list,cmp_op,l);
  1452. end
  1453. else
  1454. begin
  1455. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1456. inherited;
  1457. end;
  1458. end;
  1459. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1460. begin
  1461. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c,cpu_cfv4e]) then
  1462. begin
  1463. sign_extend(list,size,reg1);
  1464. sign_extend(list,size,reg2);
  1465. size:=OS_INT;
  1466. end;
  1467. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1468. { emit the actual jump to the label }
  1469. a_jmp_cond(list,cmp_op,l);
  1470. end;
  1471. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1472. var
  1473. ai: taicpu;
  1474. begin
  1475. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1476. ai.is_jmp := true;
  1477. list.concat(ai);
  1478. end;
  1479. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1480. var
  1481. ai: taicpu;
  1482. begin
  1483. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1484. ai.is_jmp := true;
  1485. list.concat(ai);
  1486. end;
  1487. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1488. var
  1489. ai : taicpu;
  1490. begin
  1491. if not (f in FloatResFlags) then
  1492. ai := Taicpu.op_sym(A_BXX,S_NO,l)
  1493. else
  1494. ai := Taicpu.op_sym(A_FBXX,S_NO,l);
  1495. ai.SetCondition(flags_to_cond(f));
  1496. ai.is_jmp := true;
  1497. list.concat(ai);
  1498. end;
  1499. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1500. var
  1501. ai : taicpu;
  1502. htrue: tasmlabel;
  1503. begin
  1504. if isaddressregister(reg) then
  1505. internalerror(2017051701);
  1506. if (f in FloatResFlags) then
  1507. begin
  1508. //list.concat(tai_comment.create(strpnew('flags2reg: float resflags')));
  1509. current_asmdata.getjumplabel(htrue);
  1510. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,1,reg);
  1511. a_jmp_flags(list, f, htrue);
  1512. a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,reg);
  1513. a_label(current_asmdata.CurrAsmList,htrue);
  1514. exit;
  1515. end;
  1516. ai:=Taicpu.Op_reg(A_Sxx,S_B,reg);
  1517. ai.SetCondition(flags_to_cond(f));
  1518. list.concat(ai);
  1519. { Scc stores a complete byte of 1s, but the compiler expects only one
  1520. bit set, so ensure this is the case }
  1521. if not (current_settings.cputype in cpu_coldfire) then
  1522. begin
  1523. if size in [OS_S8,OS_8] then
  1524. list.concat(taicpu.op_reg(A_NEG,S_B,reg))
  1525. else
  1526. list.concat(taicpu.op_const_reg(A_AND,TCgSize2OpSize[size],1,reg));
  1527. end
  1528. else
  1529. list.concat(taicpu.op_const_reg(A_AND,S_L,1,reg));
  1530. end;
  1531. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1532. const
  1533. lentocgsize: array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1534. var
  1535. helpsize : longint;
  1536. i : byte;
  1537. hregister : tregister;
  1538. iregister : tregister;
  1539. jregister : tregister;
  1540. hl : tasmlabel;
  1541. srcrefp,dstrefp : treference;
  1542. srcref,dstref : treference;
  1543. begin
  1544. if (len = 1) or ((len in [2,4]) and (current_settings.cputype <> cpu_mc68000)) then
  1545. begin
  1546. //list.concat(tai_comment.create(strpnew('g_concatcopy: small')));
  1547. a_load_ref_ref(list,lentocgsize[len],lentocgsize[len],source,dest);
  1548. exit;
  1549. end;
  1550. //list.concat(tai_comment.create(strpnew('g_concatcopy')));
  1551. hregister := getintregister(list,OS_INT);
  1552. iregister:=getaddressregister(list);
  1553. reference_reset_base(srcref,iregister,0,source.alignment,source.volatility);
  1554. srcrefp:=srcref;
  1555. srcrefp.direction := dir_inc;
  1556. jregister:=getaddressregister(list);
  1557. reference_reset_base(dstref,jregister,0,dest.alignment,dest.volatility);
  1558. dstrefp:=dstref;
  1559. dstrefp.direction := dir_inc;
  1560. { iregister = source }
  1561. { jregister = destination }
  1562. a_loadaddr_ref_reg(list,source,iregister);
  1563. a_loadaddr_ref_reg(list,dest,jregister);
  1564. if not (needs_unaligned(source.alignment,OS_INT) or needs_unaligned(dest.alignment,OS_INT)) then
  1565. begin
  1566. if not ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=16))) then
  1567. begin
  1568. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1569. helpsize := len - len mod 4;
  1570. len := len mod 4;
  1571. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1572. current_asmdata.getjumplabel(hl);
  1573. a_label(list,hl);
  1574. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp));
  1575. if (current_settings.cputype in cpu_coldfire) or ((helpsize div 4)-1 > high(smallint)) then
  1576. begin
  1577. { Coldfire does not support DBRA, also it is word only }
  1578. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1579. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1580. end
  1581. else
  1582. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1583. end;
  1584. helpsize:=len div 4;
  1585. { move a dword x times }
  1586. for i:=1 to helpsize do
  1587. begin
  1588. dec(len,4);
  1589. if (len > 0) then
  1590. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcrefp,dstrefp))
  1591. else
  1592. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,srcref,dstref));
  1593. end;
  1594. { move a word }
  1595. if len>1 then
  1596. begin
  1597. dec(len,2);
  1598. if (len > 0) then
  1599. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcrefp,dstrefp))
  1600. else
  1601. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,srcref,dstref));
  1602. end;
  1603. { move a single byte }
  1604. if len>0 then
  1605. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcref,dstref));
  1606. end
  1607. else
  1608. begin
  1609. { Fast 68010 loop mode with no possible alignment problems }
  1610. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1611. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1612. current_asmdata.getjumplabel(hl);
  1613. a_label(list,hl);
  1614. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,srcrefp,dstrefp));
  1615. if (len - 1) > high(smallint) then
  1616. begin
  1617. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1618. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1619. end
  1620. else
  1621. list.concat(taicpu.op_reg_sym(A_DBRA,S_NO,hregister,hl));
  1622. end;
  1623. end;
  1624. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1625. var
  1626. hl : tasmlabel;
  1627. ai : taicpu;
  1628. cond : TAsmCond;
  1629. begin
  1630. if not(cs_check_overflow in current_settings.localswitches) then
  1631. exit;
  1632. current_asmdata.getjumplabel(hl);
  1633. if not ((def.typ=pointerdef) or
  1634. ((def.typ=orddef) and
  1635. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1636. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1637. cond:=C_VC
  1638. else
  1639. begin
  1640. { MUL/DIV always sets the overflow flag, and never the carry flag }
  1641. { Note/Fixme: This still doesn't cover the ColdFire, where none of these opcodes
  1642. set either the overflow or the carry flag. So CF must be handled in other ways. }
  1643. if taicpu(list.last).opcode in [A_MULU,A_MULS,A_DIVS,A_DIVU,A_DIVUL,A_DIVSL] then
  1644. cond:=C_VC
  1645. else
  1646. cond:=C_CC;
  1647. end;
  1648. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1649. ai.SetCondition(cond);
  1650. ai.is_jmp:=true;
  1651. list.concat(ai);
  1652. a_call_name(list,'FPC_OVERFLOW',false);
  1653. a_label(list,hl);
  1654. end;
  1655. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1656. begin
  1657. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1658. However, a LINK seems faster than two moves on everything from 68000
  1659. to '060, so the two move branch here was dropped. (KB) }
  1660. if not nostackframe then
  1661. begin
  1662. localsize:=align(localsize,4);
  1663. if (localsize > high(smallint)) then
  1664. begin
  1665. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1666. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1667. end
  1668. else
  1669. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1670. end;
  1671. end;
  1672. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1673. var
  1674. r,hregister : TRegister;
  1675. ref : TReference;
  1676. ref2: TReference;
  1677. begin
  1678. if not nostackframe then
  1679. begin
  1680. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1681. { if parasize is less than zero here, we probably have a cdecl function.
  1682. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1683. 68k GCC uses two different methods to free the stack, depending if the target
  1684. architecture supports RTD or not, and one does callee side, the other does
  1685. caller side free, which looks like a PITA to support. We have to figure this
  1686. out later. More info welcomed. (KB) }
  1687. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1688. begin
  1689. if current_settings.cputype in cpu_mc68020p then
  1690. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1691. else
  1692. begin
  1693. { We must pull the PC Counter from the stack, before }
  1694. { restoring the stack pointer, otherwise the PC would }
  1695. { point to nowhere! }
  1696. { Instead of doing a slow copy of the return address while trying }
  1697. { to feed it to the RTS instruction, load the PC to A1 (scratch reg) }
  1698. { then free up the stack allocated for paras, then use a JMP (A1) to }
  1699. { return to the caller with the paras freed. (KB) }
  1700. hregister:=NR_A1;
  1701. cg.a_reg_alloc(list,hregister);
  1702. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4,[]);
  1703. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1704. { instead of using a postincrement above (which also writes the }
  1705. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1706. { below then take that size into account as well, so SP reg is only }
  1707. { written once (KB) }
  1708. parasize:=parasize+4;
  1709. r:=NR_SP;
  1710. { can we do a quick addition ... }
  1711. if (parasize < 9) then
  1712. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1713. else { nope ... }
  1714. begin
  1715. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4,[]);
  1716. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1717. end;
  1718. reference_reset_base(ref,hregister,0,4,[]);
  1719. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1720. end;
  1721. end
  1722. else
  1723. list.concat(taicpu.op_none(A_RTS,S_NO));
  1724. end
  1725. else
  1726. begin
  1727. list.concat(taicpu.op_none(A_RTS,S_NO));
  1728. end;
  1729. { Routines with the poclearstack flag set use only a ret.
  1730. also routines with parasize=0 }
  1731. { TODO: figure out if these are still relevant to us (KB) }
  1732. (*
  1733. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1734. begin
  1735. { complex return values are removed from stack in C code PM }
  1736. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1737. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1738. else
  1739. list.concat(taicpu.op_none(A_RTS,S_NO));
  1740. end
  1741. else if (parasize=0) then
  1742. begin
  1743. list.concat(taicpu.op_none(A_RTS,S_NO));
  1744. end
  1745. else
  1746. *)
  1747. end;
  1748. procedure tcg68k.g_save_registers(list:TAsmList);
  1749. var
  1750. dataregs: tcpuregisterset;
  1751. addrregs: tcpuregisterset;
  1752. fpuregs: tcpuregisterset;
  1753. href : treference;
  1754. hreg : tregister;
  1755. hfreg : tregister;
  1756. size : longint;
  1757. fsize : longint;
  1758. r : integer;
  1759. begin
  1760. { The code generated by the section below, particularly the movem.l
  1761. instruction is known to cause an issue when compiled by some GNU
  1762. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1763. when you run into this problem, just call inherited here instead
  1764. to skip the movem.l generation. But better just use working GNU
  1765. AS version instead. (KB) }
  1766. dataregs:=[];
  1767. addrregs:=[];
  1768. fpuregs:=[];
  1769. { calculate temp. size }
  1770. size:=0;
  1771. fsize:=0;
  1772. hreg:=NR_NO;
  1773. hfreg:=NR_NO;
  1774. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1775. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1776. begin
  1777. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1778. inc(size,sizeof(aint));
  1779. dataregs:=dataregs + [saved_standard_registers[r]];
  1780. end;
  1781. if uses_registers(R_ADDRESSREGISTER) then
  1782. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1783. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1784. begin
  1785. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1786. inc(size,sizeof(aint));
  1787. addrregs:=addrregs + [saved_address_registers[r]];
  1788. end;
  1789. if uses_registers(R_FPUREGISTER) then
  1790. for r:=low(saved_fpu_registers) to high(saved_fpu_registers) do
  1791. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1792. begin
  1793. hfreg:=newreg(R_FPUREGISTER,saved_fpu_registers[r],R_SUBNONE);
  1794. inc(fsize,fpuregsize);
  1795. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1796. end;
  1797. { 68k has no MM registers }
  1798. if uses_registers(R_MMREGISTER) then
  1799. internalerror(2014030201);
  1800. if (size+fsize) > 0 then
  1801. begin
  1802. tg.GetTemp(list,size+fsize,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1803. include(current_procinfo.flags,pi_has_saved_regs);
  1804. { Copy registers to temp }
  1805. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1806. href:=current_procinfo.save_regs_ref;
  1807. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire+[cpu_mc68000]) then
  1808. begin
  1809. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1810. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1811. reference_reset_base(href,NR_A0,0,sizeof(pint),[]);
  1812. end;
  1813. if size > 0 then
  1814. if size = sizeof(aint) then
  1815. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1816. else
  1817. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,[],href));
  1818. if fsize > 0 then
  1819. begin
  1820. { size is always longword aligned, while fsize is not }
  1821. inc(href.offset,size);
  1822. if fsize = fpuregsize then
  1823. list.concat(taicpu.op_reg_ref(A_FMOVE,fpuregopsize,hfreg,href))
  1824. else
  1825. list.concat(taicpu.op_regset_ref(A_FMOVEM,fpuregopsize,[],[],fpuregs,href));
  1826. end;
  1827. end;
  1828. end;
  1829. procedure tcg68k.g_restore_registers(list:TAsmList);
  1830. var
  1831. dataregs: tcpuregisterset;
  1832. addrregs: tcpuregisterset;
  1833. fpuregs : tcpuregisterset;
  1834. href : treference;
  1835. r : integer;
  1836. hreg : tregister;
  1837. hfreg : tregister;
  1838. size : longint;
  1839. fsize : longint;
  1840. begin
  1841. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1842. dataregs:=[];
  1843. addrregs:=[];
  1844. fpuregs:=[];
  1845. if not(pi_has_saved_regs in current_procinfo.flags) then
  1846. exit;
  1847. { Copy registers from temp }
  1848. size:=0;
  1849. fsize:=0;
  1850. hreg:=NR_NO;
  1851. hfreg:=NR_NO;
  1852. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1853. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1854. begin
  1855. inc(size,sizeof(aint));
  1856. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1857. { Allocate register so the optimizer does not remove the load }
  1858. a_reg_alloc(list,hreg);
  1859. dataregs:=dataregs + [saved_standard_registers[r]];
  1860. end;
  1861. if uses_registers(R_ADDRESSREGISTER) then
  1862. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1863. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1864. begin
  1865. inc(size,sizeof(aint));
  1866. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1867. { Allocate register so the optimizer does not remove the load }
  1868. a_reg_alloc(list,hreg);
  1869. addrregs:=addrregs + [saved_address_registers[r]];
  1870. end;
  1871. if uses_registers(R_FPUREGISTER) then
  1872. for r:=low(saved_fpu_registers) to high(saved_fpu_registers) do
  1873. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1874. begin
  1875. inc(fsize,fpuregsize);
  1876. hfreg:=newreg(R_FPUREGISTER,saved_fpu_registers[r],R_SUBNONE);
  1877. { Allocate register so the optimizer does not remove the load }
  1878. a_reg_alloc(list,hfreg);
  1879. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1880. end;
  1881. { 68k has no MM registers }
  1882. if uses_registers(R_MMREGISTER) then
  1883. internalerror(2014030202);
  1884. { Restore registers from temp }
  1885. href:=current_procinfo.save_regs_ref;
  1886. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire+[cpu_mc68000]) then
  1887. begin
  1888. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1889. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1890. reference_reset_base(href,NR_A0,0,sizeof(pint),[]);
  1891. end;
  1892. if size > 0 then
  1893. if size = sizeof(aint) then
  1894. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1895. else
  1896. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs,[]));
  1897. if fsize > 0 then
  1898. begin
  1899. { size is always longword aligned, while fsize is not }
  1900. inc(href.offset,size);
  1901. if fsize = fpuregsize then
  1902. list.concat(taicpu.op_ref_reg(A_FMOVE,fpuregopsize,href,hfreg))
  1903. else
  1904. list.concat(taicpu.op_ref_regset(A_FMOVEM,fpuregopsize,href,[],[],fpuregs));
  1905. end;
  1906. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1907. end;
  1908. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1909. begin
  1910. case _newsize of
  1911. OS_S16, OS_16:
  1912. case _oldsize of
  1913. OS_S8:
  1914. begin { 8 -> 16 bit sign extend }
  1915. if (isaddressregister(reg)) then
  1916. internalerror(2014031201);
  1917. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1918. end;
  1919. OS_8: { 8 -> 16 bit zero extend }
  1920. begin
  1921. if (current_settings.cputype in cpu_coldfire) then
  1922. { ColdFire has no ANDI.W }
  1923. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1924. else
  1925. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1926. end;
  1927. end;
  1928. OS_S32, OS_32:
  1929. case _oldsize of
  1930. OS_S8:
  1931. begin { 8 -> 32 bit sign extend }
  1932. if (isaddressregister(reg)) then
  1933. internalerror(2014031202);
  1934. if (current_settings.cputype = cpu_MC68000) then
  1935. begin
  1936. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1937. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1938. end
  1939. else
  1940. begin
  1941. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1942. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1943. end;
  1944. end;
  1945. OS_8: { 8 -> 32 bit zero extend }
  1946. begin
  1947. if (isaddressregister(reg)) then
  1948. internalerror(2015031501);
  1949. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1950. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1951. end;
  1952. OS_S16: { 16 -> 32 bit sign extend }
  1953. begin
  1954. { address registers are sign-extended from 16->32 bit anyway
  1955. automagically on every W operation by the CPU, so this is a NOP }
  1956. if not isaddressregister(reg) then
  1957. begin
  1958. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1959. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1960. end;
  1961. end;
  1962. OS_16:
  1963. begin
  1964. if (isaddressregister(reg)) then
  1965. internalerror(2015031502);
  1966. //list.concat(tai_comment.create(strpnew('zero extend word')));
  1967. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1968. end;
  1969. end;
  1970. end; { otherwise the size is already correct }
  1971. end;
  1972. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1973. begin
  1974. sign_extend(list, _oldsize, OS_INT, reg);
  1975. end;
  1976. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1977. var
  1978. ai : taicpu;
  1979. begin
  1980. if cond=OC_None then
  1981. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1982. else
  1983. begin
  1984. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1985. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1986. end;
  1987. ai.is_jmp:=true;
  1988. list.concat(ai);
  1989. end;
  1990. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1991. operations on an address register. if the register is a dataregister anyway, it
  1992. just returns it untouched.}
  1993. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1994. var
  1995. scratch_reg: TRegister;
  1996. instr: Taicpu;
  1997. begin
  1998. if isaddressregister(reg) then
  1999. begin
  2000. scratch_reg:=getintregister(list,OS_INT);
  2001. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  2002. add_move_instruction(instr);
  2003. list.concat(instr);
  2004. result:=scratch_reg;
  2005. end
  2006. else
  2007. result:=reg;
  2008. end;
  2009. { moves source register to destination register, if the two are not the same. can be used in pair
  2010. with force_to_dataregister() }
  2011. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  2012. var
  2013. instr: Taicpu;
  2014. begin
  2015. if (src <> dest) then
  2016. begin
  2017. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  2018. add_move_instruction(instr);
  2019. list.concat(instr);
  2020. end;
  2021. end;
  2022. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2023. var
  2024. hsym : tsym;
  2025. href : treference;
  2026. paraloc : Pcgparalocation;
  2027. begin
  2028. { calculate the parameter info for the procdef }
  2029. procdef.init_paraloc_info(callerside);
  2030. hsym:=tsym(procdef.parast.Find('self'));
  2031. if not(assigned(hsym) and
  2032. (hsym.typ=paravarsym)) then
  2033. internalerror(2013100702);
  2034. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2035. while paraloc<>nil do
  2036. with paraloc^ do
  2037. begin
  2038. case loc of
  2039. LOC_REGISTER:
  2040. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  2041. LOC_REFERENCE:
  2042. begin
  2043. { offset in the wrapper needs to be adjusted for the stored
  2044. return address }
  2045. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint),[]);
  2046. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  2047. and it's probably smaller code for the majority of cases (if ioffset small, the
  2048. load will use MOVEQ) (KB) }
  2049. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  2050. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  2051. end
  2052. else
  2053. internalerror(2013100703);
  2054. end;
  2055. paraloc:=next;
  2056. end;
  2057. end;
  2058. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2059. begin
  2060. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  2061. end;
  2062. procedure tcg68k.check_register_size(size:tcgsize;reg:tregister);
  2063. begin
  2064. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  2065. internalerror(201512131);
  2066. end;
  2067. function tcg68k.optimize_const_mul_to_shift_sub_add(list: TAsmList; maxops: longint; a: tcgint; size: tcgsize; reg: TRegister): boolean;
  2068. var
  2069. i: longint;
  2070. nextpower: tcgint;
  2071. powerbit: longint;
  2072. submask: tcgint;
  2073. lastshift: longint;
  2074. hreg: tregister;
  2075. firstmov: boolean;
  2076. begin
  2077. nextpower:=nextpowerof2(a,powerbit);
  2078. submask:=nextpower-a;
  2079. result:=not ((popcnt(qword(a)) > maxops) and ((popcnt(qword(submask))+1) > maxops));
  2080. if not result then
  2081. exit;
  2082. list.concat(tai_comment.create(strpnew('optimize_const_mul_to_shift_sub_add, multiplier: '+tostr(a))));
  2083. lastshift:=0;
  2084. hreg:=getintregister(list,OS_INT);
  2085. if (popcnt(qword(a)) < (popcnt(qword(submask))+1)) then
  2086. begin
  2087. { doing additions }
  2088. firstmov:=(a and 1) = 0;
  2089. if not firstmov then
  2090. a_load_reg_reg(list,size,OS_INT,reg,hreg);
  2091. for i:=1 to bsrqword(a) do
  2092. if ((a shr i) and 1) = 1 then
  2093. begin
  2094. if firstmov then
  2095. begin
  2096. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2097. a_load_reg_reg(list,OS_INT,OS_INT,reg,hreg);
  2098. firstmov:=false;
  2099. end
  2100. else
  2101. begin
  2102. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,hreg);
  2103. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2104. end;
  2105. lastshift:=i;
  2106. end;
  2107. end
  2108. else
  2109. begin
  2110. { doing subtractions }
  2111. a_load_const_reg(list,OS_INT,0,hreg);
  2112. for i:=0 to bsrqword(submask) do
  2113. if ((submask shr i) and 1) = 1 then
  2114. begin
  2115. a_op_const_reg(list,OP_SHL,OS_INT,i-lastshift,reg);
  2116. a_op_reg_reg(list,OP_SUB,OS_INT,reg,hreg);
  2117. lastshift:=i;
  2118. end;
  2119. a_op_const_reg(list,OP_SHL,OS_INT,powerbit-lastshift,reg);
  2120. a_op_reg_reg(list,OP_ADD,OS_INT,hreg,reg);
  2121. end;
  2122. result:=true;
  2123. end;
  2124. {****************************************************************************}
  2125. { TCG64F68K }
  2126. {****************************************************************************}
  2127. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  2128. var
  2129. opcode : tasmop;
  2130. xopcode : tasmop;
  2131. instr : taicpu;
  2132. begin
  2133. opcode := topcg2tasmop[op];
  2134. xopcode := topcg2tasmopx[op];
  2135. case op of
  2136. OP_ADD,OP_SUB:
  2137. begin
  2138. { if one of these three registers is an address
  2139. register, we'll really get into problems! }
  2140. if isaddressregister(regdst.reglo) or
  2141. isaddressregister(regdst.reghi) or
  2142. isaddressregister(regsrc.reghi) then
  2143. internalerror(2014030101);
  2144. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  2145. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  2146. end;
  2147. OP_AND,OP_OR:
  2148. begin
  2149. { at least one of the registers must be a data register }
  2150. if (isaddressregister(regdst.reglo) and
  2151. isaddressregister(regsrc.reglo)) or
  2152. (isaddressregister(regsrc.reghi) and
  2153. isaddressregister(regdst.reghi)) then
  2154. internalerror(2014030102);
  2155. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2156. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2157. end;
  2158. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  2159. OP_IDIV,OP_DIV,
  2160. OP_IMUL,OP_MUL:
  2161. internalerror(2002081701);
  2162. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  2163. OP_SAR,OP_SHL,OP_SHR:
  2164. internalerror(2002081702);
  2165. OP_XOR:
  2166. begin
  2167. if isaddressregister(regdst.reglo) or
  2168. isaddressregister(regsrc.reglo) or
  2169. isaddressregister(regsrc.reghi) or
  2170. isaddressregister(regdst.reghi) then
  2171. internalerror(2014030103);
  2172. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2173. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2174. end;
  2175. OP_NEG,OP_NOT:
  2176. begin
  2177. if isaddressregister(regdst.reglo) or
  2178. isaddressregister(regdst.reghi) then
  2179. internalerror(2014030104);
  2180. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  2181. cg.add_move_instruction(instr);
  2182. list.concat(instr);
  2183. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  2184. cg.add_move_instruction(instr);
  2185. list.concat(instr);
  2186. if (op = OP_NOT) then
  2187. xopcode:=opcode;
  2188. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  2189. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  2190. end;
  2191. end; { end case }
  2192. end;
  2193. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  2194. var
  2195. href : treference;
  2196. hreg: tregister;
  2197. begin
  2198. case op of
  2199. OP_NEG,OP_NOT:
  2200. begin
  2201. a_load64_ref_reg(list,ref,reg);
  2202. a_op64_reg_reg(list,op,size,reg,reg);
  2203. end;
  2204. OP_AND,OP_OR:
  2205. begin
  2206. href:=ref;
  2207. tcg68k(cg).fixref(list,href,false);
  2208. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reghi));
  2209. inc(href.offset,4);
  2210. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2211. end;
  2212. OP_ADD,OP_SUB:
  2213. begin
  2214. href:=ref;
  2215. tcg68k(cg).fixref(list,href,false);
  2216. hreg:=cg.getintregister(list,OS_32);
  2217. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2218. inc(href.offset,4);
  2219. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,href,reg.reglo));
  2220. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,hreg,reg.reghi));
  2221. end;
  2222. else
  2223. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2224. high dword, although low dword can still be handled directly. }
  2225. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2226. end;
  2227. end;
  2228. procedure tcg64f68k.a_op64_reg_ref(list : TAsmList;op:TOpCG;size : tcgsize;reg : tregister64;const ref : treference);
  2229. var
  2230. href: treference;
  2231. hreg: tregister;
  2232. begin
  2233. case op of
  2234. OP_AND,OP_OR,OP_XOR:
  2235. begin
  2236. href:=ref;
  2237. tcg68k(cg).fixref(list,href,false);
  2238. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reghi,href));
  2239. inc(href.offset,4);
  2240. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2241. end;
  2242. OP_ADD,OP_SUB:
  2243. begin
  2244. href:=ref;
  2245. tcg68k(cg).fixref(list,href,false);
  2246. hreg:=cg.getintregister(list,OS_32);
  2247. cg.a_load_ref_reg(list,OS_32,OS_32,href,hreg);
  2248. inc(href.offset,4);
  2249. list.concat(taicpu.op_reg_ref(topcg2tasmop[op],S_L,reg.reglo,href));
  2250. list.concat(taicpu.op_reg_reg(topcg2tasmopx[op],S_L,reg.reghi,hreg));
  2251. dec(href.offset,4);
  2252. cg.a_load_reg_ref(list,OS_32,OS_32,hreg,href);
  2253. end;
  2254. else
  2255. inherited a_op64_reg_ref(list,op,size,reg,ref);
  2256. end;
  2257. end;
  2258. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2259. var
  2260. lowvalue : cardinal;
  2261. highvalue : cardinal;
  2262. opcode : tasmop;
  2263. xopcode : tasmop;
  2264. hreg : tregister;
  2265. begin
  2266. { is it optimized out ? }
  2267. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2268. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2269. exit; }
  2270. lowvalue := cardinal(value);
  2271. highvalue := value shr 32;
  2272. opcode := topcg2tasmop[op];
  2273. xopcode := topcg2tasmopx[op];
  2274. { the destination registers must be data registers }
  2275. if isaddressregister(regdst.reglo) or
  2276. isaddressregister(regdst.reghi) then
  2277. internalerror(2014030105);
  2278. case op of
  2279. OP_ADD,OP_SUB:
  2280. begin
  2281. hreg:=cg.getintregister(list,OS_INT);
  2282. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2283. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2284. { don't use cg.a_op_const_reg() here, because a possible optimized
  2285. ADDQ/SUBQ wouldn't set the eXtend bit }
  2286. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2287. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2288. end;
  2289. OP_AND,OP_OR,OP_XOR:
  2290. begin
  2291. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2292. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2293. end;
  2294. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2295. OP_IDIV,OP_DIV,
  2296. OP_IMUL,OP_MUL:
  2297. internalerror(2002081701);
  2298. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2299. OP_SAR,OP_SHL,OP_SHR:
  2300. internalerror(2002081702);
  2301. { these should have been handled already by earlier passes }
  2302. OP_NOT,OP_NEG:
  2303. internalerror(2012110403);
  2304. end; { end case }
  2305. end;
  2306. procedure tcg64f68k.a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);
  2307. var
  2308. tmpref: treference;
  2309. begin
  2310. tmpref:=ref;
  2311. tcg68k(cg).fixref(list,tmpref,false);
  2312. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  2313. inc(tmpref.offset,4);
  2314. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,tmpref);
  2315. end;
  2316. procedure tcg64f68k.a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);
  2317. var
  2318. tmpref: treference;
  2319. begin
  2320. { do not allow 64bit values to be loaded to address registers }
  2321. if isaddressregister(reg.reglo) or
  2322. isaddressregister(reg.reghi) then
  2323. internalerror(2016050501);
  2324. tmpref:=ref;
  2325. tcg68k(cg).fixref(list,tmpref,false);
  2326. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  2327. inc(tmpref.offset,4);
  2328. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  2329. end;
  2330. procedure create_codegen;
  2331. begin
  2332. cg := tcg68k.create;
  2333. cg64 :=tcg64f68k.create;
  2334. end;
  2335. end.