cgcpu.pas 99 KB

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