ra68k.pas 82 KB

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