cgcpu.pas 87 KB

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