aasmcpu.pas 85 KB

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