cgcpu.pas 99 KB

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