aasmcpu.pas 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. { Operand types }
  32. OT_NONE = $00000000;
  33. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  34. OT_BITS16 = $00000002;
  35. OT_BITS32 = $00000004;
  36. OT_BITS64 = $00000008; { FPU only }
  37. OT_BITS80 = $00000010;
  38. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  39. OT_NEAR = $00000040;
  40. OT_SHORT = $00000080;
  41. OT_BITSTINY = $00000100; { fpu constant }
  42. OT_BITSSHIFTER =
  43. $00000200;
  44. OT_SIZE_MASK = $000003FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_SHIFTEROP = $00000800;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_REGLIST = $00008000;
  54. OT_IMM8 = $00002001;
  55. OT_IMM24 = $00002002;
  56. OT_IMM32 = $00002004;
  57. OT_IMM64 = $00002008;
  58. OT_IMM80 = $00002010;
  59. OT_IMMTINY = $00002100;
  60. OT_IMMSHIFTER= $00002200;
  61. OT_IMMEDIATE24 = OT_IMM24;
  62. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  63. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  64. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  65. OT_IMMEDIATEFPU = OT_IMMTINY;
  66. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  67. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  68. OT_REG8 = $00201001;
  69. OT_REG16 = $00201002;
  70. OT_REG32 = $00201004;
  71. OT_REG64 = $00201008;
  72. OT_VREG = $00201010; { vector register }
  73. OT_REGF = $00201020; { coproc register }
  74. OT_MEMORY = $00204000; { register number in 'basereg' }
  75. OT_MEM8 = $00204001;
  76. OT_MEM16 = $00204002;
  77. OT_MEM32 = $00204004;
  78. OT_MEM64 = $00204008;
  79. OT_MEM80 = $00204010;
  80. { word/byte load/store }
  81. OT_AM2 = $00010000;
  82. { misc ld/st operations }
  83. OT_AM3 = $00020000;
  84. { multiple ld/st operations }
  85. OT_AM4 = $00040000;
  86. { co proc. ld/st operations }
  87. OT_AM5 = $00080000;
  88. OT_AMMASK = $000f0000;
  89. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  90. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  91. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  92. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  93. OT_FPUREG = $01000000; { floating point stack registers }
  94. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  95. { a mask for the following }
  96. OT_MEM_OFFS = $00604000; { special type of EA }
  97. { simple [address] offset }
  98. OT_ONENESS = $00800000; { special type of immediate operand }
  99. { so UNITY == IMMEDIATE | ONENESS }
  100. OT_UNITY = $00802000; { for shift/rotate instructions }
  101. instabentries = {$i armnop.inc}
  102. maxinfolen = 5;
  103. IF_NONE = $00000000;
  104. IF_ARMMASK = $000F0000;
  105. IF_ARM7 = $00070000;
  106. IF_FPMASK = $00F00000;
  107. IF_FPA = $00100000;
  108. { if the instruction can change in a second pass }
  109. IF_PASS2 = longint($80000000);
  110. type
  111. TInsTabCache=array[TasmOp] of longint;
  112. PInsTabCache=^TInsTabCache;
  113. tinsentry = record
  114. opcode : tasmop;
  115. ops : byte;
  116. optypes : array[0..3] of longint;
  117. code : array[0..maxinfolen] of char;
  118. flags : longint;
  119. end;
  120. pinsentry=^tinsentry;
  121. const
  122. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  123. var
  124. InsTabCache : PInsTabCache;
  125. type
  126. taicpu = class(tai_cpu_abstract_sym)
  127. oppostfix : TOpPostfix;
  128. roundingmode : troundingmode;
  129. procedure loadshifterop(opidx:longint;const so:tshifterop);
  130. procedure loadregset(opidx:longint;const s:tcpuregisterset);
  131. constructor op_none(op : tasmop);
  132. constructor op_reg(op : tasmop;_op1 : tregister);
  133. constructor op_ref(op : tasmop;const _op1 : treference);
  134. constructor op_const(op : tasmop;_op1 : longint);
  135. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  136. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  137. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  138. constructor op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  139. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  140. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  141. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  142. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  143. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  144. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  145. { SFM/LFM }
  146. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  147. { *M*LL }
  148. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  149. { this is for Jmp instructions }
  150. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  151. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  152. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  153. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  154. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  155. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  156. function spilling_get_operation_type(opnr: longint): topertype;override;
  157. { assembler }
  158. public
  159. { the next will reset all instructions that can change in pass 2 }
  160. procedure ResetPass1;override;
  161. procedure ResetPass2;override;
  162. function CheckIfValid:boolean;
  163. function GetString:string;
  164. function Pass1(objdata:TObjData):longint;override;
  165. procedure Pass2(objdata:TObjData);override;
  166. protected
  167. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  168. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  169. procedure ppubuildderefimploper(var o:toper);override;
  170. procedure ppuderefoper(var o:toper);override;
  171. private
  172. { next fields are filled in pass1, so pass2 is faster }
  173. inssize : shortint;
  174. insoffset : longint;
  175. LastInsOffset : longint; { need to be public to be reset }
  176. insentry : PInsEntry;
  177. function InsEnd:longint;
  178. procedure create_ot(objdata:TObjData);
  179. function Matches(p:PInsEntry):longint;
  180. function calcsize(p:PInsEntry):shortint;
  181. procedure gencode(objdata:TObjData);
  182. function NeedAddrPrefix(opidx:byte):boolean;
  183. procedure Swapoperands;
  184. function FindInsentry(objdata:TObjData):boolean;
  185. end;
  186. tai_align = class(tai_align_abstract)
  187. { nothing to add }
  188. end;
  189. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  190. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  191. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  192. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  193. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  194. { inserts pc relative symbols at places where they are reachable }
  195. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  196. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  197. procedure InsertPData;
  198. procedure InitAsm;
  199. procedure DoneAsm;
  200. implementation
  201. uses
  202. cutils,rgobj,itcpugas;
  203. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  204. begin
  205. allocate_oper(opidx+1);
  206. with oper[opidx]^ do
  207. begin
  208. if typ<>top_shifterop then
  209. begin
  210. clearop(opidx);
  211. new(shifterop);
  212. end;
  213. shifterop^:=so;
  214. typ:=top_shifterop;
  215. if assigned(add_reg_instruction_hook) then
  216. add_reg_instruction_hook(self,shifterop^.rs);
  217. end;
  218. end;
  219. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  220. var
  221. i : byte;
  222. begin
  223. allocate_oper(opidx+1);
  224. with oper[opidx]^ do
  225. begin
  226. if typ<>top_regset then
  227. clearop(opidx);
  228. new(regset);
  229. regset^:=s;
  230. typ:=top_regset;
  231. for i:=RS_R0 to RS_R15 do
  232. begin
  233. if assigned(add_reg_instruction_hook) and (i in regset^) then
  234. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  235. end;
  236. end;
  237. end;
  238. {*****************************************************************************
  239. taicpu Constructors
  240. *****************************************************************************}
  241. constructor taicpu.op_none(op : tasmop);
  242. begin
  243. inherited create(op);
  244. end;
  245. { for pld }
  246. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  247. begin
  248. inherited create(op);
  249. ops:=1;
  250. loadref(0,_op1);
  251. end;
  252. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  253. begin
  254. inherited create(op);
  255. ops:=1;
  256. loadreg(0,_op1);
  257. end;
  258. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  259. begin
  260. inherited create(op);
  261. ops:=1;
  262. loadconst(0,aint(_op1));
  263. end;
  264. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  265. begin
  266. inherited create(op);
  267. ops:=2;
  268. loadreg(0,_op1);
  269. loadreg(1,_op2);
  270. end;
  271. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  272. begin
  273. inherited create(op);
  274. ops:=2;
  275. loadreg(0,_op1);
  276. loadconst(1,aint(_op2));
  277. end;
  278. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  279. begin
  280. inherited create(op);
  281. ops:=2;
  282. loadref(0,_op1);
  283. loadregset(1,_op2);
  284. end;
  285. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  286. begin
  287. inherited create(op);
  288. ops:=2;
  289. loadreg(0,_op1);
  290. loadref(1,_op2);
  291. end;
  292. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  293. begin
  294. inherited create(op);
  295. ops:=3;
  296. loadreg(0,_op1);
  297. loadreg(1,_op2);
  298. loadreg(2,_op3);
  299. end;
  300. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  301. begin
  302. inherited create(op);
  303. ops:=4;
  304. loadreg(0,_op1);
  305. loadreg(1,_op2);
  306. loadreg(2,_op3);
  307. loadreg(3,_op4);
  308. end;
  309. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  310. begin
  311. inherited create(op);
  312. ops:=3;
  313. loadreg(0,_op1);
  314. loadreg(1,_op2);
  315. loadconst(2,aint(_op3));
  316. end;
  317. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  318. begin
  319. inherited create(op);
  320. ops:=3;
  321. loadreg(0,_op1);
  322. loadconst(1,_op2);
  323. loadref(2,_op3);
  324. end;
  325. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  326. begin
  327. inherited create(op);
  328. ops:=3;
  329. loadreg(0,_op1);
  330. loadreg(1,_op2);
  331. loadsymbol(0,_op3,_op3ofs);
  332. end;
  333. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  334. begin
  335. inherited create(op);
  336. ops:=3;
  337. loadreg(0,_op1);
  338. loadreg(1,_op2);
  339. loadref(2,_op3);
  340. end;
  341. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  342. begin
  343. inherited create(op);
  344. ops:=3;
  345. loadreg(0,_op1);
  346. loadreg(1,_op2);
  347. loadshifterop(2,_op3);
  348. end;
  349. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  350. begin
  351. inherited create(op);
  352. ops:=4;
  353. loadreg(0,_op1);
  354. loadreg(1,_op2);
  355. loadreg(2,_op3);
  356. loadshifterop(3,_op4);
  357. end;
  358. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  359. begin
  360. inherited create(op);
  361. condition:=cond;
  362. ops:=1;
  363. loadsymbol(0,_op1,0);
  364. end;
  365. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  366. begin
  367. inherited create(op);
  368. ops:=1;
  369. loadsymbol(0,_op1,0);
  370. end;
  371. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  372. begin
  373. inherited create(op);
  374. ops:=1;
  375. loadsymbol(0,_op1,_op1ofs);
  376. end;
  377. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  378. begin
  379. inherited create(op);
  380. ops:=2;
  381. loadreg(0,_op1);
  382. loadsymbol(1,_op2,_op2ofs);
  383. end;
  384. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  385. begin
  386. inherited create(op);
  387. ops:=2;
  388. loadsymbol(0,_op1,_op1ofs);
  389. loadref(1,_op2);
  390. end;
  391. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  392. begin
  393. { allow the register allocator to remove unnecessary moves }
  394. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  395. ((opcode=A_MVF) and (regtype = R_FPUREGISTER) and (oppostfix in [PF_None,PF_D]))
  396. ) and
  397. (condition=C_None) and
  398. (ops=2) and
  399. (oper[0]^.typ=top_reg) and
  400. (oper[1]^.typ=top_reg) and
  401. (oper[0]^.reg=oper[1]^.reg);
  402. end;
  403. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  404. begin
  405. case getregtype(r) of
  406. R_INTREGISTER :
  407. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  408. R_FPUREGISTER :
  409. { use lfm because we don't know the current internal format
  410. and avoid exceptions
  411. }
  412. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  413. else
  414. internalerror(200401041);
  415. end;
  416. end;
  417. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  418. begin
  419. case getregtype(r) of
  420. R_INTREGISTER :
  421. result:=taicpu.op_reg_ref(A_STR,r,ref);
  422. R_FPUREGISTER :
  423. { use sfm because we don't know the current internal format
  424. and avoid exceptions
  425. }
  426. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  427. else
  428. internalerror(200401041);
  429. end;
  430. end;
  431. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  432. begin
  433. case opcode of
  434. A_ADC,A_ADD,A_AND,
  435. A_EOR,A_CLZ,
  436. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  437. A_LDRSH,A_LDRT,
  438. A_MOV,A_MVN,A_MLA,A_MUL,
  439. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  440. A_SWP,A_SWPB,
  441. A_LDF,A_FLT,A_FIX,
  442. A_ADF,A_DVF,A_FDV,A_FML,
  443. A_RFS,A_RFC,A_RDF,
  444. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  445. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  446. A_LFM:
  447. if opnr=0 then
  448. result:=operand_write
  449. else
  450. result:=operand_read;
  451. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  452. A_CMN,A_CMP,A_TEQ,A_TST,
  453. A_CMF,A_CMFE,A_WFS,A_CNF:
  454. result:=operand_read;
  455. A_SMLAL,A_UMLAL:
  456. if opnr in [0,1] then
  457. result:=operand_readwrite
  458. else
  459. result:=operand_read;
  460. A_SMULL,A_UMULL:
  461. if opnr in [0,1] then
  462. result:=operand_write
  463. else
  464. result:=operand_read;
  465. A_STR,A_STRB,A_STRBT,
  466. A_STRH,A_STRT,A_STF,A_SFM:
  467. { important is what happens with the involved registers }
  468. if opnr=0 then
  469. result := operand_read
  470. else
  471. { check for pre/post indexed }
  472. result := operand_read;
  473. else
  474. internalerror(200403151);
  475. end;
  476. end;
  477. procedure BuildInsTabCache;
  478. var
  479. i : longint;
  480. begin
  481. new(instabcache);
  482. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  483. i:=0;
  484. while (i<InsTabEntries) do
  485. begin
  486. if InsTabCache^[InsTab[i].Opcode]=-1 then
  487. InsTabCache^[InsTab[i].Opcode]:=i;
  488. inc(i);
  489. end;
  490. end;
  491. procedure InitAsm;
  492. begin
  493. if not assigned(instabcache) then
  494. BuildInsTabCache;
  495. end;
  496. procedure DoneAsm;
  497. begin
  498. if assigned(instabcache) then
  499. begin
  500. dispose(instabcache);
  501. instabcache:=nil;
  502. end;
  503. end;
  504. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  505. begin
  506. i.oppostfix:=pf;
  507. result:=i;
  508. end;
  509. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  510. begin
  511. i.roundingmode:=rm;
  512. result:=i;
  513. end;
  514. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  515. begin
  516. i.condition:=c;
  517. result:=i;
  518. end;
  519. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  520. var
  521. curpos,
  522. penalty,
  523. lastpos : longint;
  524. curop : longint;
  525. curtai : tai;
  526. curdatatai,hp,hp2 : tai;
  527. curdata : TAsmList;
  528. l : tasmlabel;
  529. doinsert,
  530. removeref : boolean;
  531. begin
  532. curdata:=TAsmList.create;
  533. lastpos:=-1;
  534. curpos:=0;
  535. curtai:=tai(list.first);
  536. doinsert:=false;
  537. while assigned(curtai) do
  538. begin
  539. { instruction? }
  540. if curtai.typ=ait_instruction then
  541. begin
  542. { walk through all operand of the instruction }
  543. for curop:=0 to taicpu(curtai).ops-1 do
  544. begin
  545. { reference? }
  546. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  547. begin
  548. { pc relative symbol? }
  549. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  550. if assigned(curdatatai) and
  551. { move only if we're at the first reference of a label }
  552. (taicpu(curtai).oper[curop]^.ref^.offset=0) then
  553. begin
  554. { check if symbol already used. }
  555. { if yes, reuse the symbol }
  556. hp:=tai(curdatatai.next);
  557. removeref:=false;
  558. if assigned(hp) and (hp.typ=ait_const) then
  559. begin
  560. hp2:=tai(curdata.first);
  561. while assigned(hp2) do
  562. begin
  563. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  564. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  565. then
  566. begin
  567. with taicpu(curtai).oper[curop]^.ref^ do
  568. begin
  569. symboldata:=hp2.previous;
  570. symbol:=tai_label(hp2.previous).labsym;
  571. end;
  572. removeref:=true;
  573. break;
  574. end;
  575. hp2:=tai(hp2.next);
  576. end;
  577. end;
  578. { move or remove symbol reference }
  579. repeat
  580. hp:=tai(curdatatai.next);
  581. listtoinsert.remove(curdatatai);
  582. if removeref then
  583. curdatatai.free
  584. else
  585. curdata.concat(curdatatai);
  586. curdatatai:=hp;
  587. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  588. if lastpos=-1 then
  589. lastpos:=curpos;
  590. end;
  591. end;
  592. end;
  593. inc(curpos);
  594. end
  595. else
  596. if curtai.typ=ait_const then
  597. inc(curpos);
  598. { special case for case jump tables }
  599. if assigned(curtai.next) and
  600. (taicpu(curtai.next).typ=ait_instruction) and
  601. (taicpu(curtai.next).opcode=A_LDR) and
  602. (taicpu(curtai.next).oper[0]^.typ=top_reg) and
  603. (taicpu(curtai.next).oper[0]^.reg=NR_PC) then
  604. begin
  605. penalty:=1;
  606. hp:=tai(curtai.next.next);
  607. while assigned(hp) and (hp.typ=ait_const) do
  608. begin
  609. inc(penalty);
  610. hp:=tai(hp.next);
  611. end;
  612. end
  613. else
  614. penalty:=0;
  615. { don't miss an insert }
  616. doinsert:=doinsert or (curpos-lastpos+penalty>1016);
  617. { split only at real instructions else the test below fails }
  618. if doinsert and (curtai.typ=ait_instruction) and
  619. (
  620. { don't split loads of pc to lr and the following move }
  621. not(
  622. (taicpu(curtai).opcode=A_MOV) and
  623. (taicpu(curtai).oper[0]^.typ=top_reg) and
  624. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  625. (taicpu(curtai).oper[1]^.typ=top_reg) and
  626. (taicpu(curtai).oper[1]^.reg=NR_PC)
  627. )
  628. ) then
  629. begin
  630. lastpos:=curpos;
  631. doinsert:=false;
  632. hp:=tai(curtai.next);
  633. current_asmdata.getjumplabel(l);
  634. curdata.insert(taicpu.op_sym(A_B,l));
  635. curdata.concat(tai_label.create(l));
  636. list.insertlistafter(curtai,curdata);
  637. curtai:=hp;
  638. end
  639. else
  640. curtai:=tai(curtai.next);
  641. end;
  642. list.concatlist(curdata);
  643. curdata.free;
  644. end;
  645. procedure InsertPData;
  646. var
  647. prolog: TAsmList;
  648. begin
  649. prolog:=TAsmList.create;
  650. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  651. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  652. prolog.concat(Tai_const.Create_32bit(0));
  653. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  654. { dummy function }
  655. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  656. current_asmdata.asmlists[al_start].insertList(prolog);
  657. prolog.Free;
  658. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  659. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  660. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  661. end;
  662. (*
  663. Floating point instruction format information, taken from the linux kernel
  664. ARM Floating Point Instruction Classes
  665. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  666. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  667. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  668. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  669. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  670. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  671. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  672. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  673. CPDT data transfer instructions
  674. LDF, STF, LFM (copro 2), SFM (copro 2)
  675. CPDO dyadic arithmetic instructions
  676. ADF, MUF, SUF, RSF, DVF, RDF,
  677. POW, RPW, RMF, FML, FDV, FRD, POL
  678. CPDO monadic arithmetic instructions
  679. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  680. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  681. CPRT joint arithmetic/data transfer instructions
  682. FIX (arithmetic followed by load/store)
  683. FLT (load/store followed by arithmetic)
  684. CMF, CNF CMFE, CNFE (comparisons)
  685. WFS, RFS (write/read floating point status register)
  686. WFC, RFC (write/read floating point control register)
  687. cond condition codes
  688. P pre/post index bit: 0 = postindex, 1 = preindex
  689. U up/down bit: 0 = stack grows down, 1 = stack grows up
  690. W write back bit: 1 = update base register (Rn)
  691. L load/store bit: 0 = store, 1 = load
  692. Rn base register
  693. Rd destination/source register
  694. Fd floating point destination register
  695. Fn floating point source register
  696. Fm floating point source register or floating point constant
  697. uv transfer length (TABLE 1)
  698. wx register count (TABLE 2)
  699. abcd arithmetic opcode (TABLES 3 & 4)
  700. ef destination size (rounding precision) (TABLE 5)
  701. gh rounding mode (TABLE 6)
  702. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  703. i constant bit: 1 = constant (TABLE 6)
  704. */
  705. /*
  706. TABLE 1
  707. +-------------------------+---+---+---------+---------+
  708. | Precision | u | v | FPSR.EP | length |
  709. +-------------------------+---+---+---------+---------+
  710. | Single | 0 | 0 | x | 1 words |
  711. | Double | 1 | 1 | x | 2 words |
  712. | Extended | 1 | 1 | x | 3 words |
  713. | Packed decimal | 1 | 1 | 0 | 3 words |
  714. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  715. +-------------------------+---+---+---------+---------+
  716. Note: x = don't care
  717. */
  718. /*
  719. TABLE 2
  720. +---+---+---------------------------------+
  721. | w | x | Number of registers to transfer |
  722. +---+---+---------------------------------+
  723. | 0 | 1 | 1 |
  724. | 1 | 0 | 2 |
  725. | 1 | 1 | 3 |
  726. | 0 | 0 | 4 |
  727. +---+---+---------------------------------+
  728. */
  729. /*
  730. TABLE 3: Dyadic Floating Point Opcodes
  731. +---+---+---+---+----------+-----------------------+-----------------------+
  732. | a | b | c | d | Mnemonic | Description | Operation |
  733. +---+---+---+---+----------+-----------------------+-----------------------+
  734. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  735. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  736. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  737. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  738. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  739. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  740. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  741. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  742. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  743. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  744. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  745. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  746. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  747. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  748. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  749. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  750. +---+---+---+---+----------+-----------------------+-----------------------+
  751. Note: POW, RPW, POL are deprecated, and are available for backwards
  752. compatibility only.
  753. */
  754. /*
  755. TABLE 4: Monadic Floating Point Opcodes
  756. +---+---+---+---+----------+-----------------------+-----------------------+
  757. | a | b | c | d | Mnemonic | Description | Operation |
  758. +---+---+---+---+----------+-----------------------+-----------------------+
  759. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  760. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  761. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  762. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  763. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  764. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  765. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  766. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  767. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  768. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  769. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  770. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  771. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  772. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  773. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  774. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  775. +---+---+---+---+----------+-----------------------+-----------------------+
  776. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  777. available for backwards compatibility only.
  778. */
  779. /*
  780. TABLE 5
  781. +-------------------------+---+---+
  782. | Rounding Precision | e | f |
  783. +-------------------------+---+---+
  784. | IEEE Single precision | 0 | 0 |
  785. | IEEE Double precision | 0 | 1 |
  786. | IEEE Extended precision | 1 | 0 |
  787. | undefined (trap) | 1 | 1 |
  788. +-------------------------+---+---+
  789. */
  790. /*
  791. TABLE 5
  792. +---------------------------------+---+---+
  793. | Rounding Mode | g | h |
  794. +---------------------------------+---+---+
  795. | Round to nearest (default) | 0 | 0 |
  796. | Round toward plus infinity | 0 | 1 |
  797. | Round toward negative infinity | 1 | 0 |
  798. | Round toward zero | 1 | 1 |
  799. +---------------------------------+---+---+
  800. *)
  801. function taicpu.GetString:string;
  802. var
  803. i : longint;
  804. s : string;
  805. addsize : boolean;
  806. begin
  807. s:='['+gas_op2str[opcode];
  808. for i:=0 to ops-1 do
  809. begin
  810. with oper[i]^ do
  811. begin
  812. if i=0 then
  813. s:=s+' '
  814. else
  815. s:=s+',';
  816. { type }
  817. addsize:=false;
  818. if (ot and OT_VREG)=OT_VREG then
  819. s:=s+'vreg'
  820. else
  821. if (ot and OT_FPUREG)=OT_FPUREG then
  822. s:=s+'fpureg'
  823. else
  824. if (ot and OT_REGISTER)=OT_REGISTER then
  825. begin
  826. s:=s+'reg';
  827. addsize:=true;
  828. end
  829. else
  830. if (ot and OT_REGLIST)=OT_REGLIST then
  831. begin
  832. s:=s+'reglist';
  833. addsize:=false;
  834. end
  835. else
  836. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  837. begin
  838. s:=s+'imm';
  839. addsize:=true;
  840. end
  841. else
  842. if (ot and OT_MEMORY)=OT_MEMORY then
  843. begin
  844. s:=s+'mem';
  845. addsize:=true;
  846. if (ot and OT_AM2)<>0 then
  847. s:=s+' am2 ';
  848. end
  849. else
  850. s:=s+'???';
  851. { size }
  852. if addsize then
  853. begin
  854. if (ot and OT_BITS8)<>0 then
  855. s:=s+'8'
  856. else
  857. if (ot and OT_BITS16)<>0 then
  858. s:=s+'24'
  859. else
  860. if (ot and OT_BITS32)<>0 then
  861. s:=s+'32'
  862. else
  863. if (ot and OT_BITSSHIFTER)<>0 then
  864. s:=s+'shifter'
  865. else
  866. s:=s+'??';
  867. { signed }
  868. if (ot and OT_SIGNED)<>0 then
  869. s:=s+'s';
  870. end;
  871. end;
  872. end;
  873. GetString:=s+']';
  874. end;
  875. procedure taicpu.ResetPass1;
  876. begin
  877. { we need to reset everything here, because the choosen insentry
  878. can be invalid for a new situation where the previously optimized
  879. insentry is not correct }
  880. InsEntry:=nil;
  881. InsSize:=0;
  882. LastInsOffset:=-1;
  883. end;
  884. procedure taicpu.ResetPass2;
  885. begin
  886. { we are here in a second pass, check if the instruction can be optimized }
  887. if assigned(InsEntry) and
  888. ((InsEntry^.flags and IF_PASS2)<>0) then
  889. begin
  890. InsEntry:=nil;
  891. InsSize:=0;
  892. end;
  893. LastInsOffset:=-1;
  894. end;
  895. function taicpu.CheckIfValid:boolean;
  896. begin
  897. Result:=False; { unimplemented }
  898. end;
  899. function taicpu.Pass1(objdata:TObjData):longint;
  900. var
  901. ldr2op : array[PF_B..PF_T] of tasmop = (
  902. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  903. str2op : array[PF_B..PF_T] of tasmop = (
  904. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  905. begin
  906. Pass1:=0;
  907. { Save the old offset and set the new offset }
  908. InsOffset:=ObjData.CurrObjSec.Size;
  909. { Error? }
  910. if (Insentry=nil) and (InsSize=-1) then
  911. exit;
  912. { set the file postion }
  913. current_filepos:=fileinfo;
  914. { tranlate LDR+postfix to complete opcode }
  915. if (opcode=A_LDR) and (oppostfix<>PF_None) then
  916. begin
  917. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  918. opcode:=ldr2op[oppostfix]
  919. else
  920. internalerror(2005091001);
  921. if opcode=A_None then
  922. internalerror(2005091004);
  923. { postfix has been added to opcode }
  924. oppostfix:=PF_None;
  925. end
  926. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  927. begin
  928. if (oppostfix in [low(str2op)..high(str2op)]) then
  929. opcode:=str2op[oppostfix]
  930. else
  931. internalerror(2005091002);
  932. if opcode=A_None then
  933. internalerror(2005091003);
  934. { postfix has been added to opcode }
  935. oppostfix:=PF_None;
  936. end;
  937. { Get InsEntry }
  938. if FindInsEntry(objdata) then
  939. begin
  940. InsSize:=4;
  941. LastInsOffset:=InsOffset;
  942. Pass1:=InsSize;
  943. exit;
  944. end;
  945. LastInsOffset:=-1;
  946. end;
  947. procedure taicpu.Pass2(objdata:TObjData);
  948. begin
  949. { error in pass1 ? }
  950. if insentry=nil then
  951. exit;
  952. current_filepos:=fileinfo;
  953. { Generate the instruction }
  954. GenCode(objdata);
  955. end;
  956. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  957. begin
  958. end;
  959. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  960. begin
  961. end;
  962. procedure taicpu.ppubuildderefimploper(var o:toper);
  963. begin
  964. end;
  965. procedure taicpu.ppuderefoper(var o:toper);
  966. begin
  967. end;
  968. function taicpu.InsEnd:longint;
  969. begin
  970. Result:=0; { unimplemented }
  971. end;
  972. procedure taicpu.create_ot(objdata:TObjData);
  973. var
  974. i,l,relsize : longint;
  975. dummy : byte;
  976. currsym : TObjSymbol;
  977. begin
  978. if ops=0 then
  979. exit;
  980. { update oper[].ot field }
  981. for i:=0 to ops-1 do
  982. with oper[i]^ do
  983. begin
  984. case typ of
  985. top_regset:
  986. begin
  987. ot:=OT_REGLIST;
  988. end;
  989. top_reg :
  990. begin
  991. case getregtype(reg) of
  992. R_INTREGISTER:
  993. ot:=OT_REG32 or OT_SHIFTEROP;
  994. R_FPUREGISTER:
  995. ot:=OT_FPUREG;
  996. else
  997. internalerror(2005090901);
  998. end;
  999. end;
  1000. top_ref :
  1001. begin
  1002. if ref^.refaddr=addr_no then
  1003. begin
  1004. { create ot field }
  1005. { we should get the size here dependend on the
  1006. instruction }
  1007. if (ot and OT_SIZE_MASK)=0 then
  1008. ot:=OT_MEMORY or OT_BITS32
  1009. else
  1010. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1011. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1012. ot:=ot or OT_MEM_OFFS;
  1013. { if we need to fix a reference, we do it here }
  1014. { pc relative addressing }
  1015. if (ref^.base=NR_NO) and
  1016. (ref^.index=NR_NO) and
  1017. (ref^.shiftmode=SM_None)
  1018. { at least we should check if the destination symbol
  1019. is in a text section }
  1020. { and
  1021. (ref^.symbol^.owner="text") } then
  1022. ref^.base:=NR_PC;
  1023. { determine possible address modes }
  1024. if (ref^.base<>NR_NO) and
  1025. (
  1026. (
  1027. (ref^.index=NR_NO) and
  1028. (ref^.shiftmode=SM_None) and
  1029. (ref^.offset>=-4097) and
  1030. (ref^.offset<=4097)
  1031. ) or
  1032. (
  1033. (ref^.shiftmode=SM_None) and
  1034. (ref^.offset=0)
  1035. ) or
  1036. (
  1037. (ref^.index<>NR_NO) and
  1038. (ref^.shiftmode<>SM_None) and
  1039. (ref^.shiftimm<=31) and
  1040. (ref^.offset=0)
  1041. )
  1042. ) then
  1043. ot:=ot or OT_AM2;
  1044. if (ref^.index<>NR_NO) and
  1045. (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1046. (
  1047. (ref^.base=NR_NO) and
  1048. (ref^.shiftmode=SM_None) and
  1049. (ref^.offset=0)
  1050. ) then
  1051. ot:=ot or OT_AM4;
  1052. end
  1053. else
  1054. begin
  1055. l:=ref^.offset;
  1056. currsym:=ObjData.symbolref(ref^.symbol);
  1057. if assigned(currsym) then
  1058. inc(l,currsym.address);
  1059. relsize:=(InsOffset+2)-l;
  1060. if (relsize<-33554428) or (relsize>33554428) then
  1061. ot:=OT_IMM32
  1062. else
  1063. ot:=OT_IMM24;
  1064. end;
  1065. end;
  1066. top_local :
  1067. begin
  1068. { we should get the size here dependend on the
  1069. instruction }
  1070. if (ot and OT_SIZE_MASK)=0 then
  1071. ot:=OT_MEMORY or OT_BITS32
  1072. else
  1073. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1074. end;
  1075. top_const :
  1076. begin
  1077. ot:=OT_IMMEDIATE;
  1078. if is_shifter_const(val,dummy) then
  1079. ot:=OT_IMMSHIFTER
  1080. else
  1081. ot:=OT_IMM32
  1082. end;
  1083. top_none :
  1084. begin
  1085. { generated when there was an error in the
  1086. assembler reader. It never happends when generating
  1087. assembler }
  1088. end;
  1089. top_shifterop:
  1090. begin
  1091. ot:=OT_SHIFTEROP;
  1092. end;
  1093. else
  1094. internalerror(200402261);
  1095. end;
  1096. end;
  1097. end;
  1098. function taicpu.Matches(p:PInsEntry):longint;
  1099. { * IF_SM stands for Size Match: any operand whose size is not
  1100. * explicitly specified by the template is `really' intended to be
  1101. * the same size as the first size-specified operand.
  1102. * Non-specification is tolerated in the input instruction, but
  1103. * _wrong_ specification is not.
  1104. *
  1105. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1106. * three-operand instructions such as SHLD: it implies that the
  1107. * first two operands must match in size, but that the third is
  1108. * required to be _unspecified_.
  1109. *
  1110. * IF_SB invokes Size Byte: operands with unspecified size in the
  1111. * template are really bytes, and so no non-byte specification in
  1112. * the input instruction will be tolerated. IF_SW similarly invokes
  1113. * Size Word, and IF_SD invokes Size Doubleword.
  1114. *
  1115. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1116. * that any operand with unspecified size in the template is
  1117. * required to have unspecified size in the instruction too...)
  1118. }
  1119. var
  1120. i{,j,asize,oprs} : longint;
  1121. {siz : array[0..3] of longint;}
  1122. begin
  1123. Matches:=100;
  1124. writeln(getstring,'---');
  1125. { Check the opcode and operands }
  1126. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1127. begin
  1128. Matches:=0;
  1129. exit;
  1130. end;
  1131. { Check that no spurious colons or TOs are present }
  1132. for i:=0 to p^.ops-1 do
  1133. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1134. begin
  1135. Matches:=0;
  1136. exit;
  1137. end;
  1138. { Check that the operand flags all match up }
  1139. for i:=0 to p^.ops-1 do
  1140. begin
  1141. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1142. ((p^.optypes[i] and OT_SIZE_MASK) and
  1143. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1144. begin
  1145. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1146. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1147. begin
  1148. Matches:=0;
  1149. exit;
  1150. end
  1151. else
  1152. Matches:=1;
  1153. end;
  1154. end;
  1155. { check postfixes:
  1156. the existance of a certain postfix requires a
  1157. particular code }
  1158. { update condition flags
  1159. or floating point single }
  1160. if (oppostfix=PF_S) and
  1161. not(p^.code[0] in [#$04]) then
  1162. begin
  1163. Matches:=0;
  1164. exit;
  1165. end;
  1166. { floating point size }
  1167. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1168. not(p^.code[0] in []) then
  1169. begin
  1170. Matches:=0;
  1171. exit;
  1172. end;
  1173. { multiple load/store address modes }
  1174. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1175. not(p^.code[0] in [
  1176. // ldr,str,ldrb,strb
  1177. #$17,
  1178. // stm,ldm
  1179. #$26
  1180. ]) then
  1181. begin
  1182. Matches:=0;
  1183. exit;
  1184. end;
  1185. { we shouldn't see any opsize prefixes here }
  1186. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1187. begin
  1188. Matches:=0;
  1189. exit;
  1190. end;
  1191. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1192. begin
  1193. Matches:=0;
  1194. exit;
  1195. end;
  1196. { Check operand sizes }
  1197. { as default an untyped size can get all the sizes, this is different
  1198. from nasm, but else we need to do a lot checking which opcodes want
  1199. size or not with the automatic size generation }
  1200. (*
  1201. asize:=longint($ffffffff);
  1202. if (p^.flags and IF_SB)<>0 then
  1203. asize:=OT_BITS8
  1204. else if (p^.flags and IF_SW)<>0 then
  1205. asize:=OT_BITS16
  1206. else if (p^.flags and IF_SD)<>0 then
  1207. asize:=OT_BITS32;
  1208. if (p^.flags and IF_ARMASK)<>0 then
  1209. begin
  1210. siz[0]:=0;
  1211. siz[1]:=0;
  1212. siz[2]:=0;
  1213. if (p^.flags and IF_AR0)<>0 then
  1214. siz[0]:=asize
  1215. else if (p^.flags and IF_AR1)<>0 then
  1216. siz[1]:=asize
  1217. else if (p^.flags and IF_AR2)<>0 then
  1218. siz[2]:=asize;
  1219. end
  1220. else
  1221. begin
  1222. { we can leave because the size for all operands is forced to be
  1223. the same
  1224. but not if IF_SB IF_SW or IF_SD is set PM }
  1225. if asize=-1 then
  1226. exit;
  1227. siz[0]:=asize;
  1228. siz[1]:=asize;
  1229. siz[2]:=asize;
  1230. end;
  1231. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1232. begin
  1233. if (p^.flags and IF_SM2)<>0 then
  1234. oprs:=2
  1235. else
  1236. oprs:=p^.ops;
  1237. for i:=0 to oprs-1 do
  1238. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1239. begin
  1240. for j:=0 to oprs-1 do
  1241. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1242. break;
  1243. end;
  1244. end
  1245. else
  1246. oprs:=2;
  1247. { Check operand sizes }
  1248. for i:=0 to p^.ops-1 do
  1249. begin
  1250. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1251. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1252. { Immediates can always include smaller size }
  1253. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1254. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1255. Matches:=2;
  1256. end;
  1257. *)
  1258. end;
  1259. function taicpu.calcsize(p:PInsEntry):shortint;
  1260. begin
  1261. result:=4;
  1262. end;
  1263. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1264. begin
  1265. Result:=False; { unimplemented }
  1266. end;
  1267. procedure taicpu.Swapoperands;
  1268. begin
  1269. end;
  1270. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1271. var
  1272. i : longint;
  1273. begin
  1274. result:=false;
  1275. { Things which may only be done once, not when a second pass is done to
  1276. optimize }
  1277. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1278. begin
  1279. { create the .ot fields }
  1280. create_ot(objdata);
  1281. { set the file postion }
  1282. current_filepos:=fileinfo;
  1283. end
  1284. else
  1285. begin
  1286. { we've already an insentry so it's valid }
  1287. result:=true;
  1288. exit;
  1289. end;
  1290. { Lookup opcode in the table }
  1291. InsSize:=-1;
  1292. i:=instabcache^[opcode];
  1293. if i=-1 then
  1294. begin
  1295. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1296. exit;
  1297. end;
  1298. insentry:=@instab[i];
  1299. while (insentry^.opcode=opcode) do
  1300. begin
  1301. if matches(insentry)=100 then
  1302. begin
  1303. result:=true;
  1304. exit;
  1305. end;
  1306. inc(i);
  1307. insentry:=@instab[i];
  1308. end;
  1309. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1310. { No instruction found, set insentry to nil and inssize to -1 }
  1311. insentry:=nil;
  1312. inssize:=-1;
  1313. end;
  1314. procedure taicpu.gencode(objdata:TObjData);
  1315. var
  1316. bytes : dword;
  1317. i_field : byte;
  1318. procedure setshifterop(op : byte);
  1319. begin
  1320. case oper[op]^.typ of
  1321. top_const:
  1322. begin
  1323. i_field:=1;
  1324. bytes:=bytes or dword(oper[op]^.val and $fff);
  1325. end;
  1326. top_reg:
  1327. begin
  1328. i_field:=0;
  1329. bytes:=bytes or (getsupreg(oper[op]^.reg) shl 16);
  1330. { does a real shifter op follow? }
  1331. if (op+1<=op) and (oper[op+1]^.typ=top_shifterop) then
  1332. begin
  1333. end;
  1334. end;
  1335. else
  1336. internalerror(2005091103);
  1337. end;
  1338. end;
  1339. begin
  1340. bytes:=$0;
  1341. { evaluate and set condition code }
  1342. { condition code allowed? }
  1343. { setup rest of the instruction }
  1344. case insentry^.code[0] of
  1345. #$08:
  1346. begin
  1347. { set instruction code }
  1348. bytes:=bytes or (ord(insentry^.code[1]) shl 26);
  1349. bytes:=bytes or (ord(insentry^.code[2]) shl 21);
  1350. { set destination }
  1351. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  1352. { create shifter op }
  1353. setshifterop(1);
  1354. { set i field }
  1355. bytes:=bytes or (i_field shl 25);
  1356. { set s if necessary }
  1357. if oppostfix=PF_S then
  1358. bytes:=bytes or (1 shl 20);
  1359. end;
  1360. #$ff:
  1361. internalerror(2005091101);
  1362. else
  1363. internalerror(2005091102);
  1364. end;
  1365. { we're finished, write code }
  1366. objdata.writebytes(bytes,sizeof(bytes));
  1367. end;
  1368. {$ifdef dummy}
  1369. (*
  1370. static void gencode (long segment, long offset, int bits,
  1371. insn *ins, char *codes, long insn_end)
  1372. {
  1373. int has_S_code; /* S - setflag */
  1374. int has_B_code; /* B - setflag */
  1375. int has_T_code; /* T - setflag */
  1376. int has_W_code; /* ! => W flag */
  1377. int has_F_code; /* ^ => S flag */
  1378. int keep;
  1379. unsigned char c;
  1380. unsigned char bytes[4];
  1381. long data, size;
  1382. static int cc_code[] = /* bit pattern of cc */
  1383. { /* order as enum in */
  1384. 0x0E, 0x03, 0x02, 0x00, /* nasm.h */
  1385. 0x0A, 0x0C, 0x08, 0x0D,
  1386. 0x09, 0x0B, 0x04, 0x01,
  1387. 0x05, 0x07, 0x06,
  1388. };
  1389. #ifdef DEBUG
  1390. static char *CC[] =
  1391. { /* condition code names */
  1392. "AL", "CC", "CS", "EQ",
  1393. "GE", "GT", "HI", "LE",
  1394. "LS", "LT", "MI", "NE",
  1395. "PL", "VC", "VS", "",
  1396. "S"
  1397. };
  1398. has_S_code = (ins->condition & C_SSETFLAG);
  1399. has_B_code = (ins->condition & C_BSETFLAG);
  1400. has_T_code = (ins->condition & C_TSETFLAG);
  1401. has_W_code = (ins->condition & C_EXSETFLAG);
  1402. has_F_code = (ins->condition & C_FSETFLAG);
  1403. ins->condition = (ins->condition & 0x0F);
  1404. if (rt_debug)
  1405. {
  1406. printf ("gencode: instruction: %s%s", insn_names[ins->opcode],
  1407. CC[ins->condition & 0x0F]);
  1408. if (has_S_code)
  1409. printf ("S");
  1410. if (has_B_code)
  1411. printf ("B");
  1412. if (has_T_code)
  1413. printf ("T");
  1414. if (has_W_code)
  1415. printf ("!");
  1416. if (has_F_code)
  1417. printf ("^");
  1418. printf ("\n");
  1419. c = *codes;
  1420. printf (" (%d) decode - '0x%02X'\n", ins->operands, c);
  1421. bytes[0] = 0xB;
  1422. bytes[1] = 0xE;
  1423. bytes[2] = 0xE;
  1424. bytes[3] = 0xF;
  1425. }
  1426. // First condition code in upper nibble
  1427. if (ins->condition < C_NONE)
  1428. {
  1429. c = cc_code[ins->condition] << 4;
  1430. }
  1431. else
  1432. {
  1433. c = cc_code[C_AL] << 4; // is often ALWAYS but not always
  1434. }
  1435. switch (keep = *codes)
  1436. {
  1437. case 1:
  1438. // B, BL
  1439. ++codes;
  1440. c |= *codes++;
  1441. bytes[0] = c;
  1442. if (ins->oprs[0].segment != segment)
  1443. {
  1444. // fais une relocation
  1445. c = 1;
  1446. data = 0; // Let the linker locate ??
  1447. }
  1448. else
  1449. {
  1450. c = 0;
  1451. data = ins->oprs[0].offset - (offset + 8);
  1452. if (data % 4)
  1453. {
  1454. errfunc (ERR_NONFATAL, "offset not aligned on 4 bytes");
  1455. }
  1456. }
  1457. if (data >= 0x1000)
  1458. {
  1459. errfunc (ERR_NONFATAL, "too long offset");
  1460. }
  1461. data = data >> 2;
  1462. bytes[1] = (data >> 16) & 0xFF;
  1463. bytes[2] = (data >> 8) & 0xFF;
  1464. bytes[3] = (data ) & 0xFF;
  1465. if (c == 1)
  1466. {
  1467. // out (offset, segment, &bytes[0], OUT_RAWDATA+1, NO_SEG, NO_SEG);
  1468. out (offset, segment, &bytes[0], OUT_REL3ADR+4, ins->oprs[0].segment, NO_SEG);
  1469. }
  1470. else
  1471. {
  1472. out (offset, segment, &bytes[0], OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1473. }
  1474. return;
  1475. case 2:
  1476. // SWI
  1477. ++codes;
  1478. c |= *codes++;
  1479. bytes[0] = c;
  1480. data = ins->oprs[0].offset;
  1481. bytes[1] = (data >> 16) & 0xFF;
  1482. bytes[2] = (data >> 8) & 0xFF;
  1483. bytes[3] = (data) & 0xFF;
  1484. out (offset, segment, &bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1485. return;
  1486. case 3:
  1487. // BX
  1488. ++codes;
  1489. c |= *codes++;
  1490. bytes[0] = c;
  1491. bytes[1] = *codes++;
  1492. bytes[2] = *codes++;
  1493. bytes[3] = *codes++;
  1494. c = regval (&ins->oprs[0],1);
  1495. if (c == 15) // PC
  1496. {
  1497. errfunc (ERR_WARNING, "'BX' with R15 has undefined behaviour");
  1498. }
  1499. else if (c > 15)
  1500. {
  1501. errfunc (ERR_NONFATAL, "Illegal register specified for 'BX'");
  1502. }
  1503. bytes[3] |= (c & 0x0F);
  1504. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1505. return;
  1506. case 4: // AND Rd,Rn,Rm
  1507. case 5: // AND Rd,Rn,Rm,<shift>Rs
  1508. case 6: // AND Rd,Rn,Rm,<shift>imm
  1509. case 7: // AND Rd,Rn,<shift>imm
  1510. ++codes;
  1511. #ifdef DEBUG
  1512. if (rt_debug)
  1513. {
  1514. printf (" decode - '0x%02X'\n", keep);
  1515. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1516. }
  1517. #endif
  1518. bytes[0] = c | *codes;
  1519. ++codes;
  1520. bytes[1] = *codes;
  1521. if (has_S_code)
  1522. bytes[1] |= 0x10;
  1523. c = regval (&ins->oprs[1],1);
  1524. // Rn in low nibble
  1525. bytes[1] |= c;
  1526. // Rd in high nibble
  1527. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1528. if (keep != 7)
  1529. {
  1530. // Rm in low nibble
  1531. bytes[3] = regval (&ins->oprs[2],1);
  1532. }
  1533. // Shifts if any
  1534. if (keep == 5 || keep == 6)
  1535. {
  1536. // Shift in bytes 2 and 3
  1537. if (keep == 5)
  1538. {
  1539. // Rs
  1540. c = regval (&ins->oprs[3],1);
  1541. bytes[2] |= c;
  1542. c = 0x10; // Set bit 4 in byte[3]
  1543. }
  1544. if (keep == 6)
  1545. {
  1546. c = (ins->oprs[3].offset) & 0x1F;
  1547. // #imm
  1548. bytes[2] |= c >> 1;
  1549. if (c & 0x01)
  1550. {
  1551. bytes[3] |= 0x80;
  1552. }
  1553. c = 0; // Clr bit 4 in byte[3]
  1554. }
  1555. // <shift>
  1556. c |= shiftval (&ins->oprs[3]) << 5;
  1557. bytes[3] |= c;
  1558. }
  1559. // reg,reg,imm
  1560. if (keep == 7)
  1561. {
  1562. int shimm;
  1563. shimm = imm_shift (ins->oprs[2].offset);
  1564. if (shimm == -1)
  1565. {
  1566. errfunc (ERR_NONFATAL, "cannot create that constant");
  1567. }
  1568. bytes[3] = shimm & 0xFF;
  1569. bytes[2] |= (shimm & 0xF00) >> 8;
  1570. }
  1571. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1572. return;
  1573. case 8: // MOV Rd,Rm
  1574. case 9: // MOV Rd,Rm,<shift>Rs
  1575. case 0xA: // MOV Rd,Rm,<shift>imm
  1576. case 0xB: // MOV Rd,<shift>imm
  1577. ++codes;
  1578. #ifdef DEBUG
  1579. if (rt_debug)
  1580. {
  1581. printf (" decode - '0x%02X'\n", keep);
  1582. printf (" code - '0x%02X'\n", (unsigned char) ( *codes));
  1583. }
  1584. #endif
  1585. bytes[0] = c | *codes;
  1586. ++codes;
  1587. bytes[1] = *codes;
  1588. if (has_S_code)
  1589. bytes[1] |= 0x10;
  1590. // Rd in high nibble
  1591. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1592. if (keep != 0x0B)
  1593. {
  1594. // Rm in low nibble
  1595. bytes[3] = regval (&ins->oprs[1],1);
  1596. }
  1597. // Shifts if any
  1598. if (keep == 0x09 || keep == 0x0A)
  1599. {
  1600. // Shift in bytes 2 and 3
  1601. if (keep == 0x09)
  1602. {
  1603. // Rs
  1604. c = regval (&ins->oprs[2],1);
  1605. bytes[2] |= c;
  1606. c = 0x10; // Set bit 4 in byte[3]
  1607. }
  1608. if (keep == 0x0A)
  1609. {
  1610. c = (ins->oprs[2].offset) & 0x1F;
  1611. // #imm
  1612. bytes[2] |= c >> 1;
  1613. if (c & 0x01)
  1614. {
  1615. bytes[3] |= 0x80;
  1616. }
  1617. c = 0; // Clr bit 4 in byte[3]
  1618. }
  1619. // <shift>
  1620. c |= shiftval (&ins->oprs[2]) << 5;
  1621. bytes[3] |= c;
  1622. }
  1623. // reg,imm
  1624. if (keep == 0x0B)
  1625. {
  1626. int shimm;
  1627. shimm = imm_shift (ins->oprs[1].offset);
  1628. if (shimm == -1)
  1629. {
  1630. errfunc (ERR_NONFATAL, "cannot create that constant");
  1631. }
  1632. bytes[3] = shimm & 0xFF;
  1633. bytes[2] |= (shimm & 0xF00) >> 8;
  1634. }
  1635. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1636. return;
  1637. case 0xC: // CMP Rn,Rm
  1638. case 0xD: // CMP Rn,Rm,<shift>Rs
  1639. case 0xE: // CMP Rn,Rm,<shift>imm
  1640. case 0xF: // CMP Rn,<shift>imm
  1641. ++codes;
  1642. bytes[0] = c | *codes++;
  1643. bytes[1] = *codes;
  1644. // Implicit S code
  1645. bytes[1] |= 0x10;
  1646. c = regval (&ins->oprs[0],1);
  1647. // Rn in low nibble
  1648. bytes[1] |= c;
  1649. // No destination
  1650. bytes[2] = 0;
  1651. if (keep != 0x0B)
  1652. {
  1653. // Rm in low nibble
  1654. bytes[3] = regval (&ins->oprs[1],1);
  1655. }
  1656. // Shifts if any
  1657. if (keep == 0x0D || keep == 0x0E)
  1658. {
  1659. // Shift in bytes 2 and 3
  1660. if (keep == 0x0D)
  1661. {
  1662. // Rs
  1663. c = regval (&ins->oprs[2],1);
  1664. bytes[2] |= c;
  1665. c = 0x10; // Set bit 4 in byte[3]
  1666. }
  1667. if (keep == 0x0E)
  1668. {
  1669. c = (ins->oprs[2].offset) & 0x1F;
  1670. // #imm
  1671. bytes[2] |= c >> 1;
  1672. if (c & 0x01)
  1673. {
  1674. bytes[3] |= 0x80;
  1675. }
  1676. c = 0; // Clr bit 4 in byte[3]
  1677. }
  1678. // <shift>
  1679. c |= shiftval (&ins->oprs[2]) << 5;
  1680. bytes[3] |= c;
  1681. }
  1682. // reg,imm
  1683. if (keep == 0x0F)
  1684. {
  1685. int shimm;
  1686. shimm = imm_shift (ins->oprs[1].offset);
  1687. if (shimm == -1)
  1688. {
  1689. errfunc (ERR_NONFATAL, "cannot create that constant");
  1690. }
  1691. bytes[3] = shimm & 0xFF;
  1692. bytes[2] |= (shimm & 0xF00) >> 8;
  1693. }
  1694. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1695. return;
  1696. case 0x10: // MRS Rd,<psr>
  1697. ++codes;
  1698. bytes[0] = c | *codes++;
  1699. bytes[1] = *codes++;
  1700. // Rd
  1701. c = regval (&ins->oprs[0],1);
  1702. bytes[2] = c << 4;
  1703. bytes[3] = 0;
  1704. c = ins->oprs[1].basereg;
  1705. if (c == R_CPSR || c == R_SPSR)
  1706. {
  1707. if (c == R_SPSR)
  1708. {
  1709. bytes[1] |= 0x40;
  1710. }
  1711. }
  1712. else
  1713. {
  1714. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1715. }
  1716. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  1717. return;
  1718. case 0x11: // MSR <psr>,Rm
  1719. case 0x12: // MSR <psrf>,Rm
  1720. case 0x13: // MSR <psrf>,#expression
  1721. ++codes;
  1722. bytes[0] = c | *codes++;
  1723. bytes[1] = *codes++;
  1724. bytes[2] = *codes;
  1725. if (keep == 0x11 || keep == 0x12)
  1726. {
  1727. // Rm
  1728. c = regval (&ins->oprs[1],1);
  1729. bytes[3] = c;
  1730. }
  1731. else
  1732. {
  1733. int shimm;
  1734. shimm = imm_shift (ins->oprs[1].offset);
  1735. if (shimm == -1)
  1736. {
  1737. errfunc (ERR_NONFATAL, "cannot create that constant");
  1738. }
  1739. bytes[3] = shimm & 0xFF;
  1740. bytes[2] |= (shimm & 0xF00) >> 8;
  1741. }
  1742. c = ins->oprs[0].basereg;
  1743. if ( keep == 0x11)
  1744. {
  1745. if ( c == R_CPSR || c == R_SPSR)
  1746. {
  1747. if ( c== R_SPSR)
  1748. {
  1749. bytes[1] |= 0x40;
  1750. }
  1751. }
  1752. else
  1753. {
  1754. errfunc (ERR_NONFATAL, "CPSR or SPSR expected");
  1755. }
  1756. }
  1757. else
  1758. {
  1759. if ( c == R_CPSR_FLG || c == R_SPSR_FLG)
  1760. {
  1761. if ( c== R_SPSR_FLG)
  1762. {
  1763. bytes[1] |= 0x40;
  1764. }
  1765. }
  1766. else
  1767. {
  1768. errfunc (ERR_NONFATAL, "CPSR_flg or SPSR_flg expected");
  1769. }
  1770. }
  1771. break;
  1772. case 0x14: // MUL Rd,Rm,Rs
  1773. case 0x15: // MULA Rd,Rm,Rs,Rn
  1774. ++codes;
  1775. bytes[0] = c | *codes++;
  1776. bytes[1] = *codes++;
  1777. bytes[3] = *codes;
  1778. // Rd
  1779. bytes[1] |= regval (&ins->oprs[0],1);
  1780. if (has_S_code)
  1781. bytes[1] |= 0x10;
  1782. // Rm
  1783. bytes[3] |= regval (&ins->oprs[1],1);
  1784. // Rs
  1785. bytes[2] = regval (&ins->oprs[2],1);
  1786. if (keep == 0x15)
  1787. {
  1788. bytes[2] |= regval (&ins->oprs[3],1) << 4;
  1789. }
  1790. break;
  1791. case 0x16: // SMLAL RdHi,RdLo,Rm,Rs
  1792. ++codes;
  1793. bytes[0] = c | *codes++;
  1794. bytes[1] = *codes++;
  1795. bytes[3] = *codes;
  1796. // RdHi
  1797. bytes[1] |= regval (&ins->oprs[1],1);
  1798. if (has_S_code)
  1799. bytes[1] |= 0x10;
  1800. // RdLo
  1801. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1802. // Rm
  1803. bytes[3] |= regval (&ins->oprs[2],1);
  1804. // Rs
  1805. bytes[2] |= regval (&ins->oprs[3],1);
  1806. break;
  1807. case 0x17: // LDR Rd, expression
  1808. ++codes;
  1809. bytes[0] = c | *codes++;
  1810. bytes[1] = *codes++;
  1811. // Rd
  1812. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1813. if (has_B_code)
  1814. bytes[1] |= 0x40;
  1815. if (has_T_code)
  1816. {
  1817. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1818. }
  1819. if (has_W_code)
  1820. {
  1821. errfunc (ERR_NONFATAL, "'!' not allowed");
  1822. }
  1823. // Rn - implicit R15
  1824. bytes[1] |= 0xF;
  1825. if (ins->oprs[1].segment != segment)
  1826. {
  1827. errfunc (ERR_NONFATAL, "label not in same segment");
  1828. }
  1829. data = ins->oprs[1].offset - (offset + 8);
  1830. if (data < 0)
  1831. {
  1832. data = -data;
  1833. }
  1834. else
  1835. {
  1836. bytes[1] |= 0x80;
  1837. }
  1838. if (data >= 0x1000)
  1839. {
  1840. errfunc (ERR_NONFATAL, "too long offset");
  1841. }
  1842. bytes[2] |= ((data & 0xF00) >> 8);
  1843. bytes[3] = data & 0xFF;
  1844. break;
  1845. case 0x18: // LDR Rd, [Rn]
  1846. ++codes;
  1847. bytes[0] = c | *codes++;
  1848. bytes[1] = *codes++;
  1849. // Rd
  1850. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1851. if (has_B_code)
  1852. bytes[1] |= 0x40;
  1853. if (has_T_code)
  1854. {
  1855. bytes[1] |= 0x20; // write-back
  1856. }
  1857. else
  1858. {
  1859. bytes[0] |= 0x01; // implicit pre-index mode
  1860. }
  1861. if (has_W_code)
  1862. {
  1863. bytes[1] |= 0x20; // write-back
  1864. }
  1865. // Rn
  1866. c = regval (&ins->oprs[1],1);
  1867. bytes[1] |= c;
  1868. if (c == 0x15) // R15
  1869. data = -8;
  1870. else
  1871. data = 0;
  1872. if (data < 0)
  1873. {
  1874. data = -data;
  1875. }
  1876. else
  1877. {
  1878. bytes[1] |= 0x80;
  1879. }
  1880. bytes[2] |= ((data & 0xF00) >> 8);
  1881. bytes[3] = data & 0xFF;
  1882. break;
  1883. case 0x19: // LDR Rd, [Rn,#expression]
  1884. case 0x20: // LDR Rd, [Rn,Rm]
  1885. case 0x21: // LDR Rd, [Rn,Rm,shift]
  1886. ++codes;
  1887. bytes[0] = c | *codes++;
  1888. bytes[1] = *codes++;
  1889. // Rd
  1890. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1891. if (has_B_code)
  1892. bytes[1] |= 0x40;
  1893. // Rn
  1894. c = regval (&ins->oprs[1],1);
  1895. bytes[1] |= c;
  1896. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  1897. {
  1898. bytes[0] |= 0x01; // pre-index mode
  1899. if (has_W_code)
  1900. {
  1901. bytes[1] |= 0x20;
  1902. }
  1903. if (has_T_code)
  1904. {
  1905. errfunc (ERR_NONFATAL, "'T' not allowed in pre-index mode");
  1906. }
  1907. }
  1908. else
  1909. {
  1910. if (has_T_code) // Forced write-back in post-index mode
  1911. {
  1912. bytes[1] |= 0x20;
  1913. }
  1914. if (has_W_code)
  1915. {
  1916. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  1917. }
  1918. }
  1919. if (keep == 0x19)
  1920. {
  1921. data = ins->oprs[2].offset;
  1922. if (data < 0)
  1923. {
  1924. data = -data;
  1925. }
  1926. else
  1927. {
  1928. bytes[1] |= 0x80;
  1929. }
  1930. if (data >= 0x1000)
  1931. {
  1932. errfunc (ERR_NONFATAL, "too long offset");
  1933. }
  1934. bytes[2] |= ((data & 0xF00) >> 8);
  1935. bytes[3] = data & 0xFF;
  1936. }
  1937. else
  1938. {
  1939. if (ins->oprs[2].minus == 0)
  1940. {
  1941. bytes[1] |= 0x80;
  1942. }
  1943. c = regval (&ins->oprs[2],1);
  1944. bytes[3] = c;
  1945. if (keep == 0x21)
  1946. {
  1947. c = ins->oprs[3].offset;
  1948. if (c > 0x1F)
  1949. {
  1950. errfunc (ERR_NONFATAL, "too large shiftvalue");
  1951. c = c & 0x1F;
  1952. }
  1953. bytes[2] |= c >> 1;
  1954. if (c & 0x01)
  1955. {
  1956. bytes[3] |= 0x80;
  1957. }
  1958. bytes[3] |= shiftval (&ins->oprs[3]) << 5;
  1959. }
  1960. }
  1961. break;
  1962. case 0x22: // LDRH Rd, expression
  1963. ++codes;
  1964. bytes[0] = c | 0x01; // Implicit pre-index
  1965. bytes[1] = *codes++;
  1966. // Rd
  1967. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1968. // Rn - implicit R15
  1969. bytes[1] |= 0xF;
  1970. if (ins->oprs[1].segment != segment)
  1971. {
  1972. errfunc (ERR_NONFATAL, "label not in same segment");
  1973. }
  1974. data = ins->oprs[1].offset - (offset + 8);
  1975. if (data < 0)
  1976. {
  1977. data = -data;
  1978. }
  1979. else
  1980. {
  1981. bytes[1] |= 0x80;
  1982. }
  1983. if (data >= 0x100)
  1984. {
  1985. errfunc (ERR_NONFATAL, "too long offset");
  1986. }
  1987. bytes[3] = *codes++;
  1988. bytes[2] |= ((data & 0xF0) >> 4);
  1989. bytes[3] |= data & 0xF;
  1990. break;
  1991. case 0x23: // LDRH Rd, Rn
  1992. ++codes;
  1993. bytes[0] = c | 0x01; // Implicit pre-index
  1994. bytes[1] = *codes++;
  1995. // Rd
  1996. bytes[2] = regval (&ins->oprs[0],1) << 4;
  1997. // Rn
  1998. c = regval (&ins->oprs[1],1);
  1999. bytes[1] |= c;
  2000. if (c == 0x15) // R15
  2001. data = -8;
  2002. else
  2003. data = 0;
  2004. if (data < 0)
  2005. {
  2006. data = -data;
  2007. }
  2008. else
  2009. {
  2010. bytes[1] |= 0x80;
  2011. }
  2012. if (data >= 0x100)
  2013. {
  2014. errfunc (ERR_NONFATAL, "too long offset");
  2015. }
  2016. bytes[3] = *codes++;
  2017. bytes[2] |= ((data & 0xF0) >> 4);
  2018. bytes[3] |= data & 0xF;
  2019. break;
  2020. case 0x24: // LDRH Rd, Rn, expression
  2021. case 0x25: // LDRH Rd, Rn, Rm
  2022. ++codes;
  2023. bytes[0] = c;
  2024. bytes[1] = *codes++;
  2025. // Rd
  2026. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2027. // Rn
  2028. c = regval (&ins->oprs[1],1);
  2029. bytes[1] |= c;
  2030. if (ins->oprs[ins->operands-1].bracket) // FIXME: Bracket on last operand -> pre-index <--
  2031. {
  2032. bytes[0] |= 0x01; // pre-index mode
  2033. if (has_W_code)
  2034. {
  2035. bytes[1] |= 0x20;
  2036. }
  2037. }
  2038. else
  2039. {
  2040. if (has_W_code)
  2041. {
  2042. errfunc (ERR_NONFATAL, "'!' not allowed in post-index mode");
  2043. }
  2044. }
  2045. bytes[3] = *codes++;
  2046. if (keep == 0x24)
  2047. {
  2048. data = ins->oprs[2].offset;
  2049. if (data < 0)
  2050. {
  2051. data = -data;
  2052. }
  2053. else
  2054. {
  2055. bytes[1] |= 0x80;
  2056. }
  2057. if (data >= 0x100)
  2058. {
  2059. errfunc (ERR_NONFATAL, "too long offset");
  2060. }
  2061. bytes[2] |= ((data & 0xF0) >> 4);
  2062. bytes[3] |= data & 0xF;
  2063. }
  2064. else
  2065. {
  2066. if (ins->oprs[2].minus == 0)
  2067. {
  2068. bytes[1] |= 0x80;
  2069. }
  2070. c = regval (&ins->oprs[2],1);
  2071. bytes[3] |= c;
  2072. }
  2073. break;
  2074. case 0x26: // LDM/STM Rn, {reg-list}
  2075. ++codes;
  2076. bytes[0] = c;
  2077. bytes[0] |= ( *codes >> 4) & 0xF;
  2078. bytes[1] = ( *codes << 4) & 0xF0;
  2079. ++codes;
  2080. if (has_W_code)
  2081. {
  2082. bytes[1] |= 0x20;
  2083. }
  2084. if (has_F_code)
  2085. {
  2086. bytes[1] |= 0x40;
  2087. }
  2088. // Rn
  2089. bytes[1] |= regval (&ins->oprs[0],1);
  2090. data = ins->oprs[1].basereg;
  2091. bytes[2] = ((data >> 8) & 0xFF);
  2092. bytes[3] = (data & 0xFF);
  2093. break;
  2094. case 0x27: // SWP Rd, Rm, [Rn]
  2095. ++codes;
  2096. bytes[0] = c;
  2097. bytes[0] |= *codes++;
  2098. bytes[1] = regval (&ins->oprs[2],1);
  2099. if (has_B_code)
  2100. {
  2101. bytes[1] |= 0x40;
  2102. }
  2103. bytes[2] = regval (&ins->oprs[0],1) << 4;
  2104. bytes[3] = *codes++;
  2105. bytes[3] |= regval (&ins->oprs[1],1);
  2106. break;
  2107. default:
  2108. errfunc (ERR_FATAL, "unknown decoding of instruction");
  2109. bytes[0] = c;
  2110. // And a fix nibble
  2111. ++codes;
  2112. bytes[0] |= *codes++;
  2113. if ( *codes == 0x01) // An I bit
  2114. {
  2115. }
  2116. if ( *codes == 0x02) // An I bit
  2117. {
  2118. }
  2119. ++codes;
  2120. }
  2121. out (offset, segment, bytes, OUT_RAWDATA+4, NO_SEG, NO_SEG);
  2122. }
  2123. *)
  2124. {$endif dummy}
  2125. begin
  2126. cai_align:=tai_align;
  2127. end.