ra68kmot.pas 82 KB

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