cgcpu.pas 94 KB

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