cgcpu.pas 87 KB

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