cgcpu.pas 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  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. 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. private
  81. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  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. hreg : tregister;
  839. opsize : topsize;
  840. begin
  841. { move to destination register }
  842. opsize:=TCGSize2OpSize[fromsize];
  843. if isaddressregister(reg2) and not (opsize in [S_L]) then
  844. begin
  845. hreg:=cg.getintregister(list,OS_ADDR);
  846. instr:=taicpu.op_reg_reg(A_MOVE,TCGSize2OpSize[fromsize],reg1,hreg);
  847. add_move_instruction(instr);
  848. list.concat(instr);
  849. sign_extend(list,fromsize,hreg);
  850. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg2));
  851. end
  852. else
  853. begin
  854. if (reg1<>reg2) then
  855. begin
  856. instr:=taicpu.op_reg_reg(A_MOVE,opsize,reg1,reg2);
  857. add_move_instruction(instr);
  858. list.concat(instr);
  859. end;
  860. sign_extend(list,fromsize,reg2);
  861. end;
  862. end;
  863. procedure tcg68k.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref : treference;register : tregister);
  864. var
  865. href : treference;
  866. hreg : tregister;
  867. size : tcgsize;
  868. opsize: topsize;
  869. begin
  870. href:=ref;
  871. fixref(list,href);
  872. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  873. size:=fromsize
  874. else
  875. size:=tosize;
  876. opsize:=TCGSize2OpSize[size];
  877. if isaddressregister(register) and not (opsize in [S_L]) then
  878. begin
  879. hreg:=getintregister(list,OS_ADDR);
  880. list.concat(taicpu.op_ref_reg(A_MOVE,opsize,href,hreg));
  881. sign_extend(list,size,hreg);
  882. a_load_reg_reg(list,OS_ADDR,OS_ADDR,hreg,register);
  883. end
  884. else
  885. begin
  886. list.concat(taicpu.op_ref_reg(A_MOVE,opsize,href,register));
  887. { extend the value in the register }
  888. sign_extend(list, size, register);
  889. end;
  890. end;
  891. procedure tcg68k.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  892. var
  893. href : treference;
  894. hreg : tregister;
  895. begin
  896. href:=ref;
  897. fixref(list, href);
  898. if not isaddressregister(r) then
  899. begin
  900. hreg:=getaddressregister(list);
  901. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  902. a_load_reg_reg(list, OS_ADDR, OS_ADDR, hreg, r);
  903. end
  904. else
  905. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,r));
  906. end;
  907. procedure tcg68k.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  908. var
  909. instr : taicpu;
  910. begin
  911. instr:=taicpu.op_reg_reg(A_FMOVE,S_FX,reg1,reg2);
  912. add_move_instruction(instr);
  913. list.concat(instr);
  914. end;
  915. procedure tcg68k.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  916. var
  917. opsize : topsize;
  918. href : treference;
  919. begin
  920. opsize := tcgsize2opsize[fromsize];
  921. { extended is not supported, since it is not available on Coldfire }
  922. if opsize = S_FX then
  923. internalerror(20020729);
  924. href := ref;
  925. fixref(list,href);
  926. list.concat(taicpu.op_ref_reg(A_FMOVE,opsize,href,reg));
  927. end;
  928. procedure tcg68k.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  929. var
  930. opsize : topsize;
  931. href : treference;
  932. begin
  933. opsize := tcgsize2opsize[tosize];
  934. { extended is not supported, since it is not available on Coldfire }
  935. if opsize = S_FX then
  936. internalerror(20020729);
  937. href := ref;
  938. fixref(list,href);
  939. list.concat(taicpu.op_reg_ref(A_FMOVE,opsize,reg,href));
  940. end;
  941. procedure tcg68k.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const reg : tregister;const cgpara : tcgpara);
  942. var
  943. ref : treference;
  944. begin
  945. if use_push(cgpara) and (current_settings.fputype in [fpu_68881]) then
  946. begin
  947. cgpara.check_simple_location;
  948. { FIXME: 68k cg really needs to support 2 byte stack alignment, otherwise the "Extended"
  949. floating point type cannot work (KB) }
  950. reference_reset_base(ref, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  951. ref.direction := dir_dec;
  952. list.concat(taicpu.op_reg_ref(A_FMOVE,tcgsize2opsize[cgpara.location^.size],reg,ref));
  953. end
  954. else
  955. inherited a_loadfpu_reg_cgpara(list,size,reg,cgpara);
  956. end;
  957. procedure tcg68k.a_loadfpu_ref_cgpara(list : TAsmList; size : tcgsize;const ref : treference;const cgpara : TCGPara);
  958. var
  959. href : treference;
  960. fref : treference;
  961. freg : tregister;
  962. begin
  963. if current_settings.fputype = fpu_soft then
  964. case cgpara.location^.loc of
  965. LOC_REFERENCE,LOC_CREFERENCE:
  966. begin
  967. case size of
  968. OS_F64:
  969. cg64.a_load64_ref_cgpara(list,ref,cgpara);
  970. OS_F32:
  971. a_load_ref_cgpara(list,size,ref,cgpara);
  972. else
  973. internalerror(2013021201);
  974. end;
  975. end;
  976. else
  977. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  978. end
  979. else
  980. if use_push(cgpara) and (current_settings.fputype in [fpu_68881]) then
  981. begin
  982. fref:=ref;
  983. fixref(list,fref);
  984. { fmove can't do <ea> -> <ea>, so move it to an fpreg first }
  985. freg:=getfpuregister(list,size);
  986. a_loadfpu_ref_reg(list,size,size,fref,freg);
  987. reference_reset_base(href, NR_STACK_POINTER_REG, 0, cgpara.alignment);
  988. href.direction := dir_dec;
  989. list.concat(taicpu.op_reg_ref(A_FMOVE,tcgsize2opsize[cgpara.location^.size],freg,href));
  990. end
  991. else
  992. begin
  993. //list.concat(tai_comment.create(strpnew('a_loadfpu_ref_cgpara inherited')));
  994. inherited a_loadfpu_ref_cgpara(list,size,ref,cgpara);
  995. end;
  996. end;
  997. procedure tcg68k.a_op_const_reg(list : TAsmList; Op: TOpCG; size: tcgsize; a: tcgint; reg: TRegister);
  998. var
  999. scratch_reg : tregister;
  1000. scratch_reg2: tregister;
  1001. opcode : tasmop;
  1002. begin
  1003. optimize_op_const(size, op, a);
  1004. opcode := topcg2tasmop[op];
  1005. case op of
  1006. OP_NONE :
  1007. begin
  1008. { Opcode is optimized away }
  1009. end;
  1010. OP_MOVE :
  1011. begin
  1012. { Optimized, replaced with a simple load }
  1013. a_load_const_reg(list,size,a,reg);
  1014. end;
  1015. OP_ADD,
  1016. OP_SUB:
  1017. begin
  1018. { add/sub works the same way, so have it unified here }
  1019. if (a >= 1) and (a <= 8) then
  1020. if (op = OP_ADD) then
  1021. opcode:=A_ADDQ
  1022. else
  1023. opcode:=A_SUBQ;
  1024. list.concat(taicpu.op_const_reg(opcode, S_L, a, reg));
  1025. end;
  1026. OP_AND,
  1027. OP_OR,
  1028. OP_XOR:
  1029. begin
  1030. scratch_reg := force_to_dataregister(list, size, reg);
  1031. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1032. move_if_needed(list, size, scratch_reg, reg);
  1033. end;
  1034. OP_DIV,
  1035. OP_IDIV:
  1036. begin
  1037. internalerror(20020816);
  1038. end;
  1039. OP_MUL,
  1040. OP_IMUL:
  1041. begin
  1042. { NOTE: better have this as fast as possible on every CPU in all cases,
  1043. because the compiler uses OP_IMUL for array indexing... (KB) }
  1044. { ColdFire doesn't support MULS/MULU <imm>,dX }
  1045. if current_settings.cputype in cpu_coldfire then
  1046. begin
  1047. { move const to a register first }
  1048. scratch_reg := getintregister(list,OS_INT);
  1049. a_load_const_reg(list, size, a, scratch_reg);
  1050. { do the multiplication }
  1051. scratch_reg2 := force_to_dataregister(list, size, reg);
  1052. sign_extend(list, size, scratch_reg2);
  1053. list.concat(taicpu.op_reg_reg(opcode,S_L,scratch_reg,scratch_reg2));
  1054. { move the value back to the original register }
  1055. move_if_needed(list, size, scratch_reg2, reg);
  1056. end
  1057. else
  1058. begin
  1059. if current_settings.cputype = cpu_mc68020 then
  1060. begin
  1061. { do the multiplication }
  1062. scratch_reg := force_to_dataregister(list, size, reg);
  1063. sign_extend(list, size, scratch_reg);
  1064. list.concat(taicpu.op_const_reg(opcode,S_L,a,scratch_reg));
  1065. { move the value back to the original register }
  1066. move_if_needed(list, size, scratch_reg, reg);
  1067. end
  1068. else
  1069. { Fallback branch, plain 68000 for now }
  1070. { FIX ME: this is slow as hell, but original 68000 doesn't have 32x32 -> 32bit MUL (KB) }
  1071. if op = OP_MUL then
  1072. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_dword')
  1073. else
  1074. call_rtl_mul_const_reg(list, size, a, reg,'fpc_mul_longint');
  1075. end;
  1076. end;
  1077. OP_ROL,
  1078. OP_ROR,
  1079. OP_SAR,
  1080. OP_SHL,
  1081. OP_SHR :
  1082. begin
  1083. scratch_reg := force_to_dataregister(list, size, reg);
  1084. sign_extend(list, size, scratch_reg);
  1085. { some special cases which can generate smarter code
  1086. using the SWAP instruction }
  1087. if (a = 16) then
  1088. begin
  1089. if (op = OP_SHL) then
  1090. begin
  1091. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1092. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1093. end
  1094. else if (op = OP_SHR) then
  1095. begin
  1096. list.concat(taicpu.op_reg(A_CLR,S_W,scratch_reg));
  1097. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1098. end
  1099. else if (op = OP_SAR) then
  1100. begin
  1101. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg));
  1102. list.concat(taicpu.op_reg(A_EXT,S_L,scratch_reg));
  1103. end
  1104. else if (op = OP_ROR) or (op = OP_ROL) then
  1105. list.concat(taicpu.op_reg(A_SWAP,S_NO,scratch_reg))
  1106. end
  1107. else if (a >= 1) and (a <= 8) then
  1108. begin
  1109. list.concat(taicpu.op_const_reg(opcode, S_L, a, scratch_reg));
  1110. end
  1111. else if (a >= 9) and (a < 16) then
  1112. begin
  1113. { Use two ops instead of const -> reg + shift with reg, because
  1114. this way is the same in length and speed but has less register
  1115. pressure }
  1116. list.concat(taicpu.op_const_reg(opcode, S_L, 8, scratch_reg));
  1117. list.concat(taicpu.op_const_reg(opcode, S_L, a-8, scratch_reg));
  1118. end
  1119. else
  1120. begin
  1121. { move const to a register first }
  1122. scratch_reg2 := getintregister(list,OS_INT);
  1123. a_load_const_reg(list, size, a, scratch_reg2);
  1124. { do the operation }
  1125. list.concat(taicpu.op_reg_reg(opcode, S_L, scratch_reg2, scratch_reg));
  1126. end;
  1127. { move the value back to the original register }
  1128. move_if_needed(list, size, scratch_reg, reg);
  1129. end;
  1130. else
  1131. internalerror(20020729);
  1132. end;
  1133. end;
  1134. procedure tcg68k.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1135. var
  1136. opcode: tasmop;
  1137. opsize: topsize;
  1138. href : treference;
  1139. begin
  1140. optimize_op_const(size, op, a);
  1141. opcode := topcg2tasmop[op];
  1142. opsize := TCGSize2OpSize[size];
  1143. { on ColdFire all arithmetic operations are only possible on 32bit }
  1144. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)
  1145. and not (op in [OP_NONE,OP_MOVE])) then
  1146. begin
  1147. inherited;
  1148. exit;
  1149. end;
  1150. case op of
  1151. OP_NONE :
  1152. begin
  1153. { opcode was optimized away }
  1154. end;
  1155. OP_MOVE :
  1156. begin
  1157. { Optimized, replaced with a simple load }
  1158. a_load_const_ref(list,size,a,ref);
  1159. end;
  1160. OP_ADD,
  1161. OP_SUB :
  1162. begin
  1163. href:=ref;
  1164. fixref(list,href);
  1165. { add/sub works the same way, so have it unified here }
  1166. if (a >= 1) and (a <= 8) then
  1167. begin
  1168. if (op = OP_ADD) then
  1169. opcode:=A_ADDQ
  1170. else
  1171. opcode:=A_SUBQ;
  1172. list.concat(taicpu.op_const_ref(opcode, opsize, a, href));
  1173. end
  1174. else
  1175. if not(current_settings.cputype in cpu_coldfire) then
  1176. list.concat(taicpu.op_const_ref(opcode, opsize, a, href))
  1177. else
  1178. { on ColdFire, ADDI/SUBI cannot act on memory
  1179. so we can only go through a register }
  1180. inherited;
  1181. end;
  1182. else begin
  1183. // list.concat(tai_comment.create(strpnew('a_op_const_ref inherited')));
  1184. inherited;
  1185. end;
  1186. end;
  1187. end;
  1188. procedure tcg68k.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1189. var
  1190. hreg1, hreg2: tregister;
  1191. opcode : tasmop;
  1192. opsize : topsize;
  1193. begin
  1194. opcode := topcg2tasmop[op];
  1195. if current_settings.cputype in cpu_coldfire then
  1196. opsize := S_L
  1197. else
  1198. opsize := TCGSize2OpSize[size];
  1199. case op of
  1200. OP_ADD,
  1201. OP_SUB:
  1202. begin
  1203. if current_settings.cputype in cpu_coldfire then
  1204. begin
  1205. { operation only allowed only a longword }
  1206. sign_extend(list, size, src);
  1207. sign_extend(list, size, dst);
  1208. end;
  1209. list.concat(taicpu.op_reg_reg(opcode, opsize, src, dst));
  1210. end;
  1211. OP_AND,OP_OR,
  1212. OP_SAR,OP_SHL,
  1213. OP_SHR,OP_XOR:
  1214. begin
  1215. { load to data registers }
  1216. hreg1 := force_to_dataregister(list, size, src);
  1217. hreg2 := force_to_dataregister(list, size, dst);
  1218. if current_settings.cputype in cpu_coldfire then
  1219. begin
  1220. { operation only allowed only a longword }
  1221. {!***************************************
  1222. in the case of shifts, the value to
  1223. shift by, should already be valid, so
  1224. no need to sign extend the value
  1225. !
  1226. }
  1227. if op in [OP_AND,OP_OR,OP_XOR] then
  1228. sign_extend(list, size, hreg1);
  1229. sign_extend(list, size, hreg2);
  1230. end;
  1231. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1232. { move back result into destination register }
  1233. move_if_needed(list, size, hreg2, dst);
  1234. end;
  1235. OP_DIV,
  1236. OP_IDIV :
  1237. begin
  1238. internalerror(20020816);
  1239. end;
  1240. OP_MUL,
  1241. OP_IMUL:
  1242. begin
  1243. if (current_settings.cputype <> cpu_mc68020) and
  1244. (not (current_settings.cputype in cpu_coldfire)) then
  1245. if op = OP_MUL then
  1246. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_dword')
  1247. else
  1248. call_rtl_mul_reg_reg(list,src,dst,'fpc_mul_longint')
  1249. else
  1250. begin
  1251. { 68020+ and ColdFire codepath, probably could be improved }
  1252. hreg1 := force_to_dataregister(list, size, src);
  1253. hreg2 := force_to_dataregister(list, size, dst);
  1254. sign_extend(list, size, hreg1);
  1255. sign_extend(list, size, hreg2);
  1256. list.concat(taicpu.op_reg_reg(opcode, opsize, hreg1, hreg2));
  1257. { move back result into destination register }
  1258. move_if_needed(list, size, hreg2, dst);
  1259. end;
  1260. end;
  1261. OP_NEG,
  1262. OP_NOT :
  1263. begin
  1264. { if there are two operands, move the register,
  1265. since the operation will only be done on the result
  1266. register. }
  1267. if (src<>dst) then
  1268. a_load_reg_reg(list,size,size,src,dst);
  1269. hreg2 := force_to_dataregister(list, size, dst);
  1270. { coldfire only supports long version }
  1271. if current_settings.cputype in cpu_ColdFire then
  1272. sign_extend(list, size, hreg2);
  1273. list.concat(taicpu.op_reg(opcode, opsize, hreg2));
  1274. { move back the result to the result register if needed }
  1275. move_if_needed(list, size, hreg2, dst);
  1276. end;
  1277. else
  1278. internalerror(20020729);
  1279. end;
  1280. end;
  1281. procedure tcg68k.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize; reg: TRegister; const ref: TReference);
  1282. var
  1283. opcode : tasmop;
  1284. opsize : topsize;
  1285. href : treference;
  1286. hreg : tregister;
  1287. begin
  1288. opcode := topcg2tasmop[op];
  1289. opsize := TCGSize2OpSize[size];
  1290. { on ColdFire all arithmetic operations are only possible on 32bit
  1291. and addressing modes are limited }
  1292. if ((current_settings.cputype in cpu_coldfire) and (opsize <> S_L)) then
  1293. begin
  1294. inherited;
  1295. exit;
  1296. end;
  1297. case op of
  1298. OP_ADD,
  1299. OP_SUB :
  1300. begin
  1301. href:=ref;
  1302. fixref(list,href);
  1303. { areg -> ref arithmetic operations are impossible on 68k }
  1304. hreg:=force_to_dataregister(list,size,reg);
  1305. { add/sub works the same way, so have it unified here }
  1306. list.concat(taicpu.op_reg_ref(opcode, opsize, hreg, href));
  1307. end;
  1308. else begin
  1309. // list.concat(tai_comment.create(strpnew('a_op_reg_ref inherited')));
  1310. inherited;
  1311. end;
  1312. end;
  1313. end;
  1314. procedure tcg68k.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1315. l : tasmlabel);
  1316. var
  1317. hregister : tregister;
  1318. instr : taicpu;
  1319. need_temp_reg : boolean;
  1320. temp_size: topsize;
  1321. begin
  1322. need_temp_reg := false;
  1323. { plain 68000 doesn't support address registers for TST }
  1324. need_temp_reg := (current_settings.cputype = cpu_mc68000) and
  1325. (a = 0) and isaddressregister(reg);
  1326. { ColdFire doesn't support address registers for CMPI }
  1327. need_temp_reg := need_temp_reg or ((current_settings.cputype in cpu_coldfire)
  1328. and (a <> 0) and isaddressregister(reg));
  1329. if need_temp_reg then
  1330. begin
  1331. hregister := getintregister(list,OS_INT);
  1332. temp_size := TCGSize2OpSize[size];
  1333. if temp_size < S_W then
  1334. temp_size := S_W;
  1335. instr:=taicpu.op_reg_reg(A_MOVE,temp_size,reg,hregister);
  1336. add_move_instruction(instr);
  1337. list.concat(instr);
  1338. reg := hregister;
  1339. { do sign extension if size had to be modified }
  1340. if temp_size <> TCGSize2OpSize[size] then
  1341. begin
  1342. sign_extend(list, size, reg);
  1343. size:=OS_INT;
  1344. end;
  1345. end;
  1346. if a = 0 then
  1347. list.concat(taicpu.op_reg(A_TST,TCGSize2OpSize[size],reg))
  1348. else
  1349. begin
  1350. { ColdFire ISA A also needs S_L for CMPI }
  1351. { Note: older QEMU pukes from CMPI sizes <> .L even on ISA B/C, but
  1352. it's actually *LEGAL*, see CFPRM, page 4-30, the bug also seems
  1353. fixed in recent QEMU, but only when CPU cfv4e is forced, not by
  1354. default. (KB) }
  1355. if current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]} then
  1356. begin
  1357. sign_extend(list, size, reg);
  1358. size:=OS_INT;
  1359. end;
  1360. list.concat(taicpu.op_const_reg(A_CMPI,TCGSize2OpSize[size],a,reg));
  1361. end;
  1362. { emit the actual jump to the label }
  1363. a_jmp_cond(list,cmp_op,l);
  1364. end;
  1365. procedure tcg68k.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference; l : tasmlabel);
  1366. var
  1367. tmpref: treference;
  1368. begin
  1369. { optimize for usage of TST here, so ref compares against zero, which is the
  1370. most common case by far in the RTL code at least (KB) }
  1371. if (a = 0) then
  1372. begin
  1373. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label with TST')));
  1374. tmpref:=ref;
  1375. fixref(list,tmpref);
  1376. list.concat(taicpu.op_ref(A_TST,tcgsize2opsize[size],tmpref));
  1377. a_jmp_cond(list,cmp_op,l);
  1378. end
  1379. else
  1380. begin
  1381. //list.concat(tai_comment.create(strpnew('a_cmp_const_ref_label inherited')));
  1382. inherited;
  1383. end;
  1384. end;
  1385. procedure tcg68k.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1386. begin
  1387. if (current_settings.cputype in cpu_coldfire-[cpu_isa_b,cpu_isa_c]) then
  1388. begin
  1389. sign_extend(list,size,reg1);
  1390. sign_extend(list,size,reg2);
  1391. size:=OS_INT;
  1392. end;
  1393. list.concat(taicpu.op_reg_reg(A_CMP,tcgsize2opsize[size],reg1,reg2));
  1394. { emit the actual jump to the label }
  1395. a_jmp_cond(list,cmp_op,l);
  1396. end;
  1397. procedure tcg68k.a_jmp_name(list: TAsmList; const s: string);
  1398. var
  1399. ai: taicpu;
  1400. begin
  1401. ai := Taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s));
  1402. ai.is_jmp := true;
  1403. list.concat(ai);
  1404. end;
  1405. procedure tcg68k.a_jmp_always(list : TAsmList;l: tasmlabel);
  1406. var
  1407. ai: taicpu;
  1408. begin
  1409. ai := Taicpu.op_sym(A_JMP,S_NO,l);
  1410. ai.is_jmp := true;
  1411. list.concat(ai);
  1412. end;
  1413. procedure tcg68k.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1414. var
  1415. ai : taicpu;
  1416. begin
  1417. ai := Taicpu.op_sym(A_BXX,S_NO,l);
  1418. ai.SetCondition(flags_to_cond(f));
  1419. ai.is_jmp := true;
  1420. list.concat(ai);
  1421. end;
  1422. procedure tcg68k.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1423. var
  1424. ai : taicpu;
  1425. hreg : tregister;
  1426. instr : taicpu;
  1427. begin
  1428. { move to a Dx register? }
  1429. if (isaddressregister(reg)) then
  1430. hreg:=getintregister(list,OS_INT)
  1431. else
  1432. hreg:=reg;
  1433. ai:=Taicpu.Op_reg(A_Sxx,S_B,hreg);
  1434. ai.SetCondition(flags_to_cond(f));
  1435. list.concat(ai);
  1436. { Scc stores a complete byte of 1s, but the compiler expects only one
  1437. bit set, so ensure this is the case }
  1438. list.concat(taicpu.op_const_reg(A_AND,S_L,1,hreg));
  1439. if hreg<>reg then
  1440. begin
  1441. instr:=taicpu.op_reg_reg(A_MOVE,S_L,hreg,reg);
  1442. add_move_instruction(instr);
  1443. list.concat(instr);
  1444. end;
  1445. end;
  1446. procedure tcg68k.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  1447. var
  1448. helpsize : longint;
  1449. i : byte;
  1450. hregister : tregister;
  1451. iregister : tregister;
  1452. jregister : tregister;
  1453. hp1 : treference;
  1454. hp2 : treference;
  1455. hl : tasmlabel;
  1456. srcref,dstref : treference;
  1457. begin
  1458. hregister := getintregister(list,OS_INT);
  1459. { from 12 bytes movs is being used }
  1460. if ((len<=8) or (not(cs_opt_size in current_settings.optimizerswitches) and (len<=12))) then
  1461. begin
  1462. srcref := source;
  1463. dstref := dest;
  1464. helpsize:=len div 4;
  1465. { move a dword x times }
  1466. for i:=1 to helpsize do
  1467. begin
  1468. a_load_ref_reg(list,OS_INT,OS_INT,srcref,hregister);
  1469. a_load_reg_ref(list,OS_INT,OS_INT,hregister,dstref);
  1470. inc(srcref.offset,4);
  1471. inc(dstref.offset,4);
  1472. dec(len,4);
  1473. end;
  1474. { move a word }
  1475. if len>1 then
  1476. begin
  1477. a_load_ref_reg(list,OS_16,OS_16,srcref,hregister);
  1478. a_load_reg_ref(list,OS_16,OS_16,hregister,dstref);
  1479. inc(srcref.offset,2);
  1480. inc(dstref.offset,2);
  1481. dec(len,2);
  1482. end;
  1483. { move a single byte }
  1484. if len>0 then
  1485. begin
  1486. a_load_ref_reg(list,OS_8,OS_8,srcref,hregister);
  1487. a_load_reg_ref(list,OS_8,OS_8,hregister,dstref);
  1488. end
  1489. end
  1490. else
  1491. begin
  1492. iregister:=getaddressregister(list);
  1493. jregister:=getaddressregister(list);
  1494. { reference for move (An)+,(An)+ }
  1495. reference_reset(hp1,source.alignment);
  1496. hp1.base := iregister; { source register }
  1497. hp1.direction := dir_inc;
  1498. reference_reset(hp2,dest.alignment);
  1499. hp2.base := jregister;
  1500. hp2.direction := dir_inc;
  1501. { iregister = source }
  1502. { jregister = destination }
  1503. a_loadaddr_ref_reg(list,source,iregister);
  1504. a_loadaddr_ref_reg(list,dest,jregister);
  1505. { double word move only on 68020+ machines }
  1506. { because of possible alignment problems }
  1507. { use fast loop mode }
  1508. if (current_settings.cputype=cpu_MC68020) then
  1509. begin
  1510. //list.concat(tai_comment.create(strpnew('g_concatcopy tight copy loop 020+')));
  1511. helpsize := len - len mod 4;
  1512. len := len mod 4;
  1513. a_load_const_reg(list,OS_INT,(helpsize div 4)-1,hregister);
  1514. current_asmdata.getjumplabel(hl);
  1515. a_label(list,hl);
  1516. list.concat(taicpu.op_ref_ref(A_MOVE,S_L,hp1,hp2));
  1517. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1518. if len > 1 then
  1519. begin
  1520. dec(len,2);
  1521. list.concat(taicpu.op_ref_ref(A_MOVE,S_W,hp1,hp2));
  1522. end;
  1523. if len = 1 then
  1524. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1525. end
  1526. else
  1527. begin
  1528. { Fast 68010 loop mode with no possible alignment problems }
  1529. //list.concat(tai_comment.create(strpnew('g_concatcopy tight byte copy loop')));
  1530. a_load_const_reg(list,OS_INT,len - 1,hregister);
  1531. current_asmdata.getjumplabel(hl);
  1532. a_label(list,hl);
  1533. list.concat(taicpu.op_ref_ref(A_MOVE,S_B,hp1,hp2));
  1534. if current_settings.cputype in cpu_coldfire then
  1535. begin
  1536. { Coldfire does not support DBRA }
  1537. list.concat(taicpu.op_const_reg(A_SUBQ,S_L,1,hregister));
  1538. list.concat(taicpu.op_sym(A_BPL,S_NO,hl));
  1539. end
  1540. else
  1541. list.concat(taicpu.op_reg_sym(A_DBRA,S_L,hregister,hl));
  1542. end;
  1543. end;
  1544. end;
  1545. procedure tcg68k.g_overflowcheck(list: TAsmList; const l:tlocation; def:tdef);
  1546. var
  1547. hl : tasmlabel;
  1548. ai : taicpu;
  1549. cond : TAsmCond;
  1550. begin
  1551. if not(cs_check_overflow in current_settings.localswitches) then
  1552. exit;
  1553. current_asmdata.getjumplabel(hl);
  1554. if not ((def.typ=pointerdef) or
  1555. ((def.typ=orddef) and
  1556. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1557. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1558. cond:=C_VC
  1559. else
  1560. cond:=C_CC;
  1561. ai:=Taicpu.Op_Sym(A_Bxx,S_NO,hl);
  1562. ai.SetCondition(cond);
  1563. ai.is_jmp:=true;
  1564. list.concat(ai);
  1565. a_call_name(list,'FPC_OVERFLOW',false);
  1566. a_label(list,hl);
  1567. end;
  1568. procedure tcg68k.g_proc_entry(list: TAsmList; localsize: longint; nostackframe:boolean);
  1569. begin
  1570. { Carl's original code used 2x MOVE instead of LINK when localsize = 0.
  1571. However, a LINK seems faster than two moves on everything from 68000
  1572. to '060, so the two move branch here was dropped. (KB) }
  1573. if not nostackframe then
  1574. begin
  1575. { size can't be negative }
  1576. if (localsize < 0) then
  1577. internalerror(2006122601);
  1578. if (localsize > high(smallint)) then
  1579. begin
  1580. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,0));
  1581. list.concat(taicpu.op_const_reg(A_SUBA,S_L,localsize,NR_STACK_POINTER_REG));
  1582. end
  1583. else
  1584. list.concat(taicpu.op_reg_const(A_LINK,S_W,NR_FRAME_POINTER_REG,-localsize));
  1585. end;
  1586. end;
  1587. procedure tcg68k.g_proc_exit(list : TAsmList; parasize: longint; nostackframe: boolean);
  1588. var
  1589. r,hregister : TRegister;
  1590. ref : TReference;
  1591. ref2: TReference;
  1592. begin
  1593. if not nostackframe then
  1594. begin
  1595. list.concat(taicpu.op_reg(A_UNLK,S_NO,NR_FRAME_POINTER_REG));
  1596. { if parasize is less than zero here, we probably have a cdecl function.
  1597. According to the info here: http://www.makestuff.eu/wordpress/gcc-68000-abi/
  1598. 68k GCC uses two different methods to free the stack, depending if the target
  1599. architecture supports RTD or not, and one does callee side, the other does
  1600. caller side free, which looks like a PITA to support. We have to figure this
  1601. out later. More info welcomed. (KB) }
  1602. if (parasize > 0) and not (current_procinfo.procdef.proccalloption in clearstack_pocalls) then
  1603. begin
  1604. if current_settings.cputype=cpu_mc68020 then
  1605. list.concat(taicpu.op_const(A_RTD,S_NO,parasize))
  1606. else
  1607. begin
  1608. { We must pull the PC Counter from the stack, before }
  1609. { restoring the stack pointer, otherwise the PC would }
  1610. { point to nowhere! }
  1611. { Instead of doing a slow copy of the return address while trying }
  1612. { to feed it to the RTS instruction, load the PC to A0 (scratch reg) }
  1613. { then free up the stack allocated for paras, then use a JMP (A0) to }
  1614. { return to the caller with the paras freed. (KB) }
  1615. hregister:=NR_A0;
  1616. cg.a_reg_alloc(list,hregister);
  1617. reference_reset_base(ref,NR_STACK_POINTER_REG,0,4);
  1618. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,ref,hregister));
  1619. { instead of using a postincrement above (which also writes the }
  1620. { stackpointer reg) simply add 4 to the parasize, the instructions }
  1621. { below then take that size into account as well, so SP reg is only }
  1622. { written once (KB) }
  1623. parasize:=parasize+4;
  1624. r:=NR_SP;
  1625. { can we do a quick addition ... }
  1626. if (parasize < 9) then
  1627. list.concat(taicpu.op_const_reg(A_ADDQ,S_L,parasize,r))
  1628. else { nope ... }
  1629. begin
  1630. reference_reset_base(ref2,NR_STACK_POINTER_REG,parasize,4);
  1631. list.concat(taicpu.op_ref_reg(A_LEA,S_NO,ref2,r));
  1632. end;
  1633. reference_reset_base(ref,hregister,0,4);
  1634. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1635. end;
  1636. end
  1637. else
  1638. list.concat(taicpu.op_none(A_RTS,S_NO));
  1639. end
  1640. else
  1641. begin
  1642. list.concat(taicpu.op_none(A_RTS,S_NO));
  1643. end;
  1644. { Routines with the poclearstack flag set use only a ret.
  1645. also routines with parasize=0 }
  1646. { TODO: figure out if these are still relevant to us (KB) }
  1647. (*
  1648. if current_procinfo.procdef.proccalloption in clearstack_pocalls then
  1649. begin
  1650. { complex return values are removed from stack in C code PM }
  1651. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  1652. list.concat(taicpu.op_const(A_RTD,S_NO,4))
  1653. else
  1654. list.concat(taicpu.op_none(A_RTS,S_NO));
  1655. end
  1656. else if (parasize=0) then
  1657. begin
  1658. list.concat(taicpu.op_none(A_RTS,S_NO));
  1659. end
  1660. else
  1661. *)
  1662. end;
  1663. procedure tcg68k.g_save_registers(list:TAsmList);
  1664. var
  1665. dataregs: tcpuregisterset;
  1666. addrregs: tcpuregisterset;
  1667. fpuregs: tcpuregisterset;
  1668. href : treference;
  1669. hreg : tregister;
  1670. hfreg : tregister;
  1671. size : longint;
  1672. fsize : longint;
  1673. r : integer;
  1674. begin
  1675. { The code generated by the section below, particularly the movem.l
  1676. instruction is known to cause an issue when compiled by some GNU
  1677. assembler versions (I had it with 2.17, while 2.24 seems OK.)
  1678. when you run into this problem, just call inherited here instead
  1679. to skip the movem.l generation. But better just use working GNU
  1680. AS version instead. (KB) }
  1681. dataregs:=[];
  1682. addrregs:=[];
  1683. fpuregs:=[];
  1684. { calculate temp. size }
  1685. size:=0;
  1686. fsize:=0;
  1687. hreg:=NR_NO;
  1688. hfreg:=NR_NO;
  1689. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1690. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1691. begin
  1692. hreg:=newreg(R_INTREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1693. inc(size,sizeof(aint));
  1694. dataregs:=dataregs + [saved_standard_registers[r]];
  1695. end;
  1696. if uses_registers(R_ADDRESSREGISTER) then
  1697. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1698. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1699. begin
  1700. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1701. inc(size,sizeof(aint));
  1702. addrregs:=addrregs + [saved_address_registers[r]];
  1703. end;
  1704. if uses_registers(R_FPUREGISTER) then
  1705. for r:=low(saved_fpu_registers) to high(saved_fpu_registers) do
  1706. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1707. begin
  1708. hfreg:=newreg(R_FPUREGISTER,saved_fpu_registers[r],R_SUBWHOLE);
  1709. inc(fsize,12{sizeof(extended)});
  1710. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1711. end;
  1712. { 68k has no MM registers }
  1713. if uses_registers(R_MMREGISTER) then
  1714. internalerror(2014030201);
  1715. if (size+fsize) > 0 then
  1716. begin
  1717. tg.GetTemp(list,size+fsize,sizeof(aint),tt_noreuse,current_procinfo.save_regs_ref);
  1718. include(current_procinfo.flags,pi_has_saved_regs);
  1719. { Copy registers to temp }
  1720. { NOTE: virtual registers allocated here won't be translated --> no higher-level stuff. }
  1721. href:=current_procinfo.save_regs_ref;
  1722. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1723. begin
  1724. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1725. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1726. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1727. end;
  1728. if size > 0 then
  1729. if size = sizeof(aint) then
  1730. list.concat(taicpu.op_reg_ref(A_MOVE,S_L,hreg,href))
  1731. else
  1732. list.concat(taicpu.op_regset_ref(A_MOVEM,S_L,dataregs,addrregs,[],href));
  1733. if fsize > 0 then
  1734. begin
  1735. { size is always longword aligned, while fsize is not }
  1736. inc(href.offset,size);
  1737. if fsize = 12{sizeof(extended)} then
  1738. list.concat(taicpu.op_reg_ref(A_FMOVE,S_FX,hfreg,href))
  1739. else
  1740. list.concat(taicpu.op_regset_ref(A_FMOVEM,S_FX,[],[],fpuregs,href));
  1741. end;
  1742. end;
  1743. end;
  1744. procedure tcg68k.g_restore_registers(list:TAsmList);
  1745. var
  1746. dataregs: tcpuregisterset;
  1747. addrregs: tcpuregisterset;
  1748. fpuregs : tcpuregisterset;
  1749. href : treference;
  1750. r : integer;
  1751. hreg : tregister;
  1752. hfreg : tregister;
  1753. size : longint;
  1754. fsize : longint;
  1755. begin
  1756. { see the remark about buggy GNU AS versions in g_save_registers() (KB) }
  1757. dataregs:=[];
  1758. addrregs:=[];
  1759. fpuregs:=[];
  1760. if not(pi_has_saved_regs in current_procinfo.flags) then
  1761. exit;
  1762. { Copy registers from temp }
  1763. size:=0;
  1764. fsize:=0;
  1765. hreg:=NR_NO;
  1766. hfreg:=NR_NO;
  1767. for r:=low(saved_standard_registers) to high(saved_standard_registers) do
  1768. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  1769. begin
  1770. inc(size,sizeof(aint));
  1771. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  1772. { Allocate register so the optimizer does not remove the load }
  1773. a_reg_alloc(list,hreg);
  1774. dataregs:=dataregs + [saved_standard_registers[r]];
  1775. end;
  1776. if uses_registers(R_ADDRESSREGISTER) then
  1777. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1778. if saved_address_registers[r] in rg[R_ADDRESSREGISTER].used_in_proc then
  1779. begin
  1780. inc(size,sizeof(aint));
  1781. hreg:=newreg(R_ADDRESSREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1782. { Allocate register so the optimizer does not remove the load }
  1783. a_reg_alloc(list,hreg);
  1784. addrregs:=addrregs + [saved_address_registers[r]];
  1785. end;
  1786. if uses_registers(R_FPUREGISTER) then
  1787. for r:=low(saved_address_registers) to high(saved_address_registers) do
  1788. if saved_fpu_registers[r] in rg[R_FPUREGISTER].used_in_proc then
  1789. begin
  1790. inc(fsize,12{sizeof(extended)});
  1791. hfreg:=newreg(R_FPUREGISTER,saved_address_registers[r],R_SUBWHOLE);
  1792. { Allocate register so the optimizer does not remove the load }
  1793. a_reg_alloc(list,hfreg);
  1794. fpuregs:=fpuregs + [saved_fpu_registers[r]];
  1795. end;
  1796. { 68k has no MM registers }
  1797. if uses_registers(R_MMREGISTER) then
  1798. internalerror(2014030202);
  1799. { Restore registers from temp }
  1800. href:=current_procinfo.save_regs_ref;
  1801. if (href.offset<low(smallint)) and (current_settings.cputype in cpu_coldfire) then
  1802. begin
  1803. list.concat(taicpu.op_reg_reg(A_MOVE,S_L,href.base,NR_A0));
  1804. list.concat(taicpu.op_const_reg(A_ADDA,S_L,href.offset,NR_A0));
  1805. reference_reset_base(href,NR_A0,0,sizeof(pint));
  1806. end;
  1807. if size > 0 then
  1808. if size = sizeof(aint) then
  1809. list.concat(taicpu.op_ref_reg(A_MOVE,S_L,href,hreg))
  1810. else
  1811. list.concat(taicpu.op_ref_regset(A_MOVEM,S_L,href,dataregs,addrregs,[]));
  1812. if fsize > 0 then
  1813. begin
  1814. { size is always longword aligned, while fsize is not }
  1815. inc(href.offset,size);
  1816. if fsize = 12{sizeof(extended)} then
  1817. list.concat(taicpu.op_ref_reg(A_FMOVE,S_FX,href,hfreg))
  1818. else
  1819. list.concat(taicpu.op_ref_regset(A_FMOVEM,S_FX,href,[],[],fpuregs));
  1820. end;
  1821. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  1822. end;
  1823. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; _newsize : tcgsize; reg: tregister);
  1824. begin
  1825. case _newsize of
  1826. OS_S16, OS_16:
  1827. case _oldsize of
  1828. OS_S8:
  1829. begin { 8 -> 16 bit sign extend }
  1830. if (isaddressregister(reg)) then
  1831. internalerror(2014031201);
  1832. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1833. end;
  1834. OS_8: { 8 -> 16 bit zero extend }
  1835. begin
  1836. if (current_settings.cputype in cpu_coldfire) then
  1837. { ColdFire has no ANDI.W }
  1838. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg))
  1839. else
  1840. list.concat(taicpu.op_const_reg(A_AND,S_W,$FF,reg));
  1841. end;
  1842. end;
  1843. OS_S32, OS_32:
  1844. case _oldsize of
  1845. OS_S8:
  1846. begin { 8 -> 32 bit sign extend }
  1847. if (isaddressregister(reg)) then
  1848. internalerror(2014031202);
  1849. if (current_settings.cputype = cpu_MC68000) then
  1850. begin
  1851. list.concat(taicpu.op_reg(A_EXT,S_W,reg));
  1852. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1853. end
  1854. else
  1855. begin
  1856. //list.concat(tai_comment.create(strpnew('sign extend byte')));
  1857. list.concat(taicpu.op_reg(A_EXTB,S_L,reg));
  1858. end;
  1859. end;
  1860. OS_8: { 8 -> 32 bit zero extend }
  1861. begin
  1862. if (isaddressregister(reg)) then
  1863. internalerror(2015031501);
  1864. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1865. list.concat(taicpu.op_const_reg(A_AND,S_L,$FF,reg));
  1866. end;
  1867. OS_S16: { 16 -> 32 bit sign extend }
  1868. begin
  1869. { address registers are sign-extended from 16->32 bit anyway
  1870. automagically on every W operation by the CPU, so this is a NOP }
  1871. if not isaddressregister(reg) then
  1872. begin
  1873. //list.concat(tai_comment.create(strpnew('sign extend word')));
  1874. list.concat(taicpu.op_reg(A_EXT,S_L,reg));
  1875. end;
  1876. end;
  1877. OS_16:
  1878. begin
  1879. if (isaddressregister(reg)) then
  1880. internalerror(2015031502);
  1881. //list.concat(tai_comment.create(strpnew('zero extend byte')));
  1882. list.concat(taicpu.op_const_reg(A_AND,S_L,$FFFF,reg));
  1883. end;
  1884. end;
  1885. end; { otherwise the size is already correct }
  1886. end;
  1887. procedure tcg68k.sign_extend(list: TAsmList;_oldsize : tcgsize; reg: tregister);
  1888. begin
  1889. sign_extend(list, _oldsize, OS_INT, reg);
  1890. end;
  1891. procedure tcg68k.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1892. var
  1893. ai : taicpu;
  1894. begin
  1895. if cond=OC_None then
  1896. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1897. else
  1898. begin
  1899. ai:=Taicpu.Op_sym(A_Bxx,S_NO,l);
  1900. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1901. end;
  1902. ai.is_jmp:=true;
  1903. list.concat(ai);
  1904. end;
  1905. { ensures a register is a dataregister. this is often used, as 68k can't do lots of
  1906. operations on an address register. if the register is a dataregister anyway, it
  1907. just returns it untouched.}
  1908. function tcg68k.force_to_dataregister(list: TAsmList; size: TCGSize; reg: TRegister): TRegister;
  1909. var
  1910. scratch_reg: TRegister;
  1911. instr: Taicpu;
  1912. begin
  1913. if isaddressregister(reg) then
  1914. begin
  1915. scratch_reg:=getintregister(list,OS_INT);
  1916. instr:=taicpu.op_reg_reg(A_MOVE,S_L,reg,scratch_reg);
  1917. add_move_instruction(instr);
  1918. list.concat(instr);
  1919. result:=scratch_reg;
  1920. end
  1921. else
  1922. result:=reg;
  1923. end;
  1924. { moves source register to destination register, if the two are not the same. can be used in pair
  1925. with force_to_dataregister() }
  1926. procedure tcg68k.move_if_needed(list: TAsmList; size: TCGSize; src: TRegister; dest: TRegister);
  1927. var
  1928. instr: Taicpu;
  1929. begin
  1930. if (src <> dest) then
  1931. begin
  1932. instr:=taicpu.op_reg_reg(A_MOVE,S_L,src,dest);
  1933. add_move_instruction(instr);
  1934. list.concat(instr);
  1935. end;
  1936. end;
  1937. procedure tcg68k.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1938. var
  1939. hsym : tsym;
  1940. href : treference;
  1941. paraloc : Pcgparalocation;
  1942. begin
  1943. { calculate the parameter info for the procdef }
  1944. procdef.init_paraloc_info(callerside);
  1945. hsym:=tsym(procdef.parast.Find('self'));
  1946. if not(assigned(hsym) and
  1947. (hsym.typ=paravarsym)) then
  1948. internalerror(2013100702);
  1949. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1950. while paraloc<>nil do
  1951. with paraloc^ do
  1952. begin
  1953. case loc of
  1954. LOC_REGISTER:
  1955. a_op_const_reg(list,OP_SUB,size,ioffset,register);
  1956. LOC_REFERENCE:
  1957. begin
  1958. { offset in the wrapper needs to be adjusted for the stored
  1959. return address }
  1960. reference_reset_base(href,reference.index,reference.offset+sizeof(pint),sizeof(pint));
  1961. { plain 68k could use SUBI on href directly, but this way it works on Coldfire too
  1962. and it's probably smaller code for the majority of cases (if ioffset small, the
  1963. load will use MOVEQ) (KB) }
  1964. a_load_const_reg(list,OS_ADDR,ioffset,NR_D0);
  1965. list.concat(taicpu.op_reg_ref(A_SUB,S_L,NR_D0,href));
  1966. end
  1967. else
  1968. internalerror(2013100703);
  1969. end;
  1970. paraloc:=next;
  1971. end;
  1972. end;
  1973. procedure tcg68k.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1974. begin
  1975. list.concat(taicpu.op_const_reg(A_SUB,S_L,localsize,NR_STACK_POINTER_REG));
  1976. end;
  1977. {****************************************************************************}
  1978. { TCG64F68K }
  1979. {****************************************************************************}
  1980. procedure tcg64f68k.a_op64_reg_reg(list : TAsmList;op:TOpCG;size: tcgsize; regsrc,regdst : tregister64);
  1981. var
  1982. opcode : tasmop;
  1983. xopcode : tasmop;
  1984. instr : taicpu;
  1985. begin
  1986. opcode := topcg2tasmop[op];
  1987. xopcode := topcg2tasmopx[op];
  1988. case op of
  1989. OP_ADD,OP_SUB:
  1990. begin
  1991. { if one of these three registers is an address
  1992. register, we'll really get into problems! }
  1993. if isaddressregister(regdst.reglo) or
  1994. isaddressregister(regdst.reghi) or
  1995. isaddressregister(regsrc.reghi) then
  1996. internalerror(2014030101);
  1997. list.concat(taicpu.op_reg_reg(opcode,S_L,regsrc.reglo,regdst.reglo));
  1998. list.concat(taicpu.op_reg_reg(xopcode,S_L,regsrc.reghi,regdst.reghi));
  1999. end;
  2000. OP_AND,OP_OR:
  2001. begin
  2002. { at least one of the registers must be a data register }
  2003. if (isaddressregister(regdst.reglo) and
  2004. isaddressregister(regsrc.reglo)) or
  2005. (isaddressregister(regsrc.reghi) and
  2006. isaddressregister(regdst.reghi)) then
  2007. internalerror(2014030102);
  2008. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2009. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2010. end;
  2011. { this is handled in 1st pass for 32-bit cpu's (helper call) }
  2012. OP_IDIV,OP_DIV,
  2013. OP_IMUL,OP_MUL:
  2014. internalerror(2002081701);
  2015. { this is also handled in 1st pass for 32-bit cpu's (helper call) }
  2016. OP_SAR,OP_SHL,OP_SHR:
  2017. internalerror(2002081702);
  2018. OP_XOR:
  2019. begin
  2020. if isaddressregister(regdst.reglo) or
  2021. isaddressregister(regsrc.reglo) or
  2022. isaddressregister(regsrc.reghi) or
  2023. isaddressregister(regdst.reghi) then
  2024. internalerror(2014030103);
  2025. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  2026. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  2027. end;
  2028. OP_NEG,OP_NOT:
  2029. begin
  2030. if isaddressregister(regdst.reglo) or
  2031. isaddressregister(regdst.reghi) then
  2032. internalerror(2014030104);
  2033. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reglo,regdst.reglo);
  2034. cg.add_move_instruction(instr);
  2035. list.concat(instr);
  2036. instr:=taicpu.op_reg_reg(A_MOVE,S_L,regsrc.reghi,regdst.reghi);
  2037. cg.add_move_instruction(instr);
  2038. list.concat(instr);
  2039. if (op = OP_NOT) then
  2040. xopcode:=opcode;
  2041. list.concat(taicpu.op_reg(opcode,S_L,regdst.reglo));
  2042. list.concat(taicpu.op_reg(xopcode,S_L,regdst.reghi));
  2043. end;
  2044. end; { end case }
  2045. end;
  2046. procedure tcg64f68k.a_op64_ref_reg(list : TAsmList;op:TOpCG;size : tcgsize;const ref : treference;reg : tregister64);
  2047. var
  2048. tempref : treference;
  2049. begin
  2050. case op of
  2051. OP_NEG,OP_NOT:
  2052. begin
  2053. a_load64_ref_reg(list,ref,reg);
  2054. a_op64_reg_reg(list,op,size,reg,reg);
  2055. end;
  2056. OP_AND,OP_OR:
  2057. begin
  2058. tempref:=ref;
  2059. tcg68k(cg).fixref(list,tempref);
  2060. inc(tempref.offset,4);
  2061. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reglo));
  2062. dec(tempref.offset,4);
  2063. list.concat(taicpu.op_ref_reg(topcg2tasmop[op],S_L,tempref,reg.reghi));
  2064. end;
  2065. else
  2066. { XOR does not allow reference for source; ADD/SUB do not allow reference for
  2067. high dword, although low dword can still be handled directly. }
  2068. inherited a_op64_ref_reg(list,op,size,ref,reg);
  2069. end;
  2070. end;
  2071. procedure tcg64f68k.a_op64_const_reg(list : TAsmList;op:TOpCG;size: tcgsize; value : int64;regdst : tregister64);
  2072. var
  2073. lowvalue : cardinal;
  2074. highvalue : cardinal;
  2075. opcode : tasmop;
  2076. xopcode : tasmop;
  2077. hreg : tregister;
  2078. begin
  2079. { is it optimized out ? }
  2080. { optimize64_op_const_reg doesn't seem to be used in any cg64f32 right now. why? (KB) }
  2081. { if cg.optimize64_op_const_reg(list,op,value,reg) then
  2082. exit; }
  2083. lowvalue := cardinal(value);
  2084. highvalue := value shr 32;
  2085. opcode := topcg2tasmop[op];
  2086. xopcode := topcg2tasmopx[op];
  2087. { the destination registers must be data registers }
  2088. if isaddressregister(regdst.reglo) or
  2089. isaddressregister(regdst.reghi) then
  2090. internalerror(2014030105);
  2091. case op of
  2092. OP_ADD,OP_SUB:
  2093. begin
  2094. hreg:=cg.getintregister(list,OS_INT);
  2095. { cg.a_load_const_reg provides optimized loading to register for special cases }
  2096. cg.a_load_const_reg(list,OS_S32,longint(highvalue),hreg);
  2097. { don't use cg.a_op_const_reg() here, because a possible optimized
  2098. ADDQ/SUBQ wouldn't set the eXtend bit }
  2099. list.concat(taicpu.op_const_reg(opcode,S_L,lowvalue,regdst.reglo));
  2100. list.concat(taicpu.op_reg_reg(xopcode,S_L,hreg,regdst.reghi));
  2101. end;
  2102. OP_AND,OP_OR,OP_XOR:
  2103. begin
  2104. cg.a_op_const_reg(list,op,OS_S32,longint(lowvalue),regdst.reglo);
  2105. cg.a_op_const_reg(list,op,OS_S32,longint(highvalue),regdst.reghi);
  2106. end;
  2107. { this is handled in 1st pass for 32-bit cpus (helper call) }
  2108. OP_IDIV,OP_DIV,
  2109. OP_IMUL,OP_MUL:
  2110. internalerror(2002081701);
  2111. { this is also handled in 1st pass for 32-bit cpus (helper call) }
  2112. OP_SAR,OP_SHL,OP_SHR:
  2113. internalerror(2002081702);
  2114. { these should have been handled already by earlier passes }
  2115. OP_NOT,OP_NEG:
  2116. internalerror(2012110403);
  2117. end; { end case }
  2118. end;
  2119. procedure create_codegen;
  2120. begin
  2121. cg := tcg68k.create;
  2122. cg64 :=tcg64f68k.create;
  2123. end;
  2124. end.