cgcpu.pas 90 KB

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