2
0

ra68kmot.pas 64 KB

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