cgcpu.pas 83 KB

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