ra68kmot.pas 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  1. {
  2. Copyright (c) 1998-2000 by Carl Eric Codere
  3. This unit does the parsing process for the motorola inline assembler
  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 ra68kmot;
  18. {$i fpcdefs.inc}
  19. {**********************************************************************}
  20. { WARNING }
  21. {**********************************************************************}
  22. { Any modification in the order or removal of terms in the tables }
  23. { in m68k.pas and asmo68k.pas will BREAK the code in this unit, }
  24. { unless the appropriate changes are made to this unit. Addition }
  25. { of terms though, will not change the code herein. }
  26. {**********************************************************************}
  27. {---------------------------------------------------------------------------}
  28. { LEFT TO DO }
  29. {---------------------------------------------------------------------------}
  30. { o Add support for sized indexing such as in d0.l }
  31. { presently only (an,dn) is supported for indexing -- }
  32. { size defaults to LONG. }
  33. { o Add support for MC68020 opcodes. }
  34. { o Add support for MC68020 adressing modes. }
  35. { o Add operand checking with m68k opcode table in ConcatOpCode }
  36. { o Add Floating point support }
  37. {---------------------------------------------------------------------------}
  38. interface
  39. uses
  40. rasm;
  41. type
  42. tasmtoken = (
  43. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  44. AS_BINNUM,AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  45. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  46. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_APPT,AS_REALNUM,
  47. AS_ALIGN,
  48. {------------------ Assembler directives --------------------}
  49. AS_DB,AS_DW,AS_DD,AS_XDEF,AS_END,
  50. {------------------ Assembler Operators --------------------}
  51. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR);
  52. tm68kmotreader = class(tasmreader)
  53. actasmtoken: tasmtoken;
  54. actasmpattern: string;
  55. destructor destroy;override;
  56. end;
  57. Implementation
  58. uses
  59. { common }
  60. cutils,cclasses,
  61. { global }
  62. globtype,globals,verbose,
  63. systems,
  64. { aasm }
  65. cpuinfo,aasmbase,aasmtai,aasmcpu,
  66. { symtable }
  67. symconst,symbase,symtype,symsym,symtable,
  68. { pass 1 }
  69. nbas,
  70. { parser }
  71. scanner,agcpugas,
  72. rautils
  73. ;
  74. const
  75. { this variable is TRUE if the lookup tables have already been setup }
  76. { for fast access. On the first call to assemble the tables are setup }
  77. { and stay set up. }
  78. _asmsorted: boolean = FALSE;
  79. firstasmreg = R_D0;
  80. lastasmreg = R_FPSR;
  81. type
  82. tiasmops = array[firstop..lastop] of string[7];
  83. piasmops = ^tiasmops;
  84. tasmkeyword = string[6];
  85. var
  86. { sorted tables of opcodes }
  87. iasmops: piasmops;
  88. { uppercased tables of registers }
  89. iasmregs: array[firstasmreg..lastasmreg] of string[6];
  90. const
  91. regname_count=17;
  92. regname_count_bsstart=16;
  93. regname2regnum:array[0..regname_count-1] of regname2regnumrec=(
  94. (name:'A0'; number:NR_A0),
  95. (name:'A1'; number:NR_A1),
  96. (name:'A2'; number:NR_A2),
  97. (name:'A3'; number:NR_A3),
  98. (name:'A4'; number:NR_A4),
  99. (name:'A5'; number:NR_A5),
  100. (name:'A6'; number:NR_A6),
  101. (name:'A7'; number:NR_A7),
  102. (name:'D0'; number:NR_D0),
  103. (name:'D1'; number:NR_D1),
  104. (name:'D2'; number:NR_D2),
  105. (name:'D3'; number:NR_D3),
  106. (name:'D4'; number:NR_D4),
  107. (name:'D5'; number:NR_D5),
  108. (name:'D6'; number:NR_D6),
  109. (name:'D7'; number:NR_D7),
  110. (name:'SP'; number:NR_A7));
  111. const
  112. firstdirective = AS_DB;
  113. lastdirective = AS_END;
  114. firstoperator = AS_MOD;
  115. lastoperator = AS_XOR;
  116. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  117. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  118. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  119. ('DC.B','DC.W','DC.L','XDEF','END');
  120. { problems with shl,shr,not,and,or and xor, they are }
  121. { context sensitive. }
  122. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  123. 'MOD','SHL','SHR','NOT','AND','OR','XOR');
  124. const
  125. firsttoken : boolean = TRUE;
  126. operandnum : byte = 0;
  127. Procedure SetupTables;
  128. { creates uppercased symbol tables for speed access }
  129. var
  130. i: tasmop;
  131. j: tregister;
  132. begin
  133. {Message(asmr_d_creating_lookup_tables);}
  134. { opcodes }
  135. new(iasmops);
  136. for i:=firstop to lastop do
  137. iasmops^[i] := upper(gas_op2str[i]);
  138. { opcodes }
  139. for j.enum:=firstasmreg to lastasmreg do
  140. iasmregs[j.enum] := upper(std_reg2str[j.enum]);
  141. end;
  142. {---------------------------------------------------------------------}
  143. { Routines for the tokenizing }
  144. {---------------------------------------------------------------------}
  145. function regnum_search(const s:string):Tnewregister;
  146. {Searches the register number that belongs to the register in s.
  147. s must be in uppercase!.}
  148. var i,p:byte;
  149. begin
  150. {Binary search.}
  151. p:=0;
  152. i:=regname_count_bsstart;
  153. while i<>0 do
  154. begin
  155. if (p+i<regname_count) and (upper(regname2regnum[p+i].name)<=s) then
  156. p:=p+i;
  157. i:=i shr 1;
  158. end;
  159. if upper(regname2regnum[p].name)=s then
  160. regnum_search:=regname2regnum[p].number
  161. else
  162. regnum_search:=NR_NO;
  163. end;
  164. function is_asmopcode(s: string):Boolean;
  165. {*********************************************************************}
  166. { FUNCTION is_asmopcode(s: string):Boolean }
  167. { Description: Determines if the s string is a valid opcode }
  168. { if so returns TRUE otherwise returns FALSE. }
  169. { Remark: Suffixes are also checked, as long as they are valid. }
  170. {*********************************************************************}
  171. var
  172. i: tasmop;
  173. j: byte;
  174. begin
  175. is_asmopcode := FALSE;
  176. { first of all we remove the suffix }
  177. j:=pos('.',s);
  178. if j<>0 then
  179. delete(s,j,2);
  180. for i:=firstop to lastop do
  181. begin
  182. if s = iasmops^[i] then
  183. begin
  184. is_asmopcode:=TRUE;
  185. exit;
  186. end;
  187. end;
  188. end;
  189. Procedure is_asmdirective(const s: string; var token: tasmtoken);
  190. {*********************************************************************}
  191. { FUNCTION is_asmdirective(s: string; var token: tinteltoken):Boolean }
  192. { Description: Determines if the s string is a valid directive }
  193. { (an operator can occur in operand fields, while a directive cannot) }
  194. { if so returns the directive token, otherwise does not change token.}
  195. {*********************************************************************}
  196. var
  197. i:byte;
  198. begin
  199. for i:=0 to _count_asmdirectives do
  200. begin
  201. if s=_asmdirectives[i] then
  202. begin
  203. token := tasmtoken(longint(firstdirective)+i);
  204. exit;
  205. end;
  206. end;
  207. end;
  208. Procedure is_register(const s: string; var token: tasmtoken);
  209. {*********************************************************************}
  210. { PROCEDURE is_register(s: string; var token: tinteltoken); }
  211. { Description: Determines if the s string is a valid register, if }
  212. { so return token equal to A_REGISTER, otherwise does not change token}
  213. {*********************************************************************}
  214. var
  215. i: tregister;
  216. begin
  217. if regnum_search(s)=NR_NO then
  218. begin
  219. for i.enum:=firstasmreg to lastasmreg do
  220. begin
  221. if s=iasmregs[i.enum] then
  222. begin
  223. token := AS_REGISTER;
  224. exit;
  225. end;
  226. end;
  227. { take care of other name for sp }
  228. if s = 'A7' then
  229. begin
  230. token:=AS_REGISTER;
  231. exit;
  232. end;
  233. end
  234. else
  235. token:=AS_REGISTER;
  236. end;
  237. Function GetToken: tasmtoken;
  238. {*********************************************************************}
  239. { FUNCTION GetToken: tinteltoken; }
  240. { Description: This routine returns intel assembler tokens and }
  241. { does some minor syntax error checking. }
  242. {*********************************************************************}
  243. var
  244. token: tasmtoken;
  245. forcelabel: boolean;
  246. begin
  247. forcelabel := FALSE;
  248. actasmpattern :='';
  249. {* INIT TOKEN TO NOTHING *}
  250. token := AS_NONE;
  251. { while space and tab , continue scan... }
  252. while c in [' ',#9] do
  253. c:=current_scanner.asmgetchar;
  254. if not (c in [newline,#13,'{',';']) then
  255. current_scanner.gettokenpos;
  256. { Possiblities for first token in a statement: }
  257. { Local Label, Label, Directive, Prefix or Opcode.... }
  258. if firsttoken and not (c in [newline,#13,'{',';']) then
  259. begin
  260. firsttoken := FALSE;
  261. if c = '@' then
  262. begin
  263. token := AS_LLABEL; { this is a local label }
  264. { Let us point to the next character }
  265. c := current_scanner.asmgetchar;
  266. end;
  267. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  268. begin
  269. { if there is an at_sign, then this must absolutely be a label }
  270. if c = '@' then forcelabel:=TRUE;
  271. actasmpattern := actasmpattern + c;
  272. c := current_scanner.asmgetchar;
  273. end;
  274. uppervar(actasmpattern);
  275. if c = ':' then
  276. begin
  277. case token of
  278. AS_NONE: token := AS_LABEL;
  279. AS_LLABEL: ; { do nothing }
  280. end; { end case }
  281. { let us point to the next character }
  282. c := current_scanner.asmgetchar;
  283. gettoken := token;
  284. exit;
  285. end;
  286. { Are we trying to create an identifier with }
  287. { an at-sign...? }
  288. if forcelabel then
  289. Message(asmr_e_none_label_contain_at);
  290. If is_asmopcode(actasmpattern) then
  291. begin
  292. gettoken := AS_OPCODE;
  293. exit;
  294. end;
  295. is_asmdirective(actasmpattern, token);
  296. if (token <> AS_NONE) then
  297. begin
  298. gettoken := token;
  299. exit
  300. end
  301. else
  302. begin
  303. gettoken := AS_NONE;
  304. Message1(asmr_e_invalid_or_missing_opcode,actasmpattern);
  305. end;
  306. end
  307. else { else firsttoken }
  308. { Here we must handle all possible cases }
  309. begin
  310. case c of
  311. '@': { possiblities : - local label reference , such as in jmp @local1 }
  312. { - @Result, @Code or @Data special variables. }
  313. begin
  314. actasmpattern := c;
  315. c:= current_scanner.asmgetchar;
  316. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  317. begin
  318. actasmpattern := actasmpattern + c;
  319. c := current_scanner.asmgetchar;
  320. end;
  321. uppervar(actasmpattern);
  322. gettoken := AS_ID;
  323. exit;
  324. end;
  325. { identifier, register, opcode, prefix or directive }
  326. 'A'..'Z','a'..'z','_': begin
  327. actasmpattern := c;
  328. c:= current_scanner.asmgetchar;
  329. while c in ['A'..'Z','a'..'z','0'..'9','_','.'] do
  330. begin
  331. actasmpattern := actasmpattern + c;
  332. c := current_scanner.asmgetchar;
  333. end;
  334. uppervar(actasmpattern);
  335. If is_asmopcode(actasmpattern) then
  336. begin
  337. gettoken := AS_OPCODE;
  338. exit;
  339. end;
  340. is_register(actasmpattern, token);
  341. {is_asmoperator(actasmpattern,token);}
  342. is_asmdirective(actasmpattern,token);
  343. { if found }
  344. if (token <> AS_NONE) then
  345. begin
  346. gettoken := token;
  347. exit;
  348. end
  349. { this is surely an identifier }
  350. else
  351. token := AS_ID;
  352. gettoken := token;
  353. exit;
  354. end;
  355. { override operator... not supported }
  356. '&': begin
  357. c:=current_scanner.asmgetchar;
  358. gettoken := AS_AND;
  359. end;
  360. { string or character }
  361. '''' :
  362. begin
  363. actasmpattern:='';
  364. while true do
  365. begin
  366. if c = '''' then
  367. begin
  368. c:=current_scanner.asmgetchar;
  369. if c=newline then
  370. begin
  371. Message(scan_f_string_exceeds_line);
  372. break;
  373. end;
  374. repeat
  375. if c=''''then
  376. begin
  377. c:=current_scanner.asmgetchar;
  378. if c='''' then
  379. begin
  380. actasmpattern:=actasmpattern+'''';
  381. c:=current_scanner.asmgetchar;
  382. if c=newline then
  383. begin
  384. Message(scan_f_string_exceeds_line);
  385. break;
  386. end;
  387. end
  388. else break;
  389. end
  390. else
  391. begin
  392. actasmpattern:=actasmpattern+c;
  393. c:=current_scanner.asmgetchar;
  394. if c=newline then
  395. begin
  396. Message(scan_f_string_exceeds_line);
  397. break
  398. end;
  399. end;
  400. until false; { end repeat }
  401. end
  402. else break; { end if }
  403. end; { end while }
  404. token:=AS_STRING;
  405. gettoken := token;
  406. exit;
  407. end;
  408. '$' : begin
  409. c:=current_scanner.asmgetchar;
  410. while c in ['0'..'9','A'..'F','a'..'f'] do
  411. begin
  412. actasmpattern := actasmpattern + c;
  413. c := current_scanner.asmgetchar;
  414. end;
  415. gettoken := AS_HEXNUM;
  416. exit;
  417. end;
  418. ',' : begin
  419. gettoken := AS_COMMA;
  420. c:=current_scanner.asmgetchar;
  421. exit;
  422. end;
  423. '(' : begin
  424. gettoken := AS_LPAREN;
  425. c:=current_scanner.asmgetchar;
  426. exit;
  427. end;
  428. ')' : begin
  429. gettoken := AS_RPAREN;
  430. c:=current_scanner.asmgetchar;
  431. exit;
  432. end;
  433. ':' : begin
  434. gettoken := AS_COLON;
  435. c:=current_scanner.asmgetchar;
  436. exit;
  437. end;
  438. { '.' : begin
  439. gettoken := AS_DOT;
  440. c:=current_scanner.asmgetchar;
  441. exit;
  442. end; }
  443. '+' : begin
  444. gettoken := AS_PLUS;
  445. c:=current_scanner.asmgetchar;
  446. exit;
  447. end;
  448. '-' : begin
  449. gettoken := AS_MINUS;
  450. c:=current_scanner.asmgetchar;
  451. exit;
  452. end;
  453. '*' : begin
  454. gettoken := AS_STAR;
  455. c:=current_scanner.asmgetchar;
  456. exit;
  457. end;
  458. '/' : begin
  459. gettoken := AS_SLASH;
  460. c:=current_scanner.asmgetchar;
  461. exit;
  462. end;
  463. '<' : begin
  464. c := current_scanner.asmgetchar;
  465. { invalid characters }
  466. if c <> '<' then
  467. Message(asmr_e_invalid_char_smaller);
  468. { still assume << }
  469. gettoken := AS_SHL;
  470. c := current_scanner.asmgetchar;
  471. exit;
  472. end;
  473. '>' : begin
  474. c := current_scanner.asmgetchar;
  475. { invalid characters }
  476. if c <> '>' then
  477. Message(asmr_e_invalid_char_greater);
  478. { still assume << }
  479. gettoken := AS_SHR;
  480. c := current_scanner.asmgetchar;
  481. exit;
  482. end;
  483. '|' : begin
  484. gettoken := AS_OR;
  485. c := current_scanner.asmgetchar;
  486. exit;
  487. end;
  488. '^' : begin
  489. gettoken := AS_XOR;
  490. c := current_scanner.asmgetchar;
  491. exit;
  492. end;
  493. '#' : begin
  494. gettoken:=AS_APPT;
  495. c:=current_scanner.asmgetchar;
  496. exit;
  497. end;
  498. '%' : begin
  499. c:=current_scanner.asmgetchar;
  500. while c in ['0','1'] do
  501. begin
  502. actasmpattern := actasmpattern + c;
  503. c := current_scanner.asmgetchar;
  504. end;
  505. gettoken := AS_BINNUM;
  506. exit;
  507. end;
  508. { integer number }
  509. '0'..'9': begin
  510. actasmpattern := c;
  511. c := current_scanner.asmgetchar;
  512. while c in ['0'..'9'] do
  513. begin
  514. actasmpattern := actasmpattern + c;
  515. c:= current_scanner.asmgetchar;
  516. end;
  517. gettoken := AS_INTNUM;
  518. exit;
  519. end;
  520. ';' : begin
  521. repeat
  522. c:=current_scanner.asmgetchar;
  523. until c=newline;
  524. firsttoken := TRUE;
  525. gettoken:=AS_SEPARATOR;
  526. end;
  527. '{',#13,newline : begin
  528. c:=current_scanner.asmgetchar;
  529. firsttoken := TRUE;
  530. gettoken:=AS_SEPARATOR;
  531. end;
  532. else
  533. begin
  534. Message(scan_f_illegal_char);
  535. end;
  536. end; { end case }
  537. end; { end else if }
  538. end;
  539. {---------------------------------------------------------------------}
  540. { Routines for the parsing }
  541. {---------------------------------------------------------------------}
  542. procedure consume(t : tasmtoken);
  543. begin
  544. if t<>actasmtoken then
  545. Message(asmr_e_syntax_error);
  546. actasmtoken:=gettoken;
  547. { if the token must be ignored, then }
  548. { get another token to parse. }
  549. if actasmtoken = AS_NONE then
  550. actasmtoken := gettoken;
  551. end;
  552. function findregister(const s : string): tregister;
  553. {*********************************************************************}
  554. { FUNCTION findregister(s: string):tasmop; }
  555. { Description: Determines if the s string is a valid register, }
  556. { if so returns correct tregister token, or R_NO if not found. }
  557. {*********************************************************************}
  558. var
  559. i: tregister;
  560. begin
  561. i.enum:=R_INTREGISTER;
  562. i.number:=regnum_search(s);
  563. if i.number=NR_NO then
  564. begin
  565. findregister.enum := R_NO;
  566. for i.enum:=firstasmreg to lastasmreg do
  567. if s = iasmregs[i.enum] then
  568. begin
  569. findregister := i;
  570. exit;
  571. end;
  572. if s = 'A7' then
  573. begin
  574. findregister.enum := R_SP;
  575. exit;
  576. end;
  577. end
  578. else
  579. findregister:=i;
  580. end;
  581. function findopcode(s: string; var opsize: topsize): tasmop;
  582. {*********************************************************************}
  583. { FUNCTION findopcode(s: string): tasmop; }
  584. { Description: Determines if the s string is a valid opcode }
  585. { if so returns correct tasmop token. }
  586. {*********************************************************************}
  587. var
  588. i: tasmop;
  589. j: byte;
  590. op_size: string;
  591. begin
  592. findopcode := A_NONE;
  593. j:=pos('.',s);
  594. if j<>0 then
  595. begin
  596. op_size:=copy(s,j+1,1);
  597. case op_size[1] of
  598. { For the motorola only opsize size is used to }
  599. { determine the size of the operands. }
  600. 'B': opsize := S_B;
  601. 'W': opsize := S_W;
  602. 'L': opsize := S_L;
  603. 'S': opsize := S_FS;
  604. 'D': opsize := S_FD;
  605. 'X': opsize := S_FX;
  606. else
  607. Message1(asmr_e_unknown_opcode,s);
  608. end;
  609. { delete everything starting from dot }
  610. delete(s,j,length(s));
  611. end;
  612. for i:=firstop to lastop do
  613. if s = iasmops^[i] then
  614. begin
  615. findopcode:=i;
  616. exit;
  617. end;
  618. end;
  619. Function BuildExpression(allow_symbol : boolean; asmsym : pstring) : longint;
  620. {*********************************************************************}
  621. { FUNCTION BuildExpression: longint }
  622. { Description: This routine calculates a constant expression to }
  623. { a given value. The return value is the value calculated from }
  624. { the expression. }
  625. { The following tokens (not strings) are recognized: }
  626. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  627. {*********************************************************************}
  628. { ENTRY: On entry the token should be any valid expression token. }
  629. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  630. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  631. { invalid tokens. }
  632. {*********************************************************************}
  633. var expr: string;
  634. hs, tempstr: string;
  635. sym : tsym;
  636. srsymtable : tsymtable;
  637. hl : tasmlabel;
  638. l : longint;
  639. errorflag: boolean;
  640. begin
  641. errorflag := FALSE;
  642. expr := '';
  643. tempstr := '';
  644. if allow_symbol then
  645. asmsym^:='';
  646. Repeat
  647. Case actasmtoken of
  648. AS_LPAREN: begin
  649. Consume(AS_LPAREN);
  650. expr := expr + '(';
  651. end;
  652. AS_RPAREN: begin
  653. Consume(AS_RPAREN);
  654. expr := expr + ')';
  655. end;
  656. AS_SHL: begin
  657. Consume(AS_SHL);
  658. expr := expr + '<';
  659. end;
  660. AS_SHR: begin
  661. Consume(AS_SHR);
  662. expr := expr + '>';
  663. end;
  664. AS_SLASH: begin
  665. Consume(AS_SLASH);
  666. expr := expr + '/';
  667. end;
  668. AS_MOD: begin
  669. Consume(AS_MOD);
  670. expr := expr + '%';
  671. end;
  672. AS_STAR: begin
  673. Consume(AS_STAR);
  674. expr := expr + '*';
  675. end;
  676. AS_PLUS: begin
  677. Consume(AS_PLUS);
  678. expr := expr + '+';
  679. end;
  680. AS_MINUS: begin
  681. Consume(AS_MINUS);
  682. expr := expr + '-';
  683. end;
  684. AS_AND: begin
  685. Consume(AS_AND);
  686. expr := expr + '&';
  687. end;
  688. AS_NOT: begin
  689. Consume(AS_NOT);
  690. expr := expr + '~';
  691. end;
  692. AS_XOR: begin
  693. Consume(AS_XOR);
  694. expr := expr + '^';
  695. end;
  696. AS_OR: begin
  697. Consume(AS_OR);
  698. expr := expr + '|';
  699. end;
  700. AS_ID: begin
  701. if SearchIConstant(actasmpattern,l) then
  702. begin
  703. str(l, tempstr);
  704. expr := expr + tempstr;
  705. Consume(AS_ID);
  706. End else
  707. if not allow_symbol then
  708. begin
  709. Message(asmr_e_syn_constant);
  710. l := 0;
  711. End else
  712. begin
  713. hs:='';
  714. if (expr[Length(expr)]='+') then
  715. Delete(expr,Length(expr),1)
  716. else if expr<>'' then
  717. begin
  718. Message(asmr_e_invalid_constant_expression);
  719. break;
  720. End;
  721. tempstr:=actasmpattern;
  722. consume(AS_ID);
  723. if (length(tempstr)>1) and (tempstr[1]='@') then
  724. begin
  725. CreateLocalLabel(tempstr,hl,false);
  726. hs:=hl.name
  727. end
  728. else if SearchLabel(tempstr,hl,false) then
  729. hs:=hl.name
  730. else
  731. begin
  732. searchsym(tempstr,sym,srsymtable);
  733. if assigned(sym) then
  734. begin
  735. case sym.typ of
  736. varsym :
  737. begin
  738. if sym.owner.symtabletype in [localsymtable,parasymtable] then
  739. Message(asmr_e_no_local_or_para_allowed);
  740. hs:=tvarsym(sym).mangledname;
  741. end;
  742. typedconstsym :
  743. hs:=ttypedconstsym(sym).mangledname;
  744. procsym :
  745. begin
  746. if tprocsym(sym).procdef_count>1 then
  747. Message(asmr_w_calling_overload_func);
  748. hs:=tprocsym(sym).first_procdef.mangledname;
  749. end;
  750. typesym :
  751. begin
  752. if not(ttypesym(sym).restype.def.deftype in [recorddef,objectdef]) then
  753. Message(asmr_e_wrong_sym_type);
  754. end;
  755. else
  756. Message(asmr_e_wrong_sym_type);
  757. end;
  758. end
  759. else
  760. Message1(sym_e_unknown_id,tempstr);
  761. end;
  762. { symbol found? }
  763. if hs<>'' then
  764. begin
  765. if asmsym^='' then
  766. asmsym^:=hs
  767. else
  768. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  769. end;
  770. end;
  771. end;
  772. AS_INTNUM: begin
  773. expr := expr + actasmpattern;
  774. Consume(AS_INTNUM);
  775. end;
  776. AS_BINNUM: begin
  777. tempstr := tostr(ValBinary(actasmpattern));
  778. if tempstr = '' then
  779. Message(asmr_e_error_converting_binary);
  780. expr:=expr+tempstr;
  781. Consume(AS_BINNUM);
  782. end;
  783. AS_HEXNUM: begin
  784. tempstr := tostr(ValHexadecimal(actasmpattern));
  785. if tempstr = '' then
  786. Message(asmr_e_error_converting_hexadecimal);
  787. expr:=expr+tempstr;
  788. Consume(AS_HEXNUM);
  789. end;
  790. AS_OCTALNUM: begin
  791. tempstr := tostr(ValOctal(actasmpattern));
  792. if tempstr = '' then
  793. Message(asmr_e_error_converting_octal);
  794. expr:=expr+tempstr;
  795. Consume(AS_OCTALNUM);
  796. end;
  797. { go to next term }
  798. AS_COMMA: begin
  799. if not ErrorFlag then
  800. BuildExpression := CalculateExpression(expr)
  801. else
  802. BuildExpression := 0;
  803. Exit;
  804. end;
  805. { go to next symbol }
  806. AS_SEPARATOR: begin
  807. if not ErrorFlag then
  808. BuildExpression := CalculateExpression(expr)
  809. else
  810. BuildExpression := 0;
  811. Exit;
  812. end;
  813. else
  814. begin
  815. { only write error once. }
  816. if not errorflag then
  817. Message(asmr_e_invalid_constant_expression);
  818. { consume tokens until we find COMMA or SEPARATOR }
  819. Consume(actasmtoken);
  820. errorflag := TRUE;
  821. End;
  822. end;
  823. Until false;
  824. end;
  825. Procedure BuildRealConstant(typ : tfloattype);
  826. {*********************************************************************}
  827. { PROCEDURE BuilRealConst }
  828. { Description: This routine calculates a constant expression to }
  829. { a given value. The return value is the value calculated from }
  830. { the expression. }
  831. { The following tokens (not strings) are recognized: }
  832. { +/-,numbers and real numbers }
  833. {*********************************************************************}
  834. { ENTRY: On entry the token should be any valid expression token. }
  835. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  836. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  837. { invalid tokens. }
  838. {*********************************************************************}
  839. var expr: string;
  840. r : extended;
  841. code : word;
  842. negativ : boolean;
  843. errorflag: boolean;
  844. begin
  845. errorflag := FALSE;
  846. Repeat
  847. negativ:=false;
  848. expr := '';
  849. if actasmtoken=AS_PLUS then Consume(AS_PLUS)
  850. else if actasmtoken=AS_MINUS then
  851. begin
  852. negativ:=true;
  853. consume(AS_MINUS);
  854. end;
  855. Case actasmtoken of
  856. AS_INTNUM: begin
  857. expr := actasmpattern;
  858. Consume(AS_INTNUM);
  859. end;
  860. AS_REALNUM: begin
  861. expr := actasmpattern;
  862. { in ATT syntax you have 0d in front of the real }
  863. { should this be forced ? yes i think so, as to }
  864. { conform to gas as much as possible. }
  865. if (expr[1]='0') and (upper(expr[2])='D') then
  866. expr:=copy(expr,3,255);
  867. Consume(AS_REALNUM);
  868. end;
  869. AS_BINNUM: begin
  870. { checking for real constants with this should use }
  871. { real DECODING otherwise the compiler will crash! }
  872. Message(asmr_e_invalid_float_expr);
  873. expr:='0.0';
  874. Consume(AS_BINNUM);
  875. end;
  876. AS_HEXNUM: begin
  877. { checking for real constants with this should use }
  878. { real DECODING otherwise the compiler will crash! }
  879. Message(asmr_e_invalid_float_expr);
  880. expr:='0.0';
  881. Consume(AS_HEXNUM);
  882. end;
  883. AS_OCTALNUM: begin
  884. { checking for real constants with this should use }
  885. { real DECODING otherwise the compiler will crash! }
  886. { xxxToDec using reals could be a solution, but the }
  887. { problem is that these will crash the m68k compiler }
  888. { when compiling -- because of lack of good fpu }
  889. { support. }
  890. Message(asmr_e_invalid_float_expr);
  891. expr:='0.0';
  892. Consume(AS_OCTALNUM);
  893. end;
  894. else
  895. begin
  896. { only write error once. }
  897. if not errorflag then
  898. Message(asmr_e_invalid_float_expr);
  899. { consume tokens until we find COMMA or SEPARATOR }
  900. Consume(actasmtoken);
  901. errorflag := TRUE;
  902. End;
  903. end;
  904. { go to next term }
  905. if (actasmtoken=AS_COMMA) or (actasmtoken=AS_SEPARATOR) then
  906. begin
  907. if negativ then expr:='-'+expr;
  908. val(expr,r,code);
  909. if code<>0 then
  910. begin
  911. r:=0;
  912. Message(asmr_e_invalid_float_expr);
  913. ConcatRealConstant(curlist,r,typ);
  914. End
  915. else
  916. begin
  917. ConcatRealConstant(curlist,r,typ);
  918. End;
  919. end
  920. else
  921. Message(asmr_e_invalid_float_expr);
  922. Until actasmtoken=AS_SEPARATOR;
  923. end;
  924. Procedure BuildConstant(maxvalue: longint);
  925. {*********************************************************************}
  926. { PROCEDURE BuildConstant }
  927. { Description: This routine takes care of parsing a DB,DD,or DW }
  928. { line and adding those to the assembler node. Expressions, range- }
  929. { checking are fullly taken care of. }
  930. { maxvalue: $ff -> indicates that this is a DB node. }
  931. { $ffff -> indicates that this is a DW node. }
  932. { $ffffffff -> indicates that this is a DD node. }
  933. {*********************************************************************}
  934. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  935. {*********************************************************************}
  936. var
  937. strlength: byte;
  938. expr: string;
  939. tempstr: string;
  940. value : longint;
  941. begin
  942. Repeat
  943. Case actasmtoken of
  944. AS_STRING: begin
  945. if maxvalue = $ff then
  946. strlength := 1
  947. else
  948. Message(asmr_e_string_not_allowed_as_const);
  949. expr := actasmpattern;
  950. if length(expr) > 1 then
  951. Message(asmr_e_string_not_allowed_as_const);
  952. Consume(AS_STRING);
  953. Case actasmtoken of
  954. AS_COMMA: Consume(AS_COMMA);
  955. AS_SEPARATOR: ;
  956. else
  957. Message(asmr_e_invalid_string_expression);
  958. end; { end case }
  959. ConcatString(curlist,expr);
  960. end;
  961. AS_INTNUM,AS_BINNUM,
  962. AS_OCTALNUM,AS_HEXNUM:
  963. begin
  964. value:=BuildExpression(false,nil);
  965. ConcatConstant(curlist,value,maxvalue);
  966. end;
  967. AS_ID:
  968. begin
  969. value:=BuildExpression(false,nil);
  970. if value > maxvalue then
  971. begin
  972. Message(asmr_e_constant_out_of_bounds);
  973. { assuming a value of maxvalue }
  974. value := maxvalue;
  975. end;
  976. ConcatConstant(curlist,value,maxvalue);
  977. end;
  978. { These terms can start an assembler expression }
  979. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: begin
  980. value := BuildExpression(false,nil);
  981. ConcatConstant(curlist,value,maxvalue);
  982. end;
  983. AS_COMMA: begin
  984. Consume(AS_COMMA);
  985. END;
  986. AS_SEPARATOR: ;
  987. else
  988. begin
  989. Message(asmr_e_syntax_error);
  990. end;
  991. end; { end case }
  992. Until actasmtoken = AS_SEPARATOR;
  993. end;
  994. {****************************************************************************
  995. Tm68kOperand
  996. ****************************************************************************}
  997. type
  998. TM68kOperand=class(TOperand)
  999. Procedure BuildOperand;override;
  1000. private
  1001. labeled : boolean;
  1002. Procedure BuildReference;
  1003. Function BuildRefExpression: longint;
  1004. Procedure BuildScaling;
  1005. end;
  1006. Procedure TM68kOperand.BuildScaling;
  1007. {*********************************************************************}
  1008. { Takes care of parsing expression starting from the scaling value }
  1009. { up to and including possible field specifiers. }
  1010. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  1011. { or AS_COMMA. On entry should point to the AS_STAR token. }
  1012. {*********************************************************************}
  1013. var str:string;
  1014. l: longint;
  1015. code: integer;
  1016. begin
  1017. Consume(AS_STAR);
  1018. if (opr.ref.scalefactor <> 0)
  1019. and (opr.ref.scalefactor <> 1) then
  1020. Message(asmr_e_wrong_base_index);
  1021. case actasmtoken of
  1022. AS_INTNUM: str := actasmpattern;
  1023. AS_HEXNUM: str := Tostr(ValHexadecimal(actasmpattern));
  1024. AS_BINNUM: str := Tostr(ValBinary(actasmpattern));
  1025. AS_OCTALNUM: str := Tostr(ValOctal(actasmpattern));
  1026. else
  1027. Message(asmr_e_syntax_error);
  1028. end;
  1029. val(str, l, code);
  1030. if code <> 0 then
  1031. Message(asmr_e_wrong_scale_factor);
  1032. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  1033. begin
  1034. opr.ref.scalefactor := l;
  1035. end
  1036. else
  1037. begin
  1038. Message(asmr_e_wrong_scale_factor);
  1039. opr.ref.scalefactor := 0;
  1040. end;
  1041. if opr.ref.index.enum = R_NO then
  1042. begin
  1043. Message(asmr_e_wrong_base_index);
  1044. opr.ref.scalefactor := 0;
  1045. end;
  1046. { Consume the scaling number }
  1047. Consume(actasmtoken);
  1048. if actasmtoken = AS_RPAREN then
  1049. Consume(AS_RPAREN)
  1050. else
  1051. Message(asmr_e_wrong_scale_factor);
  1052. { // .Field.Field ... or separator/comma // }
  1053. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  1054. begin
  1055. end
  1056. else
  1057. Message(asmr_e_syntax_error);
  1058. end;
  1059. Function TM68kOperand.BuildRefExpression: longint;
  1060. {*********************************************************************}
  1061. { FUNCTION BuildRefExpression: longint }
  1062. { Description: This routine calculates a constant expression to }
  1063. { a given value. The return value is the value calculated from }
  1064. { the expression. }
  1065. { The following tokens (not strings) are recognized: }
  1066. { SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  1067. {*********************************************************************}
  1068. { ENTRY: On entry the token should be any valid expression token. }
  1069. { EXIT: On Exit the token points to the LPAREN token. }
  1070. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  1071. { invalid tokens. }
  1072. {*********************************************************************}
  1073. var tempstr: string;
  1074. expr: string;
  1075. l : longint;
  1076. errorflag : boolean;
  1077. begin
  1078. errorflag := FALSE;
  1079. tempstr := '';
  1080. expr := '';
  1081. Repeat
  1082. Case actasmtoken of
  1083. AS_RPAREN: begin
  1084. Message(asmr_e_syntax_error);
  1085. Consume(AS_RPAREN);
  1086. end;
  1087. AS_SHL: begin
  1088. Consume(AS_SHL);
  1089. expr := expr + '<';
  1090. end;
  1091. AS_SHR: begin
  1092. Consume(AS_SHR);
  1093. expr := expr + '>';
  1094. end;
  1095. AS_SLASH: begin
  1096. Consume(AS_SLASH);
  1097. expr := expr + '/';
  1098. end;
  1099. AS_MOD: begin
  1100. Consume(AS_MOD);
  1101. expr := expr + '%';
  1102. end;
  1103. AS_STAR: begin
  1104. Consume(AS_STAR);
  1105. expr := expr + '*';
  1106. end;
  1107. AS_PLUS: begin
  1108. Consume(AS_PLUS);
  1109. expr := expr + '+';
  1110. end;
  1111. AS_MINUS: begin
  1112. Consume(AS_MINUS);
  1113. expr := expr + '-';
  1114. end;
  1115. AS_AND: begin
  1116. Consume(AS_AND);
  1117. expr := expr + '&';
  1118. end;
  1119. AS_NOT: begin
  1120. Consume(AS_NOT);
  1121. expr := expr + '~';
  1122. end;
  1123. AS_XOR: begin
  1124. Consume(AS_XOR);
  1125. expr := expr + '^';
  1126. end;
  1127. AS_OR: begin
  1128. Consume(AS_OR);
  1129. expr := expr + '|';
  1130. end;
  1131. { End of reference }
  1132. AS_LPAREN: begin
  1133. if not ErrorFlag then
  1134. BuildRefExpression := CalculateExpression(expr)
  1135. else
  1136. BuildRefExpression := 0;
  1137. { no longer in an expression }
  1138. exit;
  1139. end;
  1140. AS_ID:
  1141. begin
  1142. if NOT SearchIConstant(actasmpattern,l) then
  1143. begin
  1144. Message(asmr_e_syn_constant);
  1145. l := 0;
  1146. end;
  1147. str(l, tempstr);
  1148. expr := expr + tempstr;
  1149. Consume(AS_ID);
  1150. end;
  1151. AS_INTNUM: begin
  1152. expr := expr + actasmpattern;
  1153. Consume(AS_INTNUM);
  1154. end;
  1155. AS_BINNUM: begin
  1156. tempstr := Tostr(ValBinary(actasmpattern));
  1157. if tempstr = '' then
  1158. Message(asmr_e_error_converting_binary);
  1159. expr:=expr+tempstr;
  1160. Consume(AS_BINNUM);
  1161. end;
  1162. AS_HEXNUM: begin
  1163. tempstr := Tostr(ValHexadecimal(actasmpattern));
  1164. if tempstr = '' then
  1165. Message(asmr_e_error_converting_hexadecimal);
  1166. expr:=expr+tempstr;
  1167. Consume(AS_HEXNUM);
  1168. end;
  1169. AS_OCTALNUM: begin
  1170. tempstr := Tostr(ValOctal(actasmpattern));
  1171. if tempstr = '' then
  1172. Message(asmr_e_error_converting_octal);
  1173. expr:=expr+tempstr;
  1174. Consume(AS_OCTALNUM);
  1175. end;
  1176. else
  1177. begin
  1178. { write error only once. }
  1179. if not errorflag then
  1180. Message(asmr_e_invalid_constant_expression);
  1181. BuildRefExpression := 0;
  1182. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then exit;
  1183. { consume tokens until we find COMMA or SEPARATOR }
  1184. Consume(actasmtoken);
  1185. errorflag := TRUE;
  1186. end;
  1187. end;
  1188. Until false;
  1189. end;
  1190. {*********************************************************************}
  1191. { PROCEDURE BuildBracketExpression }
  1192. { Description: This routine builds up an expression after a LPAREN }
  1193. { token is encountered. }
  1194. { On entry actasmtoken should be equal to AS_LPAREN }
  1195. {*********************************************************************}
  1196. { EXIT CONDITION: On exit the routine should point to either the }
  1197. { AS_COMMA or AS_SEPARATOR token. }
  1198. {*********************************************************************}
  1199. procedure TM68kOperand.BuildReference;
  1200. var
  1201. l:longint;
  1202. code: integer;
  1203. str: string;
  1204. begin
  1205. Consume(AS_LPAREN);
  1206. case actasmtoken of
  1207. { // (reg ... // }
  1208. AS_REGISTER:
  1209. begin
  1210. opr.ref.base := findregister(actasmpattern);
  1211. Consume(AS_REGISTER);
  1212. { can either be a register or a right parenthesis }
  1213. { // (reg) // }
  1214. { // (reg)+ // }
  1215. if actasmtoken=AS_RPAREN then
  1216. begin
  1217. Consume(AS_RPAREN);
  1218. if actasmtoken = AS_PLUS then
  1219. begin
  1220. if (opr.ref.direction <> dir_none) then
  1221. Message(asmr_e_no_inc_and_dec_together)
  1222. else
  1223. opr.ref.direction := dir_inc;
  1224. Consume(AS_PLUS);
  1225. end;
  1226. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1227. begin
  1228. Message(asmr_e_invalid_reference_syntax);
  1229. { error recovery ... }
  1230. while actasmtoken <> AS_SEPARATOR do
  1231. Consume(actasmtoken);
  1232. end;
  1233. exit;
  1234. end;
  1235. { // (reg,reg .. // }
  1236. Consume(AS_COMMA);
  1237. if actasmtoken = AS_REGISTER then
  1238. begin
  1239. opr.ref.index :=
  1240. findregister(actasmpattern);
  1241. Consume(AS_REGISTER);
  1242. { check for scaling ... }
  1243. case actasmtoken of
  1244. AS_RPAREN:
  1245. begin
  1246. Consume(AS_RPAREN);
  1247. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1248. begin
  1249. { error recovery ... }
  1250. Message(asmr_e_invalid_reference_syntax);
  1251. while actasmtoken <> AS_SEPARATOR do
  1252. Consume(actasmtoken);
  1253. end;
  1254. exit;
  1255. end;
  1256. AS_STAR:
  1257. begin
  1258. BuildScaling;
  1259. end;
  1260. else
  1261. begin
  1262. Message(asmr_e_invalid_reference_syntax);
  1263. while (actasmtoken <> AS_SEPARATOR) do
  1264. Consume(actasmtoken);
  1265. end;
  1266. end; { end case }
  1267. end
  1268. else
  1269. begin
  1270. Message(asmr_e_invalid_reference_syntax);
  1271. while (actasmtoken <> AS_SEPARATOR) do
  1272. Consume(actasmtoken);
  1273. end;
  1274. end;
  1275. AS_HEXNUM,AS_OCTALNUM, { direct address }
  1276. AS_BINNUM,AS_INTNUM:
  1277. begin
  1278. case actasmtoken of
  1279. AS_INTNUM: str := actasmpattern;
  1280. AS_HEXNUM: str := Tostr(ValHexadecimal(actasmpattern));
  1281. AS_BINNUM: str := Tostr(ValBinary(actasmpattern));
  1282. AS_OCTALNUM: str := Tostr(ValOctal(actasmpattern));
  1283. else
  1284. Message(asmr_e_syntax_error);
  1285. end;
  1286. Consume(actasmtoken);
  1287. val(str, l, code);
  1288. if code <> 0 then
  1289. Message(asmr_e_invalid_reference_syntax)
  1290. else
  1291. opr.ref.offset := l;
  1292. Consume(AS_RPAREN);
  1293. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1294. begin
  1295. { error recovery ... }
  1296. Message(asmr_e_invalid_reference_syntax);
  1297. while actasmtoken <> AS_SEPARATOR do
  1298. Consume(actasmtoken);
  1299. end;
  1300. exit;
  1301. end;
  1302. else
  1303. begin
  1304. Message(asmr_e_invalid_reference_syntax);
  1305. while (actasmtoken <> AS_SEPARATOR) do
  1306. Consume(actasmtoken);
  1307. end;
  1308. end;
  1309. end;
  1310. Procedure TM68kOperand.BuildOperand;
  1311. {*********************************************************************}
  1312. { EXIT CONDITION: On exit the routine should point to either the }
  1313. { AS_COMMA or AS_SEPARATOR token. }
  1314. {*********************************************************************}
  1315. var
  1316. tempstr: string;
  1317. expr: string;
  1318. lab: tasmlabel;
  1319. l : longint;
  1320. i: Tsuperregister;
  1321. r:Tregister;
  1322. hl: tasmlabel;
  1323. reg_one, reg_two: tregister;
  1324. reglist: Tsupregset;
  1325. begin
  1326. reglist := [];
  1327. tempstr := '';
  1328. expr := '';
  1329. case actasmtoken of
  1330. { // Memory reference // }
  1331. AS_LPAREN:
  1332. begin
  1333. InitRef;
  1334. BuildReference;
  1335. end;
  1336. { // Constant expression // }
  1337. AS_APPT: begin
  1338. Consume(AS_APPT);
  1339. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1340. Message(asmr_e_invalid_operand_type);
  1341. { identifiers are handled by BuildExpression }
  1342. opr.typ := OPR_CONSTANT;
  1343. opr.val :=BuildExpression(true,@tempstr);
  1344. if tempstr<>'' then
  1345. begin
  1346. l:=opr.val;
  1347. opr.typ := OPR_SYMBOL;
  1348. opr.symofs := l;
  1349. opr.symbol := objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,AT_FUNCTION);
  1350. end;
  1351. end;
  1352. { // Constant memory offset . // }
  1353. { // This must absolutely be followed by ( // }
  1354. AS_HEXNUM,AS_INTNUM,
  1355. AS_BINNUM,AS_OCTALNUM,AS_PLUS:
  1356. begin
  1357. InitRef;
  1358. opr.ref.offset:=BuildRefExpression;
  1359. BuildReference;
  1360. end;
  1361. { // A constant expression, or a Variable ref. // }
  1362. AS_ID: begin
  1363. InitRef;
  1364. if actasmpattern[1] = '@' then
  1365. { // Label or Special symbol reference // }
  1366. begin
  1367. if actasmpattern = '@RESULT' then
  1368. SetUpResult
  1369. else
  1370. if actasmpattern = 'SELF' then
  1371. SetUpSelf
  1372. else
  1373. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1374. Message(asmr_w_CODE_and_DATA_not_supported)
  1375. else
  1376. begin
  1377. delete(actasmpattern,1,1);
  1378. if actasmpattern = '' then
  1379. Message(asmr_e_null_label_ref_not_allowed);
  1380. CreateLocalLabel(actasmpattern,lab,false);
  1381. opr.typ := OPR_SYMBOL;
  1382. opr.symbol := lab;
  1383. opr.symofs := 0;
  1384. labeled := TRUE;
  1385. end;
  1386. Consume(AS_ID);
  1387. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1388. Message(asmr_e_syntax_error);
  1389. end
  1390. { probably a variable or normal expression }
  1391. { or a procedure (such as in CALL ID) }
  1392. else
  1393. begin
  1394. { is it a constant ? }
  1395. if SearchIConstant(actasmpattern,l) then
  1396. begin
  1397. InitRef;
  1398. opr.ref.offset:=BuildRefExpression;
  1399. BuildReference;
  1400. end
  1401. else { is it a label variable ? }
  1402. begin
  1403. { // ID[ , ID.Field.Field or simple ID // }
  1404. { check if this is a label, if so then }
  1405. { emit it as a label. }
  1406. if SearchLabel(actasmpattern,hl,false) then
  1407. begin
  1408. opr.typ := OPR_SYMBOL;
  1409. opr.symbol := hl;
  1410. opr.symofs := 0;
  1411. labeled := TRUE;
  1412. Consume(AS_ID);
  1413. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1414. Message(asmr_e_syntax_error);
  1415. end
  1416. else
  1417. { is it a normal variable ? }
  1418. if (cs_compilesystem in aktmoduleswitches) then
  1419. begin
  1420. if not SetupDirectVar(expr) then
  1421. begin
  1422. { not found, finally ... add it anyways ... }
  1423. Message1(asmr_w_id_supposed_external,expr);
  1424. opr.ref.symbol:=objectlibrary.newasmsymbol(expr,AB_EXTERNAL,AT_FUNCTION);
  1425. end;
  1426. end
  1427. else
  1428. Message1(sym_e_unknown_id,actasmpattern);
  1429. expr := actasmpattern;
  1430. Consume(AS_ID);
  1431. case actasmtoken of
  1432. AS_LPAREN: { indexing }
  1433. BuildReference;
  1434. AS_SEPARATOR,AS_COMMA: ;
  1435. else
  1436. Message(asmr_e_syntax_error);
  1437. end;
  1438. end;
  1439. end;
  1440. end;
  1441. { // Pre-decrement mode reference or constant mem offset. // }
  1442. AS_MINUS: begin
  1443. Consume(AS_MINUS);
  1444. if actasmtoken = AS_LPAREN then
  1445. begin
  1446. InitRef;
  1447. { indicate pre-decrement mode }
  1448. opr.ref.direction := dir_dec;
  1449. BuildReference;
  1450. end
  1451. else
  1452. if actasmtoken in [AS_OCTALNUM,AS_HEXNUM,AS_BINNUM,AS_INTNUM] then
  1453. begin
  1454. InitRef;
  1455. opr.ref.offset:=BuildRefExpression;
  1456. { negate because was preceded by a negative sign! }
  1457. opr.ref.offset:=-opr.ref.offset;
  1458. BuildReference;
  1459. end
  1460. else
  1461. begin
  1462. Message(asmr_e_syntax_error);
  1463. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1464. Consume(actasmtoken);
  1465. end;
  1466. end;
  1467. { // Register, a variable reference or a constant reference // }
  1468. AS_REGISTER: begin
  1469. { save the type of register used. }
  1470. tempstr := actasmpattern;
  1471. Consume(AS_REGISTER);
  1472. { // Simple register // }
  1473. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  1474. begin
  1475. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1476. Message(asmr_e_invalid_operand_type);
  1477. opr.typ := OPR_REGISTER;
  1478. opr.reg := findregister(tempstr);
  1479. end
  1480. else
  1481. { HERE WE MUST HANDLE THE SPECIAL CASE OF MOVEM AND FMOVEM }
  1482. { // Individual register listing // }
  1483. if (actasmtoken = AS_SLASH) then
  1484. begin
  1485. r:=findregister(tempstr);
  1486. if r.enum<>R_INTREGISTER then
  1487. internalerror(200302191);
  1488. reglist := [r.number shr 8];
  1489. Consume(AS_SLASH);
  1490. if actasmtoken = AS_REGISTER then
  1491. begin
  1492. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1493. begin
  1494. case actasmtoken of
  1495. AS_REGISTER: begin
  1496. r:=findregister(tempstr);
  1497. if r.enum<>R_INTREGISTER then
  1498. internalerror(200302191);
  1499. reglist := reglist + [r.number shr 8];
  1500. Consume(AS_REGISTER);
  1501. end;
  1502. AS_SLASH: Consume(AS_SLASH);
  1503. AS_SEPARATOR,AS_COMMA: break;
  1504. else
  1505. begin
  1506. Message(asmr_e_invalid_reg_list_in_movem);
  1507. Consume(actasmtoken);
  1508. end;
  1509. end; { end case }
  1510. end; { end while }
  1511. opr.typ:= OPR_REGLIST;
  1512. opr.reglist := reglist;
  1513. end
  1514. else
  1515. { error recovery ... }
  1516. begin
  1517. Message(asmr_e_invalid_reg_list_in_movem);
  1518. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1519. Consume(actasmtoken);
  1520. end;
  1521. end
  1522. else
  1523. { // Range register listing // }
  1524. if (actasmtoken = AS_MINUS) then
  1525. begin
  1526. Consume(AS_MINUS);
  1527. reg_one:=findregister(tempstr);
  1528. if actasmtoken <> AS_REGISTER then
  1529. begin
  1530. Message(asmr_e_invalid_reg_list_in_movem);
  1531. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1532. Consume(actasmtoken);
  1533. end
  1534. else
  1535. begin
  1536. { determine the register range ... }
  1537. reg_two:=findregister(actasmpattern);
  1538. if reg_two.enum<>R_INTREGISTER then
  1539. internalerror(200302191);
  1540. if reg_one.enum > reg_two.enum then
  1541. for i:=reg_two.number shr 8 to reg_one.number shr 8 do
  1542. reglist:=reglist+[i]
  1543. else
  1544. for i:=reg_one.number shr 8 to reg_two.number shr 8 do
  1545. reglist:=reglist+[i];
  1546. Consume(AS_REGISTER);
  1547. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1548. begin
  1549. Message(asmr_e_invalid_reg_list_in_movem);
  1550. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1551. Consume(actasmtoken);
  1552. end;
  1553. { set up instruction }
  1554. opr.typ:= OPR_REGLIST;
  1555. opr.reglist := reglist;
  1556. end;
  1557. end
  1558. else
  1559. { DIVSL/DIVS/MULS/MULU with long for MC68020 only }
  1560. if (actasmtoken = AS_COLON) then
  1561. begin
  1562. if (aktoptprocessor = MC68020) or (cs_compilesystem in aktmoduleswitches) then
  1563. begin
  1564. Consume(AS_COLON);
  1565. if (actasmtoken = AS_REGISTER) then
  1566. begin
  1567. { set up old field, since register is valid }
  1568. opr.typ := OPR_REGISTER;
  1569. opr.reg := findregister(tempstr);
  1570. Inc(operandnum);
  1571. opr.typ := OPR_REGISTER;
  1572. opr.reg := findregister(actasmpattern);
  1573. Consume(AS_REGISTER);
  1574. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1575. begin
  1576. Message(asmr_e_invalid_reg_list_for_opcode);
  1577. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1578. Consume(actasmtoken);
  1579. end;
  1580. end;
  1581. end
  1582. else
  1583. begin
  1584. Message1(asmr_e_higher_cpu_mode_required,'68020');
  1585. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1586. begin
  1587. Message(asmr_e_invalid_reg_list_for_opcode);
  1588. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1589. Consume(actasmtoken);
  1590. end;
  1591. end;
  1592. end
  1593. else
  1594. Message(asmr_e_invalid_register);
  1595. end;
  1596. AS_SEPARATOR, AS_COMMA: ;
  1597. else
  1598. begin
  1599. Message(asmr_e_invalid_opcode_and_operand);
  1600. Consume(actasmtoken);
  1601. end;
  1602. end; { end case }
  1603. end;
  1604. Procedure BuildStringConstant(asciiz: boolean);
  1605. {*********************************************************************}
  1606. { PROCEDURE BuildStringConstant }
  1607. { Description: Takes care of a ASCII, or ASCIIZ directive. }
  1608. { asciiz: boolean -> if true then string will be null terminated. }
  1609. {*********************************************************************}
  1610. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1611. { On ENTRY: Token should point to AS_STRING }
  1612. {*********************************************************************}
  1613. var
  1614. expr: string;
  1615. errorflag : boolean;
  1616. begin
  1617. errorflag := FALSE;
  1618. Repeat
  1619. Case actasmtoken of
  1620. AS_STRING: begin
  1621. expr:=actasmpattern;
  1622. if asciiz then
  1623. expr:=expr+#0;
  1624. ConcatPasString(curlist,expr);
  1625. Consume(AS_STRING);
  1626. end;
  1627. AS_COMMA: begin
  1628. Consume(AS_COMMA);
  1629. END;
  1630. AS_SEPARATOR: ;
  1631. else
  1632. begin
  1633. Consume(actasmtoken);
  1634. if not errorflag then
  1635. Message(asmr_e_invalid_string_expression);
  1636. errorflag := TRUE;
  1637. end;
  1638. end; { end case }
  1639. Until actasmtoken = AS_SEPARATOR;
  1640. end;
  1641. {*****************************************************************************
  1642. TM68kInstruction
  1643. *****************************************************************************}
  1644. type
  1645. TM68kInstruction=class(TInstruction)
  1646. procedure InitOperands;override;
  1647. procedure ConcatInstruction(p : taasmoutput);override;
  1648. Procedure ConcatLabeledInstr(p : taasmoutput);
  1649. end;
  1650. procedure TM68kInstruction.InitOperands;
  1651. var
  1652. i : longint;
  1653. begin
  1654. for i:=1 to max_operands do
  1655. Operands[i]:=TM68kOperand.Create;
  1656. end;
  1657. procedure TM68kInstruction.ConcatInstruction(p : taasmoutput);
  1658. var
  1659. fits : boolean;
  1660. begin
  1661. fits := FALSE;
  1662. { setup specific opcodetions for first pass }
  1663. { Setup special operands }
  1664. { Convert to general form as to conform to the m68k opcode table }
  1665. if (opcode = A_ADDA) or (opcode = A_ADDI)
  1666. then opcode := A_ADD
  1667. else
  1668. { CMPM excluded because of GAS v1.34 BUG }
  1669. if (opcode = A_CMPA) or
  1670. (opcode = A_CMPI) then
  1671. opcode := A_CMP
  1672. else
  1673. if opcode = A_EORI then
  1674. opcode := A_EOR
  1675. else
  1676. if opcode = A_MOVEA then
  1677. opcode := A_MOVE
  1678. else
  1679. if opcode = A_ORI then
  1680. opcode := A_OR
  1681. else
  1682. if (opcode = A_SUBA) or (opcode = A_SUBI) then
  1683. opcode := A_SUB;
  1684. { Setup operand types }
  1685. (*
  1686. in opcode <> A_MOVEM then
  1687. begin
  1688. while not(fits) do
  1689. begin
  1690. { set the opcodetion cache, if the opcodetion }
  1691. { occurs the first time }
  1692. if (it[i].i=opcode) and (ins_cache[opcode]=-1) then
  1693. ins_cache[opcode]:=i;
  1694. if (it[i].i=opcode) and (instr.ops=it[i].ops) then
  1695. begin
  1696. { first fit }
  1697. case instr.ops of
  1698. 0 : begin
  1699. fits:=true;
  1700. break;
  1701. end;
  1702. 1 :
  1703. begin
  1704. if (optyp1 and it[i].o1)<>0 then
  1705. begin
  1706. fits:=true;
  1707. break;
  1708. end;
  1709. end;
  1710. 2 : if ((optyp1 and it[i].o1)<>0) and
  1711. ((optyp2 and it[i].o2)<>0) then
  1712. begin
  1713. fits:=true;
  1714. break;
  1715. end
  1716. 3 : if ((optyp1 and it[i].o1)<>0) and
  1717. ((optyp2 and it[i].o2)<>0) and
  1718. ((optyp3 and it[i].o3)<>0) then
  1719. begin
  1720. fits:=true;
  1721. break;
  1722. end;
  1723. end; { end case }
  1724. end; { endif }
  1725. if it[i].i=A_NONE then
  1726. begin
  1727. { NO MATCH! }
  1728. Message(asmr_e_invalid_combination_opcode_and_operand);
  1729. exit;
  1730. end;
  1731. inc(i);
  1732. end; { end while }
  1733. *)
  1734. fits:=TRUE;
  1735. { We add the opcode to the opcode linked list }
  1736. if fits then
  1737. begin
  1738. case ops of
  1739. 0:
  1740. if opsize <> S_NO then
  1741. p.concat((taicpu.op_none(opcode,opsize)))
  1742. else
  1743. p.concat((taicpu.op_none(opcode,S_NO)));
  1744. 1: begin
  1745. case operands[1].opr.typ of
  1746. OPR_SYMBOL:
  1747. begin
  1748. p.concat((taicpu.op_sym_ofs(opcode,
  1749. opsize, operands[1].opr.symbol,operands[1].opr.symofs)));
  1750. end;
  1751. OPR_CONSTANT:
  1752. begin
  1753. p.concat((taicpu.op_const(opcode,
  1754. opsize, operands[1].opr.val)));
  1755. end;
  1756. OPR_REGISTER:
  1757. p.concat((taicpu.op_reg(opcode,opsize,operands[1].opr.reg)));
  1758. OPR_REFERENCE:
  1759. if opsize <> S_NO then
  1760. begin
  1761. p.concat((taicpu.op_ref(opcode,
  1762. opsize,operands[1].opr.ref)));
  1763. end
  1764. else
  1765. begin
  1766. { special jmp and call case with }
  1767. { symbolic references. }
  1768. if opcode in [A_BSR,A_JMP,A_JSR,A_BRA,A_PEA] then
  1769. begin
  1770. p.concat((taicpu.op_ref(opcode,
  1771. S_NO,operands[1].opr.ref)));
  1772. end
  1773. else
  1774. Message(asmr_e_invalid_opcode_and_operand);
  1775. end;
  1776. OPR_NONE:
  1777. Message(asmr_e_invalid_opcode_and_operand);
  1778. else
  1779. begin
  1780. Message(asmr_e_invalid_opcode_and_operand);
  1781. end;
  1782. end;
  1783. end;
  1784. 2: begin
  1785. { source }
  1786. case operands[1].opr.typ of
  1787. { reg,reg }
  1788. { reg,ref }
  1789. OPR_REGISTER:
  1790. begin
  1791. case operands[2].opr.typ of
  1792. OPR_REGISTER:
  1793. begin
  1794. p.concat((taicpu.op_reg_reg(opcode,
  1795. opsize,operands[1].opr.reg,operands[2].opr.reg)));
  1796. end;
  1797. OPR_REFERENCE:
  1798. p.concat((taicpu.op_reg_ref(opcode,
  1799. opsize,operands[1].opr.reg,operands[2].opr.ref)));
  1800. else { else case }
  1801. begin
  1802. Message(asmr_e_invalid_opcode_and_operand);
  1803. end;
  1804. end; { end second operand case for OPR_REGISTER }
  1805. end;
  1806. { reglist, ref }
  1807. OPR_REGLIST:
  1808. begin
  1809. case operands[2].opr.typ of
  1810. OPR_REFERENCE :
  1811. p.concat((taicpu.op_reglist_ref(opcode,
  1812. opsize,operands[1].opr.reglist,operands[2].opr.ref)));
  1813. else
  1814. begin
  1815. Message(asmr_e_invalid_opcode_and_operand);
  1816. end;
  1817. end; { end second operand case for OPR_REGLIST }
  1818. end;
  1819. { const,reg }
  1820. { const,const }
  1821. { const,ref }
  1822. OPR_CONSTANT:
  1823. case operands[2].opr.typ of
  1824. { constant, constant does not have a specific size. }
  1825. OPR_CONSTANT:
  1826. p.concat((taicpu.op_const_const(opcode,
  1827. S_NO,operands[1].opr.val,operands[2].opr.val)));
  1828. OPR_REFERENCE:
  1829. begin
  1830. p.concat((taicpu.op_const_ref(opcode,
  1831. opsize,operands[1].opr.val,
  1832. operands[2].opr.ref)))
  1833. end;
  1834. OPR_REGISTER:
  1835. begin
  1836. p.concat((taicpu.op_const_reg(opcode,
  1837. opsize,operands[1].opr.val,
  1838. operands[2].opr.reg)))
  1839. end;
  1840. else
  1841. begin
  1842. Message(asmr_e_invalid_opcode_and_operand);
  1843. end;
  1844. end; { end second operand case for OPR_CONSTANT }
  1845. { ref,reg }
  1846. { ref,ref }
  1847. OPR_REFERENCE:
  1848. case operands[2].opr.typ of
  1849. OPR_REGISTER:
  1850. begin
  1851. p.concat((taicpu.op_ref_reg(opcode,
  1852. opsize,operands[1].opr.ref,
  1853. operands[2].opr.reg)));
  1854. end;
  1855. OPR_REGLIST:
  1856. begin
  1857. p.concat((taicpu.op_ref_reglist(opcode,
  1858. opsize,operands[1].opr.ref,
  1859. operands[2].opr.reglist)));
  1860. end;
  1861. OPR_REFERENCE: { special opcodes }
  1862. p.concat((taicpu.op_ref_ref(opcode,
  1863. opsize,operands[1].opr.ref,
  1864. operands[2].opr.ref)));
  1865. else
  1866. begin
  1867. Message(asmr_e_invalid_opcode_and_operand);
  1868. end;
  1869. end; { end second operand case for OPR_REFERENCE }
  1870. OPR_SYMBOL: case operands[2].opr.typ of
  1871. OPR_REFERENCE:
  1872. begin
  1873. p.concat((taicpu.op_sym_ofs_ref(opcode,
  1874. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  1875. operands[2].opr.ref)))
  1876. end;
  1877. OPR_REGISTER:
  1878. begin
  1879. p.concat((taicpu.op_sym_ofs_reg(opcode,
  1880. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  1881. operands[2].opr.reg)))
  1882. end;
  1883. else
  1884. begin
  1885. Message(asmr_e_invalid_opcode_and_operand);
  1886. end;
  1887. end; { end second operand case for OPR_SYMBOL }
  1888. else
  1889. begin
  1890. Message(asmr_e_invalid_opcode_and_operand);
  1891. end;
  1892. end; { end first operand case }
  1893. end;
  1894. 3: begin
  1895. if (opcode = A_DIVSL) or (opcode = A_DIVUL) or (opcode = A_MULU)
  1896. or (opcode = A_MULS) or (opcode = A_DIVS) or (opcode = A_DIVU) then
  1897. begin
  1898. if (operands[1].opr.typ <> OPR_REGISTER)
  1899. or (operands[2].opr.typ <> OPR_REGISTER)
  1900. or (operands[3].opr.typ <> OPR_REGISTER) then
  1901. begin
  1902. Message(asmr_e_invalid_opcode_and_operand);
  1903. end
  1904. else
  1905. begin
  1906. p.concat((taicpu. op_reg_reg_reg(opcode,opsize,
  1907. operands[1].opr.reg,operands[2].opr.reg,operands[3].opr.reg)));
  1908. end;
  1909. end
  1910. else
  1911. Message(asmr_e_invalid_opcode_and_operand);
  1912. end;
  1913. end; { end case }
  1914. end;
  1915. end;
  1916. procedure TM68kInstruction.ConcatLabeledInstr(p : taasmoutput);
  1917. begin
  1918. if ((opcode >= A_BCC) and (opcode <= A_BVS)) or
  1919. (opcode = A_BRA) or (opcode = A_BSR) or
  1920. (opcode = A_JMP) or (opcode = A_JSR) or
  1921. ((opcode >= A_FBEQ) and (opcode <= A_FBNGLE)) then
  1922. begin
  1923. if ops > 2 then
  1924. Message(asmr_e_invalid_opcode_and_operand)
  1925. else if operands[1].opr.typ <> OPR_SYMBOL then
  1926. Message(asmr_e_invalid_opcode_and_operand)
  1927. else if (operands[1].opr.typ = OPR_SYMBOL) and
  1928. (ops = 1) then
  1929. if assigned(operands[1].opr.symbol) and
  1930. (operands[1].opr.symofs=0) then
  1931. p.concat(taicpu.op_sym(opcode,S_NO,
  1932. operands[1].opr.symbol))
  1933. else
  1934. Message(asmr_e_invalid_opcode_and_operand);
  1935. end
  1936. else if ((opcode >= A_DBCC) and (opcode <= A_DBF))
  1937. or ((opcode >= A_FDBEQ) and (opcode <= A_FDBNGLE)) then
  1938. begin
  1939. if (ops<>2) or
  1940. (operands[1].opr.typ <> OPR_REGISTER) or
  1941. (operands[2].opr.typ <> OPR_SYMBOL) or
  1942. (operands[2].opr.symofs <> 0) then
  1943. Message(asmr_e_invalid_opcode_and_operand)
  1944. else
  1945. p.concat(taicpu.op_reg_sym(opcode,opsize,operands[1].opr.reg,
  1946. operands[2].opr.symbol));
  1947. end
  1948. else
  1949. Message(asmr_e_invalid_opcode_and_operand);
  1950. end;
  1951. Procedure TM68kReader.BuildOpCode(instr:Tm68kinstruction);
  1952. {*********************************************************************}
  1953. { PROCEDURE BuildOpcode; }
  1954. { Description: Parses the intel opcode and operands, and writes it }
  1955. { in the TInstruction object. }
  1956. {*********************************************************************}
  1957. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1958. { On ENTRY: Token should point to AS_OPCODE }
  1959. {*********************************************************************}
  1960. var asmtok: tasmop;
  1961. expr: string;
  1962. operandnum : longint;
  1963. begin
  1964. expr := '';
  1965. asmtok := A_NONE; { assmume no prefix }
  1966. { // opcode // }
  1967. { allow for newline as in gas styled syntax }
  1968. { under DOS you get two AS_SEPARATOR !! }
  1969. while actasmtoken=AS_SEPARATOR do
  1970. Consume(AS_SEPARATOR);
  1971. if (actasmtoken <> AS_OPCODE) then
  1972. begin
  1973. Message(asmr_e_invalid_or_missing_opcode);
  1974. { error recovery }
  1975. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1976. Consume(actasmtoken);
  1977. exit;
  1978. end
  1979. else
  1980. begin
  1981. Instr.opcode := findopcode(actasmpattern,opsize);
  1982. Consume(AS_OPCODE);
  1983. { // Zero operand opcode ? // }
  1984. if actasmtoken = AS_SEPARATOR then
  1985. exit
  1986. else
  1987. operandnum := 1;
  1988. end;
  1989. While actasmtoken <> AS_SEPARATOR do
  1990. begin
  1991. case actasmtoken of
  1992. { // Operand delimiter // }
  1993. AS_COMMA: begin
  1994. if operandnum > Max_Operands then
  1995. Message(asmr_e_too_many_operands)
  1996. else
  1997. Inc(operandnum);
  1998. Consume(AS_COMMA);
  1999. end;
  2000. { // End of asm operands for this opcode // }
  2001. AS_SEPARATOR: ;
  2002. else
  2003. Instr.Operands[operandnum].BuildOperand;
  2004. end; { end case }
  2005. end; { end while }
  2006. end;
  2007. function tm68kreader.Assemble: tlinkedlist;
  2008. var
  2009. hl: tasmlabel;
  2010. labelptr,nextlabel : tasmlabel;
  2011. commname : string;
  2012. instr : TM68kInstruction;
  2013. begin
  2014. Message(asmr_d_start_reading);
  2015. firsttoken := TRUE;
  2016. operandnum := 0;
  2017. { sets up all opcode and register tables in uppercase }
  2018. if not _asmsorted then
  2019. begin
  2020. SetupTables;
  2021. _asmsorted := TRUE;
  2022. end;
  2023. curlist:=TAAsmoutput.Create;
  2024. { setup label linked list }
  2025. LocalLabelList:=TLocalLabelList.Create;
  2026. c:=current_scanner.asmgetchar;
  2027. actasmtoken:=gettoken;
  2028. while actasmtoken<>AS_END do
  2029. begin
  2030. case actasmtoken of
  2031. AS_LLABEL:
  2032. begin
  2033. if CreateLocalLabel(actasmpattern,hl,true) then
  2034. ConcatLabel(curlist,hl);
  2035. Consume(AS_LLABEL);
  2036. end;
  2037. AS_LABEL:
  2038. begin
  2039. { when looking for Pascal labels, these must }
  2040. { be in uppercase. }
  2041. if SearchLabel(upper(actasmpattern),hl,true) then
  2042. ConcatLabel(curlist,hl)
  2043. else
  2044. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2045. Consume(AS_LABEL);
  2046. end;
  2047. AS_DW:
  2048. begin
  2049. Consume(AS_DW);
  2050. BuildConstant($ffff);
  2051. end;
  2052. AS_DB:
  2053. begin
  2054. Consume(AS_DB);
  2055. BuildConstant($ff);
  2056. end;
  2057. AS_DD:
  2058. begin
  2059. Consume(AS_DD);
  2060. BuildConstant($ffffffff);
  2061. end;
  2062. AS_XDEF:
  2063. begin
  2064. Consume(AS_XDEF);
  2065. if actasmtoken=AS_ID then
  2066. ConcatPublic(curlist,actasmpattern);
  2067. Consume(AS_ID);
  2068. if actasmtoken<>AS_SEPARATOR then
  2069. Consume(AS_SEPARATOR);
  2070. end;
  2071. AS_ALIGN:
  2072. begin
  2073. Message(asmr_w_align_not_supported);
  2074. while actasmtoken <> AS_SEPARATOR do
  2075. Consume(actasmtoken);
  2076. end;
  2077. AS_OPCODE:
  2078. begin
  2079. instr:=TM68kInstruction.Create;
  2080. BuildOpcode(instr);
  2081. { instr.AddReferenceSizes;}
  2082. { instr.SetInstructionOpsize;}
  2083. { instr.CheckOperandSizes;}
  2084. if instr.labeled then
  2085. instr.ConcatLabeledInstr(curlist)
  2086. else
  2087. instr.ConcatInstruction(curlist);
  2088. instr.Free;
  2089. {
  2090. instr.init;
  2091. BuildOpcode;
  2092. instr.ops := operandnum;
  2093. if instr.labeled then
  2094. ConcatLabeledInstr(instr)
  2095. else
  2096. ConcatOpCode(instr);
  2097. instr.done;}
  2098. end;
  2099. AS_SEPARATOR:
  2100. begin
  2101. Consume(AS_SEPARATOR);
  2102. { let us go back to the first operand }
  2103. operandnum := 0;
  2104. end;
  2105. AS_END:
  2106. { end assembly block }
  2107. ;
  2108. else
  2109. begin
  2110. Message(asmr_e_syntax_error);
  2111. { error recovery }
  2112. Consume(actasmtoken);
  2113. end;
  2114. end; { end case }
  2115. end; { end while }
  2116. { Check LocalLabelList }
  2117. LocalLabelList.CheckEmitted;
  2118. LocalLabelList.Free;
  2119. assemble:=curlist;
  2120. Message(asmr_d_finish_reading);
  2121. end;
  2122. {*****************************************************************************
  2123. Initialize
  2124. *****************************************************************************}
  2125. const
  2126. asmmode_m68k_mot_info : tasmmodeinfo =
  2127. (
  2128. id : asmmode_m68k_mot;
  2129. idtxt : 'MOTOROLA';
  2130. casmreader : tm68kmotreader;
  2131. );
  2132. begin
  2133. RegisterAsmMode(asmmode_i386_intel_info);
  2134. end.