ra68kmot.pas 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  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. cutils,
  41. globtype,cclasses,cpubase,
  42. symconst,
  43. aasmbase,
  44. rabase,rasm,ra68k,rautils;
  45. type
  46. tasmtoken = (
  47. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_HEXNUM,AS_OCTALNUM,
  48. AS_BINNUM,AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  49. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,AS_INTNUM,
  50. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_APPT,AS_REALNUM,
  51. AS_ALIGN,
  52. {------------------ Assembler directives --------------------}
  53. AS_DB,AS_DW,AS_DD,AS_XDEF,AS_END,
  54. {------------------ Assembler Operators --------------------}
  55. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR);
  56. tasmkeyword = string[10];
  57. tm68kmotreader = class(tasmreader)
  58. actasmtoken : tasmtoken;
  59. prevasmtoken : tasmtoken;
  60. procedure SetupTables;
  61. function Assemble: tlinkedlist;override;
  62. function is_asmopcode(const s: string) : boolean;
  63. Function is_asmdirective(const s: string):boolean;
  64. function is_register(const s:string):boolean;
  65. procedure GetToken;
  66. function consume(t : tasmtoken):boolean;
  67. function findopcode(s: string; var opsize: topsize): tasmop;
  68. Function BuildExpression(allow_symbol : boolean; asmsym : pshortstring) : longint;
  69. Procedure BuildConstant(maxvalue: longint);
  70. Procedure BuildRealConstant(typ : tfloattype);
  71. Procedure BuildScaling(const oper:tm68koperand);
  72. Function BuildRefExpression: longint;
  73. procedure BuildReference(const oper:tm68koperand);
  74. Procedure BuildOperand(const oper:tm68koperand);
  75. Procedure BuildStringConstant(asciiz: boolean);
  76. Procedure BuildOpCode(instr:Tm68kinstruction);
  77. end;
  78. Implementation
  79. uses
  80. { global }
  81. globals,verbose,
  82. systems,
  83. { aasm }
  84. cpuinfo,aasmtai,aasmdata,aasmcpu,
  85. cgbase,cgutils,
  86. { symtable }
  87. symbase,symtype,symsym,symdef,symtable,
  88. { pass 1 }
  89. nbas,
  90. { parser }
  91. scanner,ag68kgas,
  92. itcpugas
  93. ;
  94. const
  95. firstdirective = AS_DB;
  96. lastdirective = AS_END;
  97. firstoperator = AS_MOD;
  98. lastoperator = AS_XOR;
  99. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  100. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  101. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  102. ('DC.B','DC.W','DC.L','XDEF','END');
  103. { problems with shl,shr,not,and,or and xor, they are }
  104. { context sensitive. }
  105. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  106. 'MOD','SHL','SHR','NOT','AND','OR','XOR');
  107. token2str : array[tasmtoken] of tasmkeyword=(
  108. 'NONE','LABEL','LLABEL','STRING','HEXNUM','OCTALNUM',
  109. 'BINNUM',',','[',']','(',
  110. ')',':','.','+','-','*','INTNUM',
  111. 'SEPARATOR','ID','REGISTER','OPCODE','/','APPT','REALNUM',
  112. 'ALIGN',
  113. {------------------ Assembler directives --------------------}
  114. 'DB','DW','DD','XDEF','END',
  115. {------------------ Assembler Operators --------------------}
  116. 'MOD','SHL','SHR','NOT','AND','OR','XOR');
  117. const
  118. firsttoken : boolean = TRUE;
  119. operandnum : byte = 0;
  120. procedure tm68kmotreader.SetupTables;
  121. { creates uppercased symbol tables for speed access }
  122. var
  123. i : tasmop;
  124. Begin
  125. { opcodes }
  126. iasmops:=TFPHashList.create;
  127. for i:=firstop to lastop do
  128. iasmops.Add(upper(gas_op2str[i]),Pointer(PtrInt(i)));
  129. end;
  130. {---------------------------------------------------------------------}
  131. { Routines for the tokenizing }
  132. {---------------------------------------------------------------------}
  133. function tm68kmotreader.is_asmopcode(const s: string):boolean;
  134. var
  135. hs : string;
  136. j : byte;
  137. begin
  138. is_asmopcode:=false;
  139. { first of all we remove the suffix }
  140. j:=pos('.',s);
  141. if j>0 then
  142. hs:=copy(s,1,j-1)
  143. else
  144. hs:=s;
  145. { Search opcodes }
  146. actopcode:=tasmop(PtrInt(iasmops.Find(hs)));
  147. if actopcode<>A_NONE then
  148. begin
  149. actasmtoken:=AS_OPCODE;
  150. result:=TRUE;
  151. exit;
  152. end;
  153. end;
  154. Function tm68kmotreader.is_asmdirective(const s: string):boolean;
  155. var
  156. i:byte;
  157. begin
  158. result:=false;
  159. for i:=0 to _count_asmdirectives do
  160. begin
  161. if s=_asmdirectives[i] then
  162. begin
  163. actasmtoken := tasmtoken(longint(firstdirective)+i);
  164. result:=true;
  165. exit;
  166. end;
  167. end;
  168. end;
  169. function tm68kmotreader.is_register(const s:string):boolean;
  170. begin
  171. is_register:=false;
  172. // FIX ME!!! Ugly, needs a proper fix (KB)
  173. actasmregister:=gas_regnum_search('%'+lower(s));
  174. if actasmregister<>NR_NO then
  175. begin
  176. is_register:=true;
  177. actasmtoken:=AS_REGISTER;
  178. end;
  179. end;
  180. Procedure tm68kmotreader.GetToken;
  181. {*********************************************************************}
  182. { FUNCTION GetToken: tinteltoken; }
  183. { Description: This routine returns intel assembler tokens and }
  184. { does some minor syntax error checking. }
  185. {*********************************************************************}
  186. var
  187. token: tasmtoken;
  188. forcelabel: boolean;
  189. s : string;
  190. begin
  191. forcelabel := FALSE;
  192. actasmpattern :='';
  193. {* INIT TOKEN TO NOTHING *}
  194. token := AS_NONE;
  195. { while space and tab , continue scan... }
  196. while c in [' ',#9] do
  197. c:=current_scanner.asmgetchar;
  198. if not (c in [#10,#13,'{',';']) then
  199. current_scanner.gettokenpos;
  200. { Possiblities for first token in a statement: }
  201. { Local Label, Label, Directive, Prefix or Opcode.... }
  202. if firsttoken and not (c in [#10,#13,'{',';']) then
  203. begin
  204. firsttoken := FALSE;
  205. if c = '@' then
  206. begin
  207. token := AS_LLABEL; { this is a local label }
  208. { Let us point to the next character }
  209. c := current_scanner.asmgetchar;
  210. end;
  211. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  212. begin
  213. { if there is an at_sign, then this must absolutely be a label }
  214. if c = '@' then forcelabel:=TRUE;
  215. actasmpattern := actasmpattern + c;
  216. c := current_scanner.asmgetchar;
  217. end;
  218. uppervar(actasmpattern);
  219. if c = ':' then
  220. begin
  221. case token of
  222. AS_NONE: token := AS_LABEL;
  223. AS_LLABEL: ; { do nothing }
  224. end; { end case }
  225. { let us point to the next character }
  226. c := current_scanner.asmgetchar;
  227. actasmtoken := token;
  228. exit;
  229. end;
  230. { Are we trying to create an identifier with }
  231. { an at-sign...? }
  232. if forcelabel then
  233. Message(asmr_e_none_label_contain_at);
  234. If is_asmopcode(actasmpattern) then
  235. exit;
  236. if is_asmdirective(actasmpattern) then
  237. exit
  238. else
  239. begin
  240. actasmtoken := AS_NONE;
  241. Message1(asmr_e_invalid_or_missing_opcode,actasmpattern);
  242. end;
  243. end
  244. else { else firsttoken }
  245. { Here we must handle all possible cases }
  246. begin
  247. case c of
  248. '@': { possiblities : - local label reference , such as in jmp @local1 }
  249. { - @Result, @Code or @Data special variables. }
  250. begin
  251. actasmpattern := c;
  252. c:= current_scanner.asmgetchar;
  253. while c in ['A'..'Z','a'..'z','0'..'9','_','@','.'] do
  254. begin
  255. actasmpattern := actasmpattern + c;
  256. c := current_scanner.asmgetchar;
  257. end;
  258. uppervar(actasmpattern);
  259. actasmtoken := AS_ID;
  260. exit;
  261. end;
  262. { identifier, register, opcode, prefix or directive }
  263. 'A'..'Z','a'..'z','_': begin
  264. actasmpattern := c;
  265. c:= current_scanner.asmgetchar;
  266. while c in ['A'..'Z','a'..'z','0'..'9','_','.'] do
  267. begin
  268. actasmpattern := actasmpattern + c;
  269. c := current_scanner.asmgetchar;
  270. end;
  271. uppervar(actasmpattern);
  272. { this isn't the first token, so it can't be an
  273. opcode }
  274. {If is_asmopcode(actasmpattern) then
  275. exit;}
  276. if is_register(actasmpattern) then
  277. exit;
  278. if is_asmdirective(actasmpattern) then
  279. exit;
  280. { this is surely an identifier }
  281. actasmtoken := AS_ID;
  282. exit;
  283. end;
  284. { override operator... not supported }
  285. '&': begin
  286. c:=current_scanner.asmgetchar;
  287. actasmtoken := AS_AND;
  288. end;
  289. { string or character }
  290. '''' :
  291. begin
  292. actasmpattern:='';
  293. while true do
  294. begin
  295. if c = '''' then
  296. begin
  297. c:=current_scanner.asmgetchar;
  298. if c=#10 then
  299. begin
  300. Message(scan_f_string_exceeds_line);
  301. break;
  302. end;
  303. repeat
  304. if c=''''then
  305. begin
  306. c:=current_scanner.asmgetchar;
  307. if c='''' then
  308. begin
  309. actasmpattern:=actasmpattern+'''';
  310. c:=current_scanner.asmgetchar;
  311. if c=#10 then
  312. begin
  313. Message(scan_f_string_exceeds_line);
  314. break;
  315. end;
  316. end
  317. else break;
  318. end
  319. else
  320. begin
  321. actasmpattern:=actasmpattern+c;
  322. c:=current_scanner.asmgetchar;
  323. if c=#10 then
  324. begin
  325. Message(scan_f_string_exceeds_line);
  326. break
  327. end;
  328. end;
  329. until false; { end repeat }
  330. end
  331. else break; { end if }
  332. end; { end while }
  333. token:=AS_STRING;
  334. actasmtoken := token;
  335. exit;
  336. end;
  337. '$' : begin
  338. c:=current_scanner.asmgetchar;
  339. while c in ['0'..'9','A'..'F','a'..'f'] do
  340. begin
  341. actasmpattern := actasmpattern + c;
  342. c := current_scanner.asmgetchar;
  343. end;
  344. actasmtoken := AS_HEXNUM;
  345. exit;
  346. end;
  347. ',' : begin
  348. actasmtoken := AS_COMMA;
  349. c:=current_scanner.asmgetchar;
  350. exit;
  351. end;
  352. '(' : begin
  353. actasmtoken := AS_LPAREN;
  354. c:=current_scanner.asmgetchar;
  355. exit;
  356. end;
  357. ')' : begin
  358. actasmtoken := AS_RPAREN;
  359. c:=current_scanner.asmgetchar;
  360. exit;
  361. end;
  362. ':' : begin
  363. actasmtoken := AS_COLON;
  364. c:=current_scanner.asmgetchar;
  365. exit;
  366. end;
  367. { '.' : begin
  368. actasmtoken := AS_DOT;
  369. c:=current_scanner.asmgetchar;
  370. exit;
  371. end; }
  372. '+' : begin
  373. actasmtoken := AS_PLUS;
  374. c:=current_scanner.asmgetchar;
  375. exit;
  376. end;
  377. '-' : begin
  378. actasmtoken := AS_MINUS;
  379. c:=current_scanner.asmgetchar;
  380. exit;
  381. end;
  382. '*' : begin
  383. actasmtoken := AS_STAR;
  384. c:=current_scanner.asmgetchar;
  385. exit;
  386. end;
  387. '/' : begin
  388. actasmtoken := AS_SLASH;
  389. c:=current_scanner.asmgetchar;
  390. exit;
  391. end;
  392. '<' : begin
  393. c := current_scanner.asmgetchar;
  394. { invalid characters }
  395. if c <> '<' then
  396. Message(asmr_e_invalid_char_smaller);
  397. { still assume << }
  398. actasmtoken := AS_SHL;
  399. c := current_scanner.asmgetchar;
  400. exit;
  401. end;
  402. '>' : begin
  403. c := current_scanner.asmgetchar;
  404. { invalid characters }
  405. if c <> '>' then
  406. Message(asmr_e_invalid_char_greater);
  407. { still assume << }
  408. actasmtoken := AS_SHR;
  409. c := current_scanner.asmgetchar;
  410. exit;
  411. end;
  412. '|' : begin
  413. actasmtoken := AS_OR;
  414. c := current_scanner.asmgetchar;
  415. exit;
  416. end;
  417. '^' : begin
  418. actasmtoken := AS_XOR;
  419. c := current_scanner.asmgetchar;
  420. exit;
  421. end;
  422. '#' : begin
  423. actasmtoken:=AS_APPT;
  424. c:=current_scanner.asmgetchar;
  425. exit;
  426. end;
  427. '%' : begin
  428. c:=current_scanner.asmgetchar;
  429. while c in ['0','1'] do
  430. begin
  431. actasmpattern := actasmpattern + c;
  432. c := current_scanner.asmgetchar;
  433. end;
  434. actasmtoken := AS_BINNUM;
  435. exit;
  436. end;
  437. { integer number }
  438. '0'..'9': begin
  439. actasmpattern := c;
  440. c := current_scanner.asmgetchar;
  441. while c in ['0'..'9'] do
  442. begin
  443. actasmpattern := actasmpattern + c;
  444. c:= current_scanner.asmgetchar;
  445. end;
  446. actasmtoken := AS_INTNUM;
  447. exit;
  448. end;
  449. ';' : begin
  450. repeat
  451. c:=current_scanner.asmgetchar;
  452. until c=#10;
  453. firsttoken := TRUE;
  454. actasmtoken:=AS_SEPARATOR;
  455. end;
  456. '{',#13,#10 : begin
  457. c:=current_scanner.asmgetchar;
  458. firsttoken := TRUE;
  459. actasmtoken:=AS_SEPARATOR;
  460. end;
  461. else
  462. begin
  463. s:=c;
  464. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  465. end;
  466. end; { end case }
  467. end; { end else if }
  468. end;
  469. {---------------------------------------------------------------------}
  470. { Routines for the parsing }
  471. {---------------------------------------------------------------------}
  472. function tm68kmotreader.consume(t : tasmtoken):boolean;
  473. var
  474. p: pointer;
  475. begin
  476. Consume:=true;
  477. if t<>actasmtoken then
  478. begin
  479. p:=nil;
  480. dword(p^):=0;
  481. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  482. Consume:=false;
  483. end;
  484. repeat
  485. gettoken;
  486. until actasmtoken<>AS_NONE;
  487. end;
  488. function tm68kmotreader.findopcode(s: string; var opsize: topsize): tasmop;
  489. {*********************************************************************}
  490. { FUNCTION findopcode(s: string): tasmop; }
  491. { Description: Determines if the s string is a valid opcode }
  492. { if so returns correct tasmop token. }
  493. {*********************************************************************}
  494. var
  495. j: byte;
  496. op_size: string;
  497. begin
  498. findopcode := A_NONE;
  499. j:=pos('.',s);
  500. if j<>0 then
  501. begin
  502. op_size:=copy(s,j+1,1);
  503. case op_size[1] of
  504. { For the motorola only opsize size is used to }
  505. { determine the size of the operands. }
  506. 'B': opsize := S_B;
  507. 'W': opsize := S_W;
  508. 'L': opsize := S_L;
  509. 'S': opsize := S_FS;
  510. 'D': opsize := S_FD;
  511. 'X': opsize := S_FX;
  512. else
  513. Message1(asmr_e_unknown_opcode,s);
  514. end;
  515. { delete everything starting from dot }
  516. delete(s,j,length(s));
  517. end;
  518. result:=actopcode;
  519. end;
  520. Function tm68kmotreader.BuildExpression(allow_symbol : boolean; asmsym : pshortstring) : longint;
  521. {*********************************************************************}
  522. { FUNCTION BuildExpression: longint }
  523. { Description: This routine calculates a constant expression to }
  524. { a given value. The return value is the value calculated from }
  525. { the expression. }
  526. { The following tokens (not strings) are recognized: }
  527. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  528. {*********************************************************************}
  529. { ENTRY: On entry the token should be any valid expression token. }
  530. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  531. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  532. { invalid tokens. }
  533. {*********************************************************************}
  534. var expr: string;
  535. hs, tempstr: string;
  536. sym : tsym;
  537. srsymtable : TSymtable;
  538. hl : tasmlabel;
  539. l : longint;
  540. errorflag: boolean;
  541. begin
  542. errorflag := FALSE;
  543. expr := '';
  544. tempstr := '';
  545. if allow_symbol then
  546. asmsym^:='';
  547. Repeat
  548. Case actasmtoken of
  549. AS_LPAREN: begin
  550. Consume(AS_LPAREN);
  551. expr := expr + '(';
  552. end;
  553. AS_RPAREN: begin
  554. Consume(AS_RPAREN);
  555. expr := expr + ')';
  556. end;
  557. AS_SHL: begin
  558. Consume(AS_SHL);
  559. expr := expr + '<';
  560. end;
  561. AS_SHR: begin
  562. Consume(AS_SHR);
  563. expr := expr + '>';
  564. end;
  565. AS_SLASH: begin
  566. Consume(AS_SLASH);
  567. expr := expr + '/';
  568. end;
  569. AS_MOD: begin
  570. Consume(AS_MOD);
  571. expr := expr + '%';
  572. end;
  573. AS_STAR: begin
  574. Consume(AS_STAR);
  575. expr := expr + '*';
  576. end;
  577. AS_PLUS: begin
  578. Consume(AS_PLUS);
  579. expr := expr + '+';
  580. end;
  581. AS_MINUS: begin
  582. Consume(AS_MINUS);
  583. expr := expr + '-';
  584. end;
  585. AS_AND: begin
  586. Consume(AS_AND);
  587. expr := expr + '&';
  588. end;
  589. AS_NOT: begin
  590. Consume(AS_NOT);
  591. expr := expr + '~';
  592. end;
  593. AS_XOR: begin
  594. Consume(AS_XOR);
  595. expr := expr + '^';
  596. end;
  597. AS_OR: begin
  598. Consume(AS_OR);
  599. expr := expr + '|';
  600. end;
  601. AS_ID: begin
  602. if SearchIConstant(actasmpattern,l) then
  603. begin
  604. str(l, tempstr);
  605. expr := expr + tempstr;
  606. Consume(AS_ID);
  607. End else
  608. if not allow_symbol then
  609. begin
  610. Message(asmr_e_syn_constant);
  611. l := 0;
  612. End else
  613. begin
  614. hs:='';
  615. if (expr[Length(expr)]='+') then
  616. Delete(expr,Length(expr),1)
  617. else if expr<>'' then
  618. begin
  619. Message(asmr_e_invalid_constant_expression);
  620. break;
  621. End;
  622. tempstr:=actasmpattern;
  623. consume(AS_ID);
  624. if (length(tempstr)>1) and (tempstr[1]='@') then
  625. begin
  626. CreateLocalLabel(tempstr,hl,false);
  627. hs:=hl.name
  628. end
  629. else if SearchLabel(tempstr,hl,false) then
  630. hs:=hl.name
  631. else
  632. begin
  633. asmsearchsym(tempstr,sym,srsymtable);
  634. if assigned(sym) then
  635. begin
  636. case sym.typ of
  637. paravarsym,
  638. localvarsym :
  639. begin
  640. Message(asmr_e_no_local_or_para_allowed);
  641. hs:=tabstractvarsym(sym).mangledname;
  642. end;
  643. staticvarsym :
  644. hs:=tstaticvarsym(sym).mangledname;
  645. procsym :
  646. begin
  647. if tprocsym(sym).procdeflist.count>1 then
  648. Message(asmr_w_calling_overload_func);
  649. hs:=tprocdef(tprocsym(sym).procdeflist[0]).mangledname;
  650. end;
  651. typesym :
  652. begin
  653. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  654. Message(asmr_e_wrong_sym_type);
  655. end;
  656. else
  657. Message(asmr_e_wrong_sym_type);
  658. end;
  659. end
  660. else
  661. Message1(sym_e_unknown_id,tempstr);
  662. end;
  663. { symbol found? }
  664. if hs<>'' then
  665. begin
  666. if asmsym^='' then
  667. asmsym^:=hs
  668. else
  669. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  670. end;
  671. end;
  672. end;
  673. AS_INTNUM: begin
  674. expr := expr + actasmpattern;
  675. Consume(AS_INTNUM);
  676. end;
  677. AS_BINNUM: begin
  678. tempstr := tostr(ParseVal(actasmpattern,2));
  679. if tempstr = '' then
  680. Message(asmr_e_error_converting_binary);
  681. expr:=expr+tempstr;
  682. Consume(AS_BINNUM);
  683. end;
  684. AS_HEXNUM: begin
  685. tempstr := tostr(ParseVal(actasmpattern,16));
  686. if tempstr = '' then
  687. Message(asmr_e_error_converting_hexadecimal);
  688. expr:=expr+tempstr;
  689. Consume(AS_HEXNUM);
  690. end;
  691. AS_OCTALNUM: begin
  692. tempstr := tostr(ParseVal(actasmpattern,8));
  693. if tempstr = '' then
  694. Message(asmr_e_error_converting_octal);
  695. expr:=expr+tempstr;
  696. Consume(AS_OCTALNUM);
  697. end;
  698. { go to next term }
  699. AS_COMMA: begin
  700. if not ErrorFlag then
  701. BuildExpression := CalculateExpression(expr)
  702. else
  703. BuildExpression := 0;
  704. Exit;
  705. end;
  706. { go to next symbol }
  707. AS_SEPARATOR: begin
  708. if not ErrorFlag then
  709. BuildExpression := CalculateExpression(expr)
  710. else
  711. BuildExpression := 0;
  712. Exit;
  713. end;
  714. else
  715. begin
  716. { only write error once. }
  717. if not errorflag then
  718. Message(asmr_e_invalid_constant_expression);
  719. { consume tokens until we find COMMA or SEPARATOR }
  720. Consume(actasmtoken);
  721. errorflag := TRUE;
  722. End;
  723. end;
  724. Until false;
  725. end;
  726. Procedure tm68kmotreader.BuildRealConstant(typ : tfloattype);
  727. {*********************************************************************}
  728. { PROCEDURE BuilRealConst }
  729. { Description: This routine calculates a constant expression to }
  730. { a given value. The return value is the value calculated from }
  731. { the expression. }
  732. { The following tokens (not strings) are recognized: }
  733. { +/-,numbers and real numbers }
  734. {*********************************************************************}
  735. { ENTRY: On entry the token should be any valid expression token. }
  736. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  737. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  738. { invalid tokens. }
  739. {*********************************************************************}
  740. var expr: string;
  741. r : extended;
  742. code : word;
  743. negativ : boolean;
  744. errorflag: boolean;
  745. begin
  746. errorflag := FALSE;
  747. Repeat
  748. negativ:=false;
  749. expr := '';
  750. if actasmtoken=AS_PLUS then Consume(AS_PLUS)
  751. else if actasmtoken=AS_MINUS then
  752. begin
  753. negativ:=true;
  754. consume(AS_MINUS);
  755. end;
  756. Case actasmtoken of
  757. AS_INTNUM: begin
  758. expr := actasmpattern;
  759. Consume(AS_INTNUM);
  760. end;
  761. AS_REALNUM: begin
  762. expr := actasmpattern;
  763. { in ATT syntax you have 0d in front of the real }
  764. { should this be forced ? yes i think so, as to }
  765. { conform to gas as much as possible. }
  766. if (expr[1]='0') and (upper(expr[2])='D') then
  767. expr:=copy(expr,3,255);
  768. Consume(AS_REALNUM);
  769. end;
  770. AS_BINNUM: begin
  771. { checking for real constants with this should use }
  772. { real DECODING otherwise the compiler will crash! }
  773. Message(asmr_e_invalid_float_expr);
  774. expr:='0.0';
  775. Consume(AS_BINNUM);
  776. end;
  777. AS_HEXNUM: begin
  778. { checking for real constants with this should use }
  779. { real DECODING otherwise the compiler will crash! }
  780. Message(asmr_e_invalid_float_expr);
  781. expr:='0.0';
  782. Consume(AS_HEXNUM);
  783. end;
  784. AS_OCTALNUM: begin
  785. { checking for real constants with this should use }
  786. { real DECODING otherwise the compiler will crash! }
  787. { xxxToDec using reals could be a solution, but the }
  788. { problem is that these will crash the m68k compiler }
  789. { when compiling -- because of lack of good fpu }
  790. { support. }
  791. Message(asmr_e_invalid_float_expr);
  792. expr:='0.0';
  793. Consume(AS_OCTALNUM);
  794. end;
  795. else
  796. begin
  797. { only write error once. }
  798. if not errorflag then
  799. Message(asmr_e_invalid_float_expr);
  800. { consume tokens until we find COMMA or SEPARATOR }
  801. Consume(actasmtoken);
  802. errorflag := TRUE;
  803. End;
  804. end;
  805. { go to next term }
  806. if (actasmtoken=AS_COMMA) or (actasmtoken=AS_SEPARATOR) then
  807. begin
  808. if negativ then expr:='-'+expr;
  809. val(expr,r,code);
  810. if code<>0 then
  811. begin
  812. r:=0;
  813. Message(asmr_e_invalid_float_expr);
  814. ConcatRealConstant(curlist,r,typ);
  815. End
  816. else
  817. begin
  818. ConcatRealConstant(curlist,r,typ);
  819. End;
  820. end
  821. else
  822. Message(asmr_e_invalid_float_expr);
  823. Until actasmtoken=AS_SEPARATOR;
  824. end;
  825. Procedure tm68kmotreader.BuildConstant(maxvalue: longint);
  826. {*********************************************************************}
  827. { PROCEDURE BuildConstant }
  828. { Description: This routine takes care of parsing a DB,DD,or DW }
  829. { line and adding those to the assembler node. Expressions, range- }
  830. { checking are fullly taken care of. }
  831. { maxvalue: $ff -> indicates that this is a DB node. }
  832. { $ffff -> indicates that this is a DW node. }
  833. { $ffffffff -> indicates that this is a DD node. }
  834. {*********************************************************************}
  835. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  836. {*********************************************************************}
  837. var
  838. expr: string;
  839. value : longint;
  840. begin
  841. Repeat
  842. Case actasmtoken of
  843. AS_STRING: begin
  844. if maxvalue <> $ff then
  845. Message(asmr_e_string_not_allowed_as_const);
  846. expr := actasmpattern;
  847. if length(expr) > 1 then
  848. Message(asmr_e_string_not_allowed_as_const);
  849. Consume(AS_STRING);
  850. Case actasmtoken of
  851. AS_COMMA: Consume(AS_COMMA);
  852. AS_SEPARATOR: ;
  853. else
  854. Message(asmr_e_invalid_string_expression);
  855. end; { end case }
  856. ConcatString(curlist,expr);
  857. end;
  858. AS_INTNUM,AS_BINNUM,
  859. AS_OCTALNUM,AS_HEXNUM:
  860. begin
  861. value:=BuildExpression(false,nil);
  862. ConcatConstant(curlist,value,maxvalue);
  863. end;
  864. AS_ID:
  865. begin
  866. value:=BuildExpression(false,nil);
  867. if value > maxvalue then
  868. begin
  869. Message(asmr_e_constant_out_of_bounds);
  870. { assuming a value of maxvalue }
  871. value := maxvalue;
  872. end;
  873. ConcatConstant(curlist,value,maxvalue);
  874. end;
  875. { These terms can start an assembler expression }
  876. AS_PLUS,AS_MINUS,AS_LPAREN,AS_NOT: begin
  877. value := BuildExpression(false,nil);
  878. ConcatConstant(curlist,value,maxvalue);
  879. end;
  880. AS_COMMA: begin
  881. Consume(AS_COMMA);
  882. END;
  883. AS_SEPARATOR: ;
  884. else
  885. begin
  886. Message(asmr_e_syntax_error);
  887. end;
  888. end; { end case }
  889. Until actasmtoken = AS_SEPARATOR;
  890. end;
  891. Procedure TM68kMotreader.BuildScaling(const oper:tm68koperand);
  892. {*********************************************************************}
  893. { Takes care of parsing expression starting from the scaling value }
  894. { up to and including possible field specifiers. }
  895. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR }
  896. { or AS_COMMA. On entry should point to the AS_STAR token. }
  897. {*********************************************************************}
  898. var str:string;
  899. l: longint;
  900. code: integer;
  901. begin
  902. Consume(AS_STAR);
  903. if (oper.opr.ref.scalefactor <> 0)
  904. and (oper.opr.ref.scalefactor <> 1) then
  905. Message(asmr_e_wrong_base_index);
  906. case actasmtoken of
  907. AS_INTNUM: str := actasmpattern;
  908. AS_HEXNUM: str := Tostr(ParseVal(actasmpattern,16));
  909. AS_BINNUM: str := Tostr(ParseVal(actasmpattern,2));
  910. AS_OCTALNUM: str := Tostr(ParseVal(actasmpattern,8));
  911. else
  912. Message(asmr_e_syntax_error);
  913. end;
  914. val(str, l, code);
  915. if code <> 0 then
  916. Message(asmr_e_wrong_scale_factor);
  917. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) and (code = 0) then
  918. begin
  919. oper.opr.ref.scalefactor := l;
  920. end
  921. else
  922. begin
  923. Message(asmr_e_wrong_scale_factor);
  924. oper.opr.ref.scalefactor := 0;
  925. end;
  926. if oper.opr.ref.index = NR_NO then
  927. begin
  928. Message(asmr_e_wrong_base_index);
  929. oper.opr.ref.scalefactor := 0;
  930. end;
  931. { Consume the scaling number }
  932. Consume(actasmtoken);
  933. if actasmtoken = AS_RPAREN then
  934. Consume(AS_RPAREN)
  935. else
  936. Message(asmr_e_wrong_scale_factor);
  937. { // .Field.Field ... or separator/comma // }
  938. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then
  939. begin
  940. end
  941. else
  942. Message(asmr_e_syntax_error);
  943. end;
  944. Function TM68kMotreader.BuildRefExpression: longint;
  945. {*********************************************************************}
  946. { FUNCTION BuildRefExpression: longint }
  947. { Description: This routine calculates a constant expression to }
  948. { a given value. The return value is the value calculated from }
  949. { the expression. }
  950. { The following tokens (not strings) are recognized: }
  951. { SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  952. {*********************************************************************}
  953. { ENTRY: On entry the token should be any valid expression token. }
  954. { EXIT: On Exit the token points to the LPAREN token. }
  955. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  956. { invalid tokens. }
  957. {*********************************************************************}
  958. var tempstr: string;
  959. expr: string;
  960. l : longint;
  961. errorflag : boolean;
  962. begin
  963. errorflag := FALSE;
  964. tempstr := '';
  965. expr := '';
  966. Repeat
  967. Case actasmtoken of
  968. AS_RPAREN: begin
  969. Message(asmr_e_syntax_error);
  970. Consume(AS_RPAREN);
  971. end;
  972. AS_SHL: begin
  973. Consume(AS_SHL);
  974. expr := expr + '<';
  975. end;
  976. AS_SHR: begin
  977. Consume(AS_SHR);
  978. expr := expr + '>';
  979. end;
  980. AS_SLASH: begin
  981. Consume(AS_SLASH);
  982. expr := expr + '/';
  983. end;
  984. AS_MOD: begin
  985. Consume(AS_MOD);
  986. expr := expr + '%';
  987. end;
  988. AS_STAR: begin
  989. Consume(AS_STAR);
  990. expr := expr + '*';
  991. end;
  992. AS_PLUS: begin
  993. Consume(AS_PLUS);
  994. expr := expr + '+';
  995. end;
  996. AS_MINUS: begin
  997. Consume(AS_MINUS);
  998. expr := expr + '-';
  999. end;
  1000. AS_AND: begin
  1001. Consume(AS_AND);
  1002. expr := expr + '&';
  1003. end;
  1004. AS_NOT: begin
  1005. Consume(AS_NOT);
  1006. expr := expr + '~';
  1007. end;
  1008. AS_XOR: begin
  1009. Consume(AS_XOR);
  1010. expr := expr + '^';
  1011. end;
  1012. AS_OR: begin
  1013. Consume(AS_OR);
  1014. expr := expr + '|';
  1015. end;
  1016. { End of reference }
  1017. AS_LPAREN: begin
  1018. if not ErrorFlag then
  1019. BuildRefExpression := CalculateExpression(expr)
  1020. else
  1021. BuildRefExpression := 0;
  1022. { no longer in an expression }
  1023. exit;
  1024. end;
  1025. AS_ID:
  1026. begin
  1027. if NOT SearchIConstant(actasmpattern,l) then
  1028. begin
  1029. Message(asmr_e_syn_constant);
  1030. l := 0;
  1031. end;
  1032. str(l, tempstr);
  1033. expr := expr + tempstr;
  1034. Consume(AS_ID);
  1035. end;
  1036. AS_INTNUM: begin
  1037. expr := expr + actasmpattern;
  1038. Consume(AS_INTNUM);
  1039. end;
  1040. AS_BINNUM: begin
  1041. tempstr := Tostr(ParseVal(actasmpattern,2));
  1042. if tempstr = '' then
  1043. Message(asmr_e_error_converting_binary);
  1044. expr:=expr+tempstr;
  1045. Consume(AS_BINNUM);
  1046. end;
  1047. AS_HEXNUM: begin
  1048. tempstr := Tostr(ParseVal(actasmpattern,16));
  1049. if tempstr = '' then
  1050. Message(asmr_e_error_converting_hexadecimal);
  1051. expr:=expr+tempstr;
  1052. Consume(AS_HEXNUM);
  1053. end;
  1054. AS_OCTALNUM: begin
  1055. tempstr := Tostr(ParseVal(actasmpattern,8));
  1056. if tempstr = '' then
  1057. Message(asmr_e_error_converting_octal);
  1058. expr:=expr+tempstr;
  1059. Consume(AS_OCTALNUM);
  1060. end;
  1061. else
  1062. begin
  1063. { write error only once. }
  1064. if not errorflag then
  1065. Message(asmr_e_invalid_constant_expression);
  1066. BuildRefExpression := 0;
  1067. if actasmtoken in [AS_COMMA,AS_SEPARATOR] then exit;
  1068. { consume tokens until we find COMMA or SEPARATOR }
  1069. Consume(actasmtoken);
  1070. errorflag := TRUE;
  1071. end;
  1072. end;
  1073. Until false;
  1074. end;
  1075. {*********************************************************************}
  1076. { PROCEDURE BuildBracketExpression }
  1077. { Description: This routine builds up an expression after a LPAREN }
  1078. { token is encountered. }
  1079. { On entry actasmtoken should be equal to AS_LPAREN }
  1080. {*********************************************************************}
  1081. { EXIT CONDITION: On exit the routine should point to either the }
  1082. { AS_COMMA or AS_SEPARATOR token. }
  1083. {*********************************************************************}
  1084. procedure TM68kMotreader.BuildReference(const oper:tm68koperand);
  1085. var
  1086. l:longint;
  1087. code: integer;
  1088. str: string;
  1089. begin
  1090. Consume(AS_LPAREN);
  1091. case actasmtoken of
  1092. { // (reg ... // }
  1093. AS_REGISTER:
  1094. begin
  1095. oper.opr.ref.base := actasmregister;
  1096. Consume(AS_REGISTER);
  1097. { can either be a register or a right parenthesis }
  1098. { // (reg) // }
  1099. { // (reg)+ // }
  1100. if actasmtoken=AS_RPAREN then
  1101. begin
  1102. Consume(AS_RPAREN);
  1103. if actasmtoken = AS_PLUS then
  1104. begin
  1105. if (oper.opr.ref.direction <> dir_none) then
  1106. Message(asmr_e_no_inc_and_dec_together)
  1107. else
  1108. oper.opr.ref.direction := dir_inc;
  1109. Consume(AS_PLUS);
  1110. end;
  1111. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1112. begin
  1113. Message(asmr_e_invalid_reference_syntax);
  1114. { error recovery ... }
  1115. while actasmtoken <> AS_SEPARATOR do
  1116. Consume(actasmtoken);
  1117. end;
  1118. exit;
  1119. end;
  1120. { // (reg,reg .. // }
  1121. Consume(AS_COMMA);
  1122. if actasmtoken = AS_REGISTER then
  1123. begin
  1124. oper.opr.ref.index :=
  1125. actasmregister;
  1126. Consume(AS_REGISTER);
  1127. { check for scaling ... }
  1128. case actasmtoken of
  1129. AS_RPAREN:
  1130. begin
  1131. Consume(AS_RPAREN);
  1132. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1133. begin
  1134. { error recovery ... }
  1135. Message(asmr_e_invalid_reference_syntax);
  1136. while actasmtoken <> AS_SEPARATOR do
  1137. Consume(actasmtoken);
  1138. end;
  1139. exit;
  1140. end;
  1141. AS_STAR:
  1142. begin
  1143. BuildScaling(oper);
  1144. end;
  1145. else
  1146. begin
  1147. Message(asmr_e_invalid_reference_syntax);
  1148. while (actasmtoken <> AS_SEPARATOR) do
  1149. Consume(actasmtoken);
  1150. end;
  1151. end; { end case }
  1152. end
  1153. else
  1154. begin
  1155. Message(asmr_e_invalid_reference_syntax);
  1156. while (actasmtoken <> AS_SEPARATOR) do
  1157. Consume(actasmtoken);
  1158. end;
  1159. end;
  1160. AS_HEXNUM,AS_OCTALNUM, { direct address }
  1161. AS_BINNUM,AS_INTNUM:
  1162. begin
  1163. case actasmtoken of
  1164. AS_INTNUM: str := actasmpattern;
  1165. AS_HEXNUM: str := Tostr(ParseVal(actasmpattern,16));
  1166. AS_BINNUM: str := Tostr(ParseVal(actasmpattern,2));
  1167. AS_OCTALNUM: str := Tostr(ParseVal(actasmpattern,8));
  1168. else
  1169. Message(asmr_e_syntax_error);
  1170. end;
  1171. Consume(actasmtoken);
  1172. val(str, l, code);
  1173. if code <> 0 then
  1174. Message(asmr_e_invalid_reference_syntax)
  1175. else
  1176. oper.opr.ref.offset := l;
  1177. Consume(AS_RPAREN);
  1178. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1179. begin
  1180. { error recovery ... }
  1181. Message(asmr_e_invalid_reference_syntax);
  1182. while actasmtoken <> AS_SEPARATOR do
  1183. Consume(actasmtoken);
  1184. end;
  1185. exit;
  1186. end;
  1187. else
  1188. begin
  1189. Message(asmr_e_invalid_reference_syntax);
  1190. while (actasmtoken <> AS_SEPARATOR) do
  1191. Consume(actasmtoken);
  1192. end;
  1193. end;
  1194. end;
  1195. Procedure TM68kMotreader.BuildOperand(const oper:tm68koperand);
  1196. {*********************************************************************}
  1197. { EXIT CONDITION: On exit the routine should point to either the }
  1198. { AS_COMMA or AS_SEPARATOR token. }
  1199. {*********************************************************************}
  1200. var
  1201. expr: string;
  1202. tempstr: string;
  1203. lab: tasmlabel;
  1204. l : longint;
  1205. i: Tsuperregister;
  1206. r:Tregister;
  1207. hl: tasmlabel;
  1208. reg_one, reg_two: tregister;
  1209. addrregset,dataregset: tcpuregisterset;
  1210. p: pointer;
  1211. begin
  1212. dataregset := [];
  1213. addrregset := [];
  1214. tempstr := '';
  1215. case actasmtoken of
  1216. { // Memory reference // }
  1217. AS_LPAREN:
  1218. begin
  1219. Oper.InitRef;
  1220. BuildReference(oper);
  1221. end;
  1222. { // Constant expression // }
  1223. AS_APPT: begin
  1224. Consume(AS_APPT);
  1225. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1226. Message(asmr_e_invalid_operand_type);
  1227. { identifiers are handled by BuildExpression }
  1228. oper.opr.typ := OPR_CONSTANT;
  1229. oper.opr.val :=BuildExpression(true,@tempstr);
  1230. if tempstr<>'' then
  1231. begin
  1232. l:=oper.opr.val;
  1233. oper.opr.typ := OPR_SYMBOL;
  1234. oper.opr.symofs := l;
  1235. oper.opr.symbol := current_asmdata.RefAsmSymbol(tempstr);
  1236. end;
  1237. end;
  1238. { // Constant memory offset . // }
  1239. { // This must absolutely be followed by ( // }
  1240. AS_HEXNUM,AS_INTNUM,
  1241. AS_BINNUM,AS_OCTALNUM,AS_PLUS:
  1242. begin
  1243. Oper.InitRef;
  1244. oper.opr.ref.offset:=BuildRefExpression;
  1245. BuildReference(oper);
  1246. end;
  1247. { // A constant expression, or a Variable ref. // }
  1248. AS_ID: begin
  1249. Oper.InitRef;
  1250. if actasmpattern[1] = '@' then
  1251. { // Label or Special symbol reference // }
  1252. begin
  1253. if actasmpattern = '@RESULT' then
  1254. oper.SetUpResult
  1255. else
  1256. if actasmpattern = 'SELF' then
  1257. oper.SetUpSelf
  1258. else
  1259. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1260. Message(asmr_w_CODE_and_DATA_not_supported)
  1261. else
  1262. begin
  1263. delete(actasmpattern,1,1);
  1264. if actasmpattern = '' then
  1265. Message(asmr_e_null_label_ref_not_allowed);
  1266. CreateLocalLabel(actasmpattern,lab,false);
  1267. oper.opr.typ := OPR_SYMBOL;
  1268. oper.opr.symbol := lab;
  1269. oper.opr.symofs := 0;
  1270. // labeled := TRUE;
  1271. end;
  1272. Consume(AS_ID);
  1273. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1274. Message(asmr_e_syntax_error);
  1275. end
  1276. { probably a variable or normal expression }
  1277. { or a procedure (such as in CALL ID) }
  1278. else
  1279. begin
  1280. { is it a constant ? }
  1281. if SearchIConstant(actasmpattern,l) then
  1282. begin
  1283. Oper.InitRef;
  1284. oper.opr.ref.offset:=BuildRefExpression;
  1285. BuildReference(oper);
  1286. end
  1287. else { is it a label variable ? }
  1288. { // ID[ , ID.Field.Field or simple ID // }
  1289. { check if this is a label, if so then }
  1290. { emit it as a label. }
  1291. if SearchLabel(actasmpattern,hl,false) then
  1292. begin
  1293. oper.opr.typ := OPR_SYMBOL;
  1294. oper.opr.symbol := hl;
  1295. oper.opr.symofs := 0;
  1296. // labeled := TRUE;
  1297. Consume(AS_ID);
  1298. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1299. Message(asmr_e_syntax_error);
  1300. end
  1301. else begin
  1302. expr:=actasmpattern;
  1303. Consume(AS_ID);
  1304. { typecasting? }
  1305. if SearchType(expr,l) then
  1306. begin
  1307. oper.hastype:=true;
  1308. oper.typesize:=l;
  1309. case actasmtoken of
  1310. AS_LPAREN :
  1311. begin
  1312. { Support Type([Reference]) }
  1313. Consume(AS_LPAREN);
  1314. BuildOperand(oper{,true});
  1315. Consume(AS_RPAREN);
  1316. end;
  1317. AS_LBRACKET :
  1318. begin
  1319. { Support Var.Type[Index] }
  1320. { Convert @label.Byte[1] to reference }
  1321. if oper.opr.typ=OPR_SYMBOL then
  1322. oper.initref;
  1323. end;
  1324. end;
  1325. end
  1326. else
  1327. begin
  1328. if not oper.SetupVar(expr,false) then
  1329. begin
  1330. { not a variable, check special variables.. }
  1331. if expr = 'SELF' then
  1332. oper.SetupSelf
  1333. else begin
  1334. writeln('unknown id: ',expr);
  1335. Message1(sym_e_unknown_id,expr);
  1336. end;
  1337. expr:='';
  1338. end;
  1339. end;
  1340. // Message1(sym_e_unknown_id,actasmpattern);
  1341. end;
  1342. case actasmtoken of
  1343. AS_LPAREN: { indexing }
  1344. BuildReference(oper);
  1345. AS_SEPARATOR,AS_COMMA: begin
  1346. end;
  1347. else
  1348. Message(asmr_e_syntax_error);
  1349. end;
  1350. end;
  1351. end;
  1352. { // Pre-decrement mode reference or constant mem offset. // }
  1353. AS_MINUS: begin
  1354. Consume(AS_MINUS);
  1355. if actasmtoken = AS_LPAREN then
  1356. begin
  1357. Oper.InitRef;
  1358. { indicate pre-decrement mode }
  1359. oper.opr.ref.direction := dir_dec;
  1360. BuildReference(oper);
  1361. end
  1362. else
  1363. if actasmtoken in [AS_OCTALNUM,AS_HEXNUM,AS_BINNUM,AS_INTNUM] then
  1364. begin
  1365. Oper.InitRef;
  1366. oper.opr.ref.offset:=BuildRefExpression;
  1367. { negate because was preceded by a negative sign! }
  1368. oper.opr.ref.offset:=-oper.opr.ref.offset;
  1369. BuildReference(oper);
  1370. end
  1371. else
  1372. begin
  1373. Message(asmr_e_syntax_error);
  1374. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1375. Consume(actasmtoken);
  1376. end;
  1377. end;
  1378. { // Register, a variable reference or a constant reference // }
  1379. AS_REGISTER: begin
  1380. // writeln('register! ',actasmpattern);
  1381. { save the type of register used. }
  1382. tempstr := actasmpattern;
  1383. Consume(AS_REGISTER);
  1384. { // Simple register // }
  1385. if (actasmtoken = AS_SEPARATOR) or (actasmtoken = AS_COMMA) then
  1386. begin
  1387. // writeln('simple reg');
  1388. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1389. Message(asmr_e_invalid_operand_type);
  1390. oper.opr.typ := OPR_REGISTER;
  1391. oper.opr.reg := actasmregister;
  1392. end
  1393. else
  1394. { HERE WE MUST HANDLE THE SPECIAL CASE OF MOVEM AND FMOVEM }
  1395. { // Individual register listing // }
  1396. if (actasmtoken = AS_SLASH) then
  1397. begin
  1398. r:=actasmregister;
  1399. if getregtype(r)=R_ADDRESSREGISTER then
  1400. include(addrregset,getsupreg(r))
  1401. else if getregtype(r)=R_INTREGISTER then
  1402. include(dataregset,getsupreg(r))
  1403. else
  1404. internalerror(200302191);
  1405. Consume(AS_SLASH);
  1406. if actasmtoken = AS_REGISTER then
  1407. begin
  1408. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1409. begin
  1410. case actasmtoken of
  1411. AS_REGISTER:
  1412. begin
  1413. if getregtype(r)=R_ADDRESSREGISTER then
  1414. include(addrregset,getsupreg(r))
  1415. else if getregtype(r)=R_INTREGISTER then
  1416. include(dataregset,getsupreg(r))
  1417. else
  1418. Message(asmr_e_invalid_reg_list_in_movem);
  1419. Consume(AS_REGISTER);
  1420. end;
  1421. AS_SLASH: Consume(AS_SLASH);
  1422. AS_SEPARATOR,AS_COMMA: break;
  1423. else
  1424. begin
  1425. Message(asmr_e_invalid_reg_list_in_movem);
  1426. Consume(actasmtoken);
  1427. end;
  1428. end; { end case }
  1429. end; { end while }
  1430. oper.opr.typ:= OPR_regset;
  1431. oper.opr.regsetdata := dataregset;
  1432. oper.opr.regsetaddr := addrregset;
  1433. end
  1434. else
  1435. { error recovery ... }
  1436. begin
  1437. Message(asmr_e_invalid_reg_list_in_movem);
  1438. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1439. Consume(actasmtoken);
  1440. end;
  1441. end
  1442. else
  1443. { // Range register listing // }
  1444. if (actasmtoken = AS_MINUS) then
  1445. begin
  1446. Consume(AS_MINUS);
  1447. reg_one:=actasmregister;
  1448. if actasmtoken <> AS_REGISTER then
  1449. begin
  1450. Message(asmr_e_invalid_reg_list_in_movem);
  1451. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1452. Consume(actasmtoken);
  1453. end
  1454. else
  1455. begin
  1456. { determine the register range ... }
  1457. reg_two:=actasmregister;
  1458. if getregtype(r)=R_ADDRESSREGISTER then
  1459. begin
  1460. if getsupreg(reg_one) > getsupreg(reg_two) then
  1461. for i:=getsupreg(reg_two) to getsupreg(reg_one) do
  1462. include(addrregset,i)
  1463. else
  1464. for i:=getsupreg(reg_one) to getsupreg(reg_two) do
  1465. include(addrregset,i);
  1466. end
  1467. else if getregtype(r)=R_INTREGISTER then
  1468. begin
  1469. if getsupreg(reg_one) > getsupreg(reg_two) then
  1470. for i:=getsupreg(reg_two) to getsupreg(reg_one) do
  1471. include(dataregset,i)
  1472. else
  1473. for i:=getsupreg(reg_one) to getsupreg(reg_two) do
  1474. include(dataregset,i);
  1475. end
  1476. else
  1477. Message(asmr_e_invalid_reg_list_in_movem);
  1478. Consume(AS_REGISTER);
  1479. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1480. begin
  1481. Message(asmr_e_invalid_reg_list_in_movem);
  1482. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1483. Consume(actasmtoken);
  1484. end;
  1485. { set up instruction }
  1486. oper.opr.typ:= OPR_regset;
  1487. oper.opr.regsetdata := dataregset;
  1488. oper.opr.regsetaddr := addrregset;
  1489. end;
  1490. end
  1491. else
  1492. { DIVSL/DIVS/MULS/MULU with long for MC68020 only }
  1493. if (actasmtoken = AS_COLON) then
  1494. begin
  1495. if (current_settings.cputype = cpu_MC68020) or (cs_compilesystem in current_settings.moduleswitches) then
  1496. begin
  1497. Consume(AS_COLON);
  1498. if (actasmtoken = AS_REGISTER) then
  1499. begin
  1500. { set up old field, since register is valid }
  1501. oper.opr.typ := OPR_REGISTER;
  1502. oper.opr.reg := actasmregister;
  1503. Inc(operandnum);
  1504. oper.opr.typ := OPR_REGISTER;
  1505. oper.opr.reg := actasmregister;
  1506. Consume(AS_REGISTER);
  1507. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1508. begin
  1509. Message(asmr_e_invalid_reg_list_for_opcode);
  1510. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1511. Consume(actasmtoken);
  1512. end;
  1513. end;
  1514. end
  1515. else
  1516. begin
  1517. Message1(asmr_e_higher_cpu_mode_required,'68020');
  1518. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1519. begin
  1520. Message(asmr_e_invalid_reg_list_for_opcode);
  1521. while not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1522. Consume(actasmtoken);
  1523. end;
  1524. end;
  1525. end
  1526. else
  1527. Message(asmr_e_invalid_register);
  1528. end;
  1529. AS_SEPARATOR, AS_COMMA: ;
  1530. else
  1531. begin
  1532. Message(asmr_e_invalid_opcode_and_operand);
  1533. Consume(actasmtoken);
  1534. end;
  1535. end; { end case }
  1536. end;
  1537. Procedure tm68kmotreader.BuildStringConstant(asciiz: boolean);
  1538. {*********************************************************************}
  1539. { PROCEDURE BuildStringConstant }
  1540. { Description: Takes care of a ASCII, or ASCIIZ directive. }
  1541. { asciiz: boolean -> if true then string will be null terminated. }
  1542. {*********************************************************************}
  1543. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1544. { On ENTRY: Token should point to AS_STRING }
  1545. {*********************************************************************}
  1546. var
  1547. expr: string;
  1548. errorflag : boolean;
  1549. begin
  1550. errorflag := FALSE;
  1551. Repeat
  1552. Case actasmtoken of
  1553. AS_STRING: begin
  1554. expr:=actasmpattern;
  1555. if asciiz then
  1556. expr:=expr+#0;
  1557. ConcatString(curlist,expr);
  1558. Consume(AS_STRING);
  1559. end;
  1560. AS_COMMA: begin
  1561. Consume(AS_COMMA);
  1562. END;
  1563. AS_SEPARATOR: ;
  1564. else
  1565. begin
  1566. Consume(actasmtoken);
  1567. if not errorflag then
  1568. Message(asmr_e_invalid_string_expression);
  1569. errorflag := TRUE;
  1570. end;
  1571. end; { end case }
  1572. Until actasmtoken = AS_SEPARATOR;
  1573. end;
  1574. Procedure TM68kmotReader.BuildOpCode(instr:Tm68kinstruction);
  1575. {*********************************************************************}
  1576. { PROCEDURE BuildOpcode; }
  1577. { Description: Parses the intel opcode and operands, and writes it }
  1578. { in the TInstruction object. }
  1579. {*********************************************************************}
  1580. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1581. { On ENTRY: Token should point to AS_OPCODE }
  1582. {*********************************************************************}
  1583. var
  1584. operandnum : longint;
  1585. begin
  1586. { // opcode // }
  1587. { allow for newline as in gas styled syntax }
  1588. { under DOS you get two AS_SEPARATOR !! }
  1589. while actasmtoken=AS_SEPARATOR do
  1590. Consume(AS_SEPARATOR);
  1591. if (actasmtoken <> AS_OPCODE) then
  1592. begin
  1593. Message(asmr_e_invalid_or_missing_opcode);
  1594. { error recovery }
  1595. While not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) do
  1596. Consume(actasmtoken);
  1597. exit;
  1598. end
  1599. else
  1600. begin
  1601. Instr.opcode := findopcode(actasmpattern,instr.opsize);
  1602. Consume(AS_OPCODE);
  1603. { // Zero operand opcode ? // }
  1604. if actasmtoken = AS_SEPARATOR then
  1605. exit
  1606. else
  1607. operandnum := 1;
  1608. end;
  1609. While actasmtoken <> AS_SEPARATOR do
  1610. begin
  1611. case actasmtoken of
  1612. { // Operand delimiter // }
  1613. AS_COMMA: begin
  1614. if operandnum > Max_Operands then
  1615. Message(asmr_e_too_many_operands)
  1616. else
  1617. Inc(operandnum);
  1618. Consume(AS_COMMA);
  1619. end;
  1620. { // End of asm operands for this opcode // }
  1621. AS_SEPARATOR: ;
  1622. else
  1623. BuildOperand(Instr.Operands[operandnum] as tm68koperand);
  1624. end; { end case }
  1625. end; { end while }
  1626. instr.Ops:=operandnum;
  1627. end;
  1628. function tm68kmotreader.Assemble: tlinkedlist;
  1629. var
  1630. hl: tasmlabel;
  1631. instr : TM68kInstruction;
  1632. begin
  1633. //Message(asmr_d_start_reading);
  1634. firsttoken := TRUE;
  1635. operandnum := 0;
  1636. { sets up all opcode and register tables in uppercase }
  1637. if not _asmsorted then
  1638. begin
  1639. SetupTables;
  1640. _asmsorted := TRUE;
  1641. end;
  1642. curlist:=TAsmList.Create;
  1643. { setup label linked list }
  1644. LocalLabelList:=TLocalLabelList.Create;
  1645. c:=current_scanner.asmgetchar;
  1646. gettoken;
  1647. while actasmtoken<>AS_END do
  1648. begin
  1649. case actasmtoken of
  1650. AS_LLABEL:
  1651. begin
  1652. if CreateLocalLabel(actasmpattern,hl,true) then
  1653. ConcatLabel(curlist,hl);
  1654. Consume(AS_LLABEL);
  1655. end;
  1656. AS_LABEL:
  1657. begin
  1658. { when looking for Pascal labels, these must }
  1659. { be in uppercase. }
  1660. if SearchLabel(upper(actasmpattern),hl,true) then
  1661. ConcatLabel(curlist,hl)
  1662. else
  1663. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1664. Consume(AS_LABEL);
  1665. end;
  1666. AS_DW:
  1667. begin
  1668. Consume(AS_DW);
  1669. BuildConstant($ffff);
  1670. end;
  1671. AS_DB:
  1672. begin
  1673. Consume(AS_DB);
  1674. BuildConstant($ff);
  1675. end;
  1676. AS_DD:
  1677. begin
  1678. Consume(AS_DD);
  1679. BuildConstant(longint($ffffffff));
  1680. end;
  1681. AS_XDEF:
  1682. begin
  1683. Consume(AS_XDEF);
  1684. if actasmtoken=AS_ID then
  1685. ConcatPublic(curlist,actasmpattern);
  1686. Consume(AS_ID);
  1687. if actasmtoken<>AS_SEPARATOR then
  1688. Consume(AS_SEPARATOR);
  1689. end;
  1690. AS_ALIGN:
  1691. begin
  1692. Message(asmr_w_align_not_supported);
  1693. while actasmtoken <> AS_SEPARATOR do
  1694. Consume(actasmtoken);
  1695. end;
  1696. AS_OPCODE:
  1697. begin
  1698. instr:=TM68kInstruction.Create(tm68koperand);
  1699. BuildOpcode(instr);
  1700. // instr.AddReferenceSizes;
  1701. // instr.SetInstructionOpsize;
  1702. // instr.CheckOperandSizes;
  1703. if instr.labeled then
  1704. instr.ConcatLabeledInstr(curlist)
  1705. else begin
  1706. instr.ConcatInstruction(curlist);
  1707. end;
  1708. instr.Free;
  1709. {
  1710. instr.init;
  1711. BuildOpcode;
  1712. instr.ops := operandnum;
  1713. if instr.labeled then
  1714. ConcatLabeledInstr(instr)
  1715. else
  1716. ConcatOpCode(instr);
  1717. instr.done;}
  1718. end;
  1719. AS_SEPARATOR:
  1720. begin
  1721. Consume(AS_SEPARATOR);
  1722. { let us go back to the first operand }
  1723. operandnum := 0;
  1724. end;
  1725. AS_END:
  1726. { end assembly block }
  1727. ;
  1728. else
  1729. begin
  1730. Message(asmr_e_syntax_error);
  1731. { error recovery }
  1732. Consume(actasmtoken);
  1733. end;
  1734. end; { end case }
  1735. end; { end while }
  1736. { Check LocalLabelList }
  1737. LocalLabelList.CheckEmitted;
  1738. LocalLabelList.Free;
  1739. assemble:=curlist;
  1740. //Message(asmr_d_finish_reading);
  1741. end;
  1742. {*****************************************************************************
  1743. Initialize
  1744. *****************************************************************************}
  1745. const
  1746. asmmode_m68k_mot_info : tasmmodeinfo =
  1747. (
  1748. id : asmmode_m68k_mot;
  1749. idtxt : 'MOTOROLA';
  1750. casmreader : tm68kmotreader;
  1751. );
  1752. asmmode_m68k_standard_info : tasmmodeinfo =
  1753. (
  1754. id : asmmode_standard;
  1755. idtxt : 'STANDARD';
  1756. casmreader : tm68kmotreader;
  1757. );
  1758. begin
  1759. RegisterAsmMode(asmmode_m68k_mot_info);
  1760. RegisterAsmMode(asmmode_m68k_standard_info);
  1761. end.