cgcpu.pas 99 KB

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