cgcpu.pas 99 KB

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