aasmcpu.pas 86 KB

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