ra68kmot.pas 69 KB

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