aasmcpu.pas 85 KB

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