ra386att.pas 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  1. {
  2. $Id$
  3. Copyright (c) 1997-99 by Carl Eric Codere and Peter Vreman
  4. Does the parsing for the AT&T styled 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. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif TP}
  21. Unit Ra386att;
  22. Interface
  23. uses
  24. tree;
  25. function assemble: ptree;
  26. Implementation
  27. Uses
  28. globtype,
  29. strings,cobjects,systems,verbose,globals,
  30. files,aasm,types,symtable,scanner,hcodegen
  31. ,i386base
  32. ,rautils,ra386;
  33. type
  34. tasmtoken = (
  35. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  36. AS_REALNUM,AS_COMMA,AS_LPAREN,
  37. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  38. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  39. {------------------ Assembler directives --------------------}
  40. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,AS_ALIGN,AS_ASCII,
  41. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,
  42. AS_DATA,AS_TEXT,AS_END,
  43. {------------------ Assembler Operators --------------------}
  44. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR);
  45. tasmkeyword = string[8];
  46. const
  47. { These tokens should be modified accordingly to the modifications }
  48. { in the different enumerations. }
  49. firstdirective = AS_DB;
  50. lastdirective = AS_END;
  51. _count_asmprefixes = 5;
  52. _count_asmspecialops = 25;
  53. _count_asmoverrides = 3;
  54. token2str : array[tasmtoken] of string[10]=(
  55. '','Label','LLabel','string','integer',
  56. 'float',',','(',
  57. ')',':','.','+','-','*',
  58. ';','identifier','register','opcode','/','$',
  59. '.byte','.word','.long','.quad','.globl','.align','.ascii',
  60. '.asciz','.lcomm','.comm','.single','.double','.tfloat',
  61. '.data','.text','END',
  62. '%','<<','>>','!','&','|','^','~');
  63. const
  64. newline = #10;
  65. firsttoken : boolean = TRUE;
  66. operandnum : byte = 0;
  67. charcount : byte = 0;
  68. var
  69. _asmsorted,
  70. inexpression : boolean;
  71. curlist : paasmoutput;
  72. c : char;
  73. actasmtoken : tasmtoken;
  74. prevasmtoken : tasmtoken;
  75. actasmpattern : string;
  76. actopcode : tasmop;
  77. actasmregister : tregister;
  78. actopsize : topsize;
  79. actcondition : tasmcond;
  80. Instr : TInstruction;
  81. labellist : TAsmLabelList;
  82. iasmops : ^op2strtable;
  83. iasmregs : ^reg2strtable;
  84. Procedure SetupTables;
  85. { creates uppercased symbol tables for speed access }
  86. var
  87. i : tasmop;
  88. j : tregister;
  89. Begin
  90. { opcodes }
  91. new(iasmops);
  92. for i:=firstop to lastop do
  93. iasmops^[i] := upper(att_op2str[i]);
  94. { registers }
  95. new(iasmregs);
  96. for j:=firstreg to lastreg do
  97. iasmregs^[j] := upper(att_reg2str[j]);
  98. end;
  99. {---------------------------------------------------------------------}
  100. { Routines for the tokenizing }
  101. {---------------------------------------------------------------------}
  102. function is_asmopcode(const s: string):boolean;
  103. const
  104. att_sizesuffixstr : array[0..8] of string[2] = (
  105. '','B','W','L','BW','BL','WL','Q','T'
  106. );
  107. att_sizesuffix : array[0..8] of topsize = (
  108. S_NO,S_B,S_W,S_L,S_BW,S_BL,S_WL,S_IQ,S_FX
  109. );
  110. var
  111. i : tasmop;
  112. cond : string[4];
  113. cnd : tasmcond;
  114. len,
  115. j,
  116. sufidx : longint;
  117. hid : string;
  118. Begin
  119. is_asmopcode:=FALSE;
  120. actopcode:=A_None;
  121. actcondition:=C_None;
  122. actopsize:=S_NO;
  123. { search for all possible suffixes }
  124. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  125. begin
  126. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  127. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  128. begin
  129. { here we search the entire table... }
  130. hid:=copy(s,1,len);
  131. for i:=firstop to lastop do
  132. if (length(hid) > 0) and (hid=iasmops^[i]) then
  133. begin
  134. actopsize:=att_sizesuffix[sufidx];
  135. actopcode:=i;
  136. actasmtoken:=AS_OPCODE;
  137. is_asmopcode:=TRUE;
  138. exit;
  139. end;
  140. { not found, check condition opcodes }
  141. j:=0;
  142. while (j<CondAsmOps) do
  143. begin
  144. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  145. begin
  146. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  147. if cond<>'' then
  148. begin
  149. for cnd:=low(TasmCond) to high(TasmCond) do
  150. if Cond=Upper(cond2str[cnd]) then
  151. begin
  152. actopcode:=CondASmOp[j];
  153. actopsize:=att_sizesuffix[sufidx];
  154. actcondition:=cnd;
  155. actasmtoken:=AS_OPCODE;
  156. is_asmopcode:=TRUE;
  157. exit;
  158. end;
  159. end;
  160. end;
  161. inc(j);
  162. end;
  163. end;
  164. end;
  165. end;
  166. Function is_asmdirective(const s: string):boolean;
  167. var
  168. i : tasmtoken;
  169. Begin
  170. for i:=firstdirective to lastdirective do
  171. if s=token2str[i] then
  172. begin
  173. actasmtoken:=i;
  174. is_asmdirective:=true;
  175. exit;
  176. end;
  177. is_asmdirective:=false;
  178. end;
  179. Function is_register(const s: string):boolean;
  180. Var
  181. i : tregister;
  182. Begin
  183. actasmregister:=R_NO;
  184. for i:=firstreg to lastreg do
  185. if s=iasmregs^[i] then
  186. begin
  187. actasmtoken:=AS_REGISTER;
  188. actasmregister:=i;
  189. is_register:=true;
  190. exit;
  191. end;
  192. is_register:=false;
  193. end;
  194. Procedure GetToken;
  195. {*********************************************************************}
  196. { FUNCTION GetToken: tasmtoken; }
  197. { Description: This routine returns intel assembler tokens and }
  198. { does some minor syntax error checking. }
  199. {*********************************************************************}
  200. var
  201. forcelabel: boolean;
  202. errorflag : boolean;
  203. len : longint;
  204. begin
  205. { save old token and reset new token }
  206. prevasmtoken:=actasmtoken;
  207. actasmtoken:=AS_NONE;
  208. { reset }
  209. errorflag:=FALSE;
  210. forcelabel:=FALSE;
  211. actasmpattern:='';
  212. { while space and tab , continue scan... }
  213. while c in [' ',#9] do
  214. c:=current_scanner^.asmgetchar;
  215. { get token pos }
  216. if not (c in [newline,#13,'{',';']) then
  217. current_scanner^.gettokenpos;
  218. { Local Label, Label, Directive, Prefix or Opcode }
  219. if firsttoken and not(c in [newline,#13,'{',';']) then
  220. begin
  221. firsttoken:=FALSE;
  222. len:=0;
  223. { directive or local label }
  224. if c = '.' then
  225. begin
  226. inc(len);
  227. actasmpattern[len]:=c;
  228. { Let us point to the next character }
  229. c:=current_scanner^.asmgetchar;
  230. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  231. begin
  232. inc(len);
  233. actasmpattern[len]:=c;
  234. c:=current_scanner^.asmgetchar;
  235. end;
  236. actasmpattern[0]:=chr(len);
  237. { this is a local label... }
  238. if (actasmpattern[2] = 'L') and (c = ':') then
  239. Begin
  240. { local variables are case sensitive }
  241. actasmtoken:=AS_LLABEL;
  242. { delete .L }
  243. delete(actasmpattern,1,2);
  244. { point to next character ... }
  245. c:=current_scanner^.asmgetchar;
  246. exit;
  247. end
  248. { must be a directive }
  249. else
  250. Begin
  251. { directives are case sensitive!! }
  252. if is_asmdirective(actasmpattern) then
  253. exit;
  254. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  255. end;
  256. end;
  257. { only opcodes and global labels are allowed now. }
  258. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  259. begin
  260. inc(len);
  261. actasmpattern[len]:=c;
  262. c:=current_scanner^.asmgetchar;
  263. end;
  264. actasmpattern[0]:=chr(len);
  265. { Label ? }
  266. if c = ':' then
  267. begin
  268. actasmtoken:=AS_LABEL;
  269. { let us point to the next character }
  270. c:=current_scanner^.asmgetchar;
  271. exit;
  272. end;
  273. { Opcode ? }
  274. If is_asmopcode(upper(actasmpattern)) then
  275. Begin
  276. uppervar(actasmpattern);
  277. exit;
  278. end;
  279. { End of assemblerblock ? }
  280. if upper(actasmpattern) = 'END' then
  281. begin
  282. actasmtoken:=AS_END;
  283. exit;
  284. end;
  285. actasmtoken:=AS_NONE;
  286. end
  287. else { else firsttoken }
  288. { Here we must handle all possible cases }
  289. begin
  290. case c of
  291. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  292. { - field of object/record }
  293. { - directive. }
  294. begin
  295. if (prevasmtoken=AS_ID) then
  296. begin
  297. c:=current_scanner^.asmgetchar;
  298. actasmtoken:=AS_DOT;
  299. exit;
  300. end;
  301. actasmpattern:=c;
  302. c:=current_scanner^.asmgetchar;
  303. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  304. begin
  305. actasmpattern:=actasmpattern + c;
  306. c:=current_scanner^.asmgetchar;
  307. end;
  308. if is_asmdirective(actasmpattern) then
  309. exit;
  310. { local label references and directives }
  311. { are case sensitive }
  312. actasmtoken:=AS_ID;
  313. exit;
  314. end;
  315. { identifier, register, prefix or directive }
  316. '_','A'..'Z','a'..'z':
  317. begin
  318. len:=0;
  319. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  320. begin
  321. inc(len);
  322. actasmpattern[len]:=c;
  323. c:=current_scanner^.asmgetchar;
  324. end;
  325. actasmpattern[0]:=chr(len);
  326. uppervar(actasmpattern);
  327. { Opcode, can only be when the previous was a prefix }
  328. If (OperandNum=0) and is_asmopcode(upper(actasmpattern)) then
  329. Begin
  330. uppervar(actasmpattern);
  331. exit;
  332. end;
  333. { check for end which is a reserved word unlike the opcodes }
  334. if actasmpattern = 'END' then
  335. Begin
  336. actasmtoken:=AS_END;
  337. exit;
  338. end;
  339. actasmtoken:=AS_ID;
  340. exit;
  341. end;
  342. '%' : { register or modulo }
  343. begin
  344. len:=1;
  345. actasmpattern[len]:='%';
  346. c:=current_scanner^.asmgetchar;
  347. while c in ['a'..'z','A'..'Z','0'..'9'] do
  348. Begin
  349. inc(len);
  350. actasmpattern[len]:=c;
  351. c:=current_scanner^.asmgetchar;
  352. end;
  353. actasmpattern[0]:=chr(len);
  354. uppervar(actasmpattern);
  355. if (actasmpattern = '%ST') and (c='(') then
  356. Begin
  357. actasmpattern:=actasmpattern+c;
  358. c:=current_scanner^.asmgetchar;
  359. if c in ['0'..'9'] then
  360. actasmpattern:=actasmpattern + c
  361. else
  362. Message(asmr_e_invalid_fpu_register);
  363. c:=current_scanner^.asmgetchar;
  364. if c <> ')' then
  365. Message(asmr_e_invalid_fpu_register)
  366. else
  367. Begin
  368. actasmpattern:=actasmpattern + c;
  369. c:=current_scanner^.asmgetchar; { let us point to next character. }
  370. end;
  371. end;
  372. if is_register(actasmpattern) then
  373. exit;
  374. Message(asmr_w_modulo_not_supported);
  375. end;
  376. '1'..'9': { integer number }
  377. begin
  378. len:=0;
  379. while c in ['0'..'9'] do
  380. Begin
  381. inc(len);
  382. actasmpattern[len]:=c;
  383. c:=current_scanner^.asmgetchar;
  384. end;
  385. actasmpattern[0]:=chr(len);
  386. actasmpattern:=tostr(ValDecimal(actasmpattern));
  387. actasmtoken:=AS_INTNUM;
  388. exit;
  389. end;
  390. '0' : { octal,hexa,real or binary number. }
  391. begin
  392. actasmpattern:=c;
  393. c:=current_scanner^.asmgetchar;
  394. case upcase(c) of
  395. 'B': { binary }
  396. Begin
  397. c:=current_scanner^.asmgetchar;
  398. while c in ['0','1'] do
  399. Begin
  400. actasmpattern:=actasmpattern + c;
  401. c:=current_scanner^.asmgetchar;
  402. end;
  403. actasmpattern:=tostr(ValBinary(actasmpattern));
  404. actasmtoken:=AS_INTNUM;
  405. exit;
  406. end;
  407. 'D': { real }
  408. Begin
  409. c:=current_scanner^.asmgetchar;
  410. { get ridd of the 0d }
  411. if (c in ['+','-']) then
  412. begin
  413. actasmpattern:=c;
  414. c:=current_scanner^.asmgetchar;
  415. end
  416. else
  417. actasmpattern:='';
  418. while c in ['0'..'9'] do
  419. Begin
  420. actasmpattern:=actasmpattern + c;
  421. c:=current_scanner^.asmgetchar;
  422. end;
  423. if c='.' then
  424. begin
  425. actasmpattern:=actasmpattern + c;
  426. c:=current_scanner^.asmgetchar;
  427. while c in ['0'..'9'] do
  428. Begin
  429. actasmpattern:=actasmpattern + c;
  430. c:=current_scanner^.asmgetchar;
  431. end;
  432. if upcase(c) = 'E' then
  433. begin
  434. actasmpattern:=actasmpattern + c;
  435. c:=current_scanner^.asmgetchar;
  436. if (c in ['+','-']) then
  437. begin
  438. actasmpattern:=actasmpattern + c;
  439. c:=current_scanner^.asmgetchar;
  440. end;
  441. while c in ['0'..'9'] do
  442. Begin
  443. actasmpattern:=actasmpattern + c;
  444. c:=current_scanner^.asmgetchar;
  445. end;
  446. end;
  447. actasmtoken:=AS_REALNUM;
  448. exit;
  449. end
  450. else
  451. begin
  452. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  453. actasmtoken:=AS_NONE;
  454. end;
  455. end;
  456. 'X': { hexadecimal }
  457. Begin
  458. c:=current_scanner^.asmgetchar;
  459. while c in ['0'..'9','a'..'f','A'..'F'] do
  460. Begin
  461. actasmpattern:=actasmpattern + c;
  462. c:=current_scanner^.asmgetchar;
  463. end;
  464. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  465. actasmtoken:=AS_INTNUM;
  466. exit;
  467. end;
  468. '1'..'7': { octal }
  469. begin
  470. actasmpattern:=actasmpattern + c;
  471. while c in ['0'..'7'] do
  472. Begin
  473. actasmpattern:=actasmpattern + c;
  474. c:=current_scanner^.asmgetchar;
  475. end;
  476. actasmpattern:=tostr(ValOctal(actasmpattern));
  477. actasmtoken:=AS_INTNUM;
  478. exit;
  479. end;
  480. else { octal number zero value...}
  481. Begin
  482. actasmpattern:=tostr(ValOctal(actasmpattern));
  483. actasmtoken:=AS_INTNUM;
  484. exit;
  485. end;
  486. end; { end case }
  487. end;
  488. '&' :
  489. begin
  490. c:=current_scanner^.asmgetchar;
  491. actasmtoken:=AS_AND;
  492. end;
  493. '''' : { char }
  494. begin
  495. actasmpattern:='';
  496. repeat
  497. c:=current_scanner^.asmgetchar;
  498. case c of
  499. '\' :
  500. begin
  501. { copy also the next char so \" is parsed correctly }
  502. c:=current_scanner^.asmgetchar;
  503. actasmpattern:=actasmpattern+c;
  504. end;
  505. '''' :
  506. begin
  507. c:=current_scanner^.asmgetchar;
  508. break;
  509. end;
  510. newline:
  511. Message(scan_f_string_exceeds_line);
  512. else
  513. actasmpattern:=actasmpattern+c;
  514. end;
  515. until false;
  516. actasmpattern:=EscapeToPascal(actasmpattern);
  517. actasmtoken:=AS_STRING;
  518. exit;
  519. end;
  520. '"' : { string }
  521. begin
  522. actasmpattern:='';
  523. repeat
  524. c:=current_scanner^.asmgetchar;
  525. case c of
  526. '\' :
  527. begin
  528. { copy also the next char so \" is parsed correctly }
  529. c:=current_scanner^.asmgetchar;
  530. actasmpattern:=actasmpattern+c;
  531. end;
  532. '"' :
  533. begin
  534. c:=current_scanner^.asmgetchar;
  535. break;
  536. end;
  537. newline:
  538. Message(scan_f_string_exceeds_line);
  539. else
  540. actasmpattern:=actasmpattern+c;
  541. end;
  542. until false;
  543. actasmpattern:=EscapeToPascal(actasmpattern);
  544. actasmtoken:=AS_STRING;
  545. exit;
  546. end;
  547. '$' :
  548. begin
  549. actasmtoken:=AS_DOLLAR;
  550. c:=current_scanner^.asmgetchar;
  551. exit;
  552. end;
  553. ',' :
  554. begin
  555. actasmtoken:=AS_COMMA;
  556. c:=current_scanner^.asmgetchar;
  557. exit;
  558. end;
  559. '<' :
  560. begin
  561. actasmtoken:=AS_SHL;
  562. c:=current_scanner^.asmgetchar;
  563. if c = '<' then
  564. c:=current_scanner^.asmgetchar;
  565. exit;
  566. end;
  567. '>' :
  568. begin
  569. actasmtoken:=AS_SHL;
  570. c:=current_scanner^.asmgetchar;
  571. if c = '>' then
  572. c:=current_scanner^.asmgetchar;
  573. exit;
  574. end;
  575. '|' :
  576. begin
  577. actasmtoken:=AS_OR;
  578. c:=current_scanner^.asmgetchar;
  579. exit;
  580. end;
  581. '^' :
  582. begin
  583. actasmtoken:=AS_XOR;
  584. c:=current_scanner^.asmgetchar;
  585. exit;
  586. end;
  587. '!' :
  588. begin
  589. Message(asmr_e_nor_not_supported);
  590. c:=current_scanner^.asmgetchar;
  591. actasmtoken:=AS_NONE;
  592. exit;
  593. end;
  594. '(' :
  595. begin
  596. actasmtoken:=AS_LPAREN;
  597. c:=current_scanner^.asmgetchar;
  598. exit;
  599. end;
  600. ')' :
  601. begin
  602. actasmtoken:=AS_RPAREN;
  603. c:=current_scanner^.asmgetchar;
  604. exit;
  605. end;
  606. ':' :
  607. begin
  608. actasmtoken:=AS_COLON;
  609. c:=current_scanner^.asmgetchar;
  610. exit;
  611. end;
  612. '+' :
  613. begin
  614. actasmtoken:=AS_PLUS;
  615. c:=current_scanner^.asmgetchar;
  616. exit;
  617. end;
  618. '-' :
  619. begin
  620. actasmtoken:=AS_MINUS;
  621. c:=current_scanner^.asmgetchar;
  622. exit;
  623. end;
  624. '*' :
  625. begin
  626. actasmtoken:=AS_STAR;
  627. c:=current_scanner^.asmgetchar;
  628. exit;
  629. end;
  630. '/' :
  631. begin
  632. c:=current_scanner^.asmgetchar;
  633. actasmtoken:=AS_SLASH;
  634. exit;
  635. end;
  636. '{',#13,newline,';' :
  637. begin
  638. { the comment is read by asmgetchar }
  639. c:=current_scanner^.asmgetchar;
  640. firsttoken:=TRUE;
  641. actasmtoken:=AS_SEPARATOR;
  642. exit;
  643. end;
  644. else
  645. Begin
  646. Message(scan_f_illegal_char);
  647. end;
  648. end;
  649. end;
  650. end;
  651. function consume(t : tasmtoken):boolean;
  652. begin
  653. Consume:=true;
  654. if t<>actasmtoken then
  655. begin
  656. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  657. Consume:=false;
  658. end;
  659. repeat
  660. gettoken;
  661. until actasmtoken<>AS_NONE;
  662. end;
  663. procedure RecoverConsume(allowcomma:boolean);
  664. begin
  665. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  666. begin
  667. if allowcomma and (actasmtoken=AS_COMMA) then
  668. break;
  669. Consume(actasmtoken);
  670. end;
  671. end;
  672. {*****************************************************************************
  673. Parsing Helpers
  674. *****************************************************************************}
  675. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  676. { Description: This routine builds up a record offset after a AS_DOT }
  677. { token is encountered. }
  678. { On entry actasmtoken should be equal to AS_DOT }
  679. var
  680. s : string;
  681. Begin
  682. offset:=0;
  683. size:=0;
  684. s:=expr;
  685. while (actasmtoken=AS_DOT) do
  686. begin
  687. Consume(AS_DOT);
  688. if actasmtoken=AS_ID then
  689. s:=s+'.'+actasmpattern;
  690. if not Consume(AS_ID) then
  691. begin
  692. RecoverConsume(true);
  693. break;
  694. end;
  695. end;
  696. if not GetRecordOffsetSize(s,offset,size) then
  697. Message(asmr_e_building_record_offset);
  698. end;
  699. Procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:longint;var asmsym:string);
  700. {*********************************************************************}
  701. { FUNCTION BuildConstExpression: longint }
  702. { Description: This routine calculates a constant expression to }
  703. { a given value. The return value is the value calculated from }
  704. { the expression. }
  705. { The following tokens (not strings) are recognized: }
  706. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  707. {*********************************************************************}
  708. { ENTRY: On entry the token should be any valid expression token. }
  709. { EXIT: On Exit the token points to any token after the closing }
  710. { RBRACKET }
  711. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  712. { invalid tokens. }
  713. {*********************************************************************}
  714. var
  715. hs,tempstr,expr : string;
  716. parenlevel,l,k : longint;
  717. errorflag : boolean;
  718. prevtok : tasmtoken;
  719. sym : psym;
  720. hl : plabel;
  721. Begin
  722. asmsym:='';
  723. value:=0;
  724. errorflag:=FALSE;
  725. tempstr:='';
  726. expr:='';
  727. inexpression:=TRUE;
  728. parenlevel:=0;
  729. Repeat
  730. Case actasmtoken of
  731. AS_LPAREN:
  732. Begin
  733. { Exit if ref? }
  734. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  735. break;
  736. Consume(AS_LPAREN);
  737. expr:=expr + '(';
  738. inc(parenlevel);
  739. end;
  740. AS_RPAREN:
  741. Begin
  742. { end of ref ? }
  743. if (parenlevel=0) and betweenbracket then
  744. break;
  745. Consume(AS_RPAREN);
  746. expr:=expr + ')';
  747. dec(parenlevel);
  748. end;
  749. AS_SHL:
  750. Begin
  751. Consume(AS_SHL);
  752. expr:=expr + '<';
  753. end;
  754. AS_SHR:
  755. Begin
  756. Consume(AS_SHR);
  757. expr:=expr + '>';
  758. end;
  759. AS_SLASH:
  760. Begin
  761. Consume(AS_SLASH);
  762. expr:=expr + '/';
  763. end;
  764. AS_MOD:
  765. Begin
  766. Consume(AS_MOD);
  767. expr:=expr + '%';
  768. end;
  769. AS_STAR:
  770. Begin
  771. Consume(AS_STAR);
  772. expr:=expr + '*';
  773. end;
  774. AS_PLUS:
  775. Begin
  776. Consume(AS_PLUS);
  777. expr:=expr + '+';
  778. end;
  779. AS_MINUS:
  780. Begin
  781. Consume(AS_MINUS);
  782. expr:=expr + '-';
  783. end;
  784. AS_AND:
  785. Begin
  786. Consume(AS_AND);
  787. expr:=expr + '&';
  788. end;
  789. AS_NOT:
  790. Begin
  791. Consume(AS_NOT);
  792. expr:=expr + '~';
  793. end;
  794. AS_XOR:
  795. Begin
  796. Consume(AS_XOR);
  797. expr:=expr + '^';
  798. end;
  799. AS_OR:
  800. Begin
  801. Consume(AS_OR);
  802. expr:=expr + '|';
  803. end;
  804. AS_INTNUM:
  805. Begin
  806. expr:=expr + actasmpattern;
  807. Consume(AS_INTNUM);
  808. end;
  809. AS_DOLLAR:
  810. begin
  811. Consume(AS_DOLLAR);
  812. if actasmtoken<>AS_ID then
  813. Comment(V_Error,'assem_e_dollar_without_identifier');
  814. end;
  815. AS_ID:
  816. Begin
  817. tempstr:=actasmpattern;
  818. prevtok:=prevasmtoken;
  819. consume(AS_ID);
  820. if actasmtoken=AS_DOT then
  821. begin
  822. BuildRecordOffsetSize(tempstr,l,k);
  823. str(l, tempstr);
  824. expr:=expr + tempstr;
  825. end
  826. else
  827. if SearchIConstant(tempstr,l) then
  828. begin
  829. str(l, tempstr);
  830. expr:=expr + tempstr;
  831. end
  832. else
  833. begin
  834. hs:='';
  835. if SearchLabel(tempstr,hl) then
  836. hs:=lab2str(hl)
  837. else
  838. begin
  839. getsym(tempstr,false);
  840. sym:=srsym;
  841. if assigned(sym) then
  842. begin
  843. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  844. Writeln('can''t use local variable or parameters here');
  845. case srsym^.typ of
  846. varsym :
  847. hs:=pvarsym(srsym)^.mangledname;
  848. typedconstsym :
  849. hs:=ptypedconstsym(srsym)^.mangledname;
  850. procsym :
  851. hs:=pprocsym(srsym)^.mangledname;
  852. else
  853. Message(asmr_e_wrong_sym_type);
  854. end;
  855. end
  856. else
  857. Message1(sym_e_unknown_id,tempstr);
  858. end;
  859. { symbol found? }
  860. if hs<>'' then
  861. begin
  862. if needofs and (prevtok<>AS_DOLLAR) then
  863. Message(asmr_e_need_offset);
  864. if asmsym='' then
  865. asmsym:=hs
  866. else
  867. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  868. if (expr='') or (expr[length(expr)]='+') then
  869. begin
  870. delete(expr,length(expr),1);
  871. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  872. Message(asmr_e_only_add_relocatable_symbol);
  873. end
  874. else
  875. Message(asmr_e_only_add_relocatable_symbol);
  876. end;
  877. end;
  878. end;
  879. AS_SEPARATOR,
  880. AS_COMMA:
  881. Begin
  882. break;
  883. end;
  884. else
  885. Begin
  886. { write error only once. }
  887. if not errorflag then
  888. Message(asmr_e_invalid_constant_expression);
  889. { consume tokens until we find COMMA or SEPARATOR }
  890. Consume(actasmtoken);
  891. errorflag:=TRUE;
  892. end;
  893. end;
  894. Until false;
  895. { calculate expression }
  896. if not ErrorFlag then
  897. value:=CalculateExpression(expr)
  898. else
  899. value:=0;
  900. { no longer in an expression }
  901. inexpression:=FALSE;
  902. end;
  903. Function BuildConstExpression(allowref,betweenbracket:boolean): longint;
  904. var
  905. l : longint;
  906. hs : string;
  907. begin
  908. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs);
  909. if hs<>'' then
  910. Message(asmr_e_relocatable_symbol_not_allowed);
  911. BuildConstExpression:=l;
  912. end;
  913. Procedure BuildRealConstant(typ : tfloattype);
  914. {*********************************************************************}
  915. { PROCEDURE BuilRealConst }
  916. { Description: This routine calculates a constant expression to }
  917. { a given value. The return value is the value calculated from }
  918. { the expression. }
  919. { The following tokens (not strings) are recognized: }
  920. { +/-,numbers and real numbers }
  921. {*********************************************************************}
  922. { ENTRY: On entry the token should be any valid expression token. }
  923. { EXIT: On Exit the token points to either COMMA or SEPARATOR }
  924. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  925. { invalid tokens. }
  926. {*********************************************************************}
  927. var expr: string;
  928. tempstr: string;
  929. r : bestreal;
  930. code : integer;
  931. negativ : boolean;
  932. errorflag: boolean;
  933. Begin
  934. errorflag:=FALSE;
  935. Repeat
  936. negativ:=false;
  937. expr:='';
  938. tempstr:='';
  939. if actasmtoken=AS_PLUS then Consume(AS_PLUS)
  940. else if actasmtoken=AS_MINUS then
  941. begin
  942. negativ:=true;
  943. consume(AS_MINUS);
  944. end;
  945. Case actasmtoken of
  946. AS_INTNUM: Begin
  947. expr:=actasmpattern;
  948. Consume(AS_INTNUM);
  949. end;
  950. AS_REALNUM: Begin
  951. expr:=actasmpattern;
  952. { in ATT syntax you have 0d in front of the real }
  953. { should this be forced ? yes i think so, as to }
  954. { conform to gas as much as possible. }
  955. if (expr[1]='0') and (upper(expr[2])='D') then
  956. expr:=copy(expr,3,255);
  957. Consume(AS_REALNUM);
  958. end;
  959. else
  960. Begin
  961. { only write error once. }
  962. if not errorflag then
  963. Message(asmr_e_invalid_float_expr);
  964. { consume tokens until we find COMMA or SEPARATOR }
  965. Consume(actasmtoken);
  966. errorflag:=TRUE;
  967. End;
  968. end;
  969. { go to next term }
  970. if (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  971. Begin
  972. if negativ then expr:='-'+expr;
  973. val(expr,r,code);
  974. if code<>0 then
  975. Begin
  976. r:=0;
  977. Message(asmr_e_invalid_float_expr);
  978. ConcatRealConstant(curlist,r,typ);
  979. End
  980. else
  981. Begin
  982. ConcatRealConstant(curlist,r,typ);
  983. End;
  984. end
  985. else
  986. Message(asmr_e_invalid_float_expr);
  987. Until actasmtoken=AS_SEPARATOR;
  988. end;
  989. Procedure BuildReference(var Instr: TInstruction);
  990. {*********************************************************************}
  991. { PROCEDURE BuildReference }
  992. { Description: This routine builds up an expression after a LPAREN }
  993. { token is encountered. }
  994. { On entry actasmtoken should be equal to AS_LPAREN }
  995. {*********************************************************************}
  996. { EXIT CONDITION: On exit the routine should point to either the }
  997. { AS_COMMA or AS_SEPARATOR token. }
  998. {*********************************************************************}
  999. procedure Consume_RParen;
  1000. begin
  1001. if actasmtoken <> AS_RPAREN then
  1002. Begin
  1003. Message(asmr_e_invalid_reference_syntax);
  1004. RecoverConsume(true);
  1005. end
  1006. else
  1007. begin
  1008. Consume(AS_RPAREN);
  1009. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR]) then
  1010. Begin
  1011. Message(asmr_e_invalid_reference_syntax);
  1012. RecoverConsume(true);
  1013. end;
  1014. end;
  1015. end;
  1016. procedure Consume_Scale;
  1017. var
  1018. l : longint;
  1019. begin
  1020. { we have to process the scaling }
  1021. l:=BuildConstExpression(false,true);
  1022. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  1023. instr.operands[operandnum].ref.scalefactor:=l
  1024. else
  1025. Begin
  1026. Message(asmr_e_wrong_scale_factor);
  1027. instr.operands[operandnum].ref.scalefactor:=0;
  1028. end;
  1029. end;
  1030. Begin
  1031. Consume(AS_LPAREN);
  1032. initAsmRef(instr,operandnum);
  1033. Case actasmtoken of
  1034. AS_INTNUM,
  1035. AS_MINUS,
  1036. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  1037. Begin
  1038. { offset(offset) is invalid }
  1039. If Instr.Operands[OperandNum].Ref.Offset <> 0 Then
  1040. Begin
  1041. Message(asmr_e_invalid_reference_syntax);
  1042. RecoverConsume(true);
  1043. End
  1044. Else
  1045. Begin
  1046. Instr.Operands[OperandNum].Ref.Offset:=BuildConstExpression(false,true);
  1047. Consume_RParen;
  1048. end;
  1049. exit;
  1050. End;
  1051. AS_REGISTER: { (reg ... }
  1052. Begin
  1053. { Check if there is already a base (mostly ebp,esp) than this is
  1054. not allowed,becuase it will give crashing code }
  1055. if instr.operands[operandnum].ref.base<>R_NO then
  1056. Message(asmr_e_cannot_index_relative_var);
  1057. instr.operands[operandnum].ref.base:=actasmregister;
  1058. Consume(AS_REGISTER);
  1059. { can either be a register or a right parenthesis }
  1060. { (reg) }
  1061. if actasmtoken=AS_RPAREN then
  1062. Begin
  1063. Consume_RParen;
  1064. exit;
  1065. end;
  1066. { (reg,reg .. }
  1067. Consume(AS_COMMA);
  1068. if actasmtoken=AS_REGISTER then
  1069. Begin
  1070. instr.operands[operandnum].ref.index:=actasmregister;
  1071. Consume(AS_REGISTER);
  1072. { check for scaling ... }
  1073. case actasmtoken of
  1074. AS_RPAREN:
  1075. Begin
  1076. Consume_RParen;
  1077. exit;
  1078. end;
  1079. AS_COMMA:
  1080. Begin
  1081. Consume(AS_COMMA);
  1082. Consume_Scale;
  1083. Consume_RParen;
  1084. end;
  1085. else
  1086. Begin
  1087. Message(asmr_e_invalid_reference_syntax);
  1088. RecoverConsume(false);
  1089. end;
  1090. end; { end case }
  1091. end
  1092. else
  1093. Begin
  1094. Message(asmr_e_invalid_reference_syntax);
  1095. RecoverConsume(false);
  1096. end;
  1097. end; {end case }
  1098. AS_COMMA: { (, ... can either be scaling, or index }
  1099. Begin
  1100. Consume(AS_COMMA);
  1101. { Index }
  1102. if (actasmtoken=AS_REGISTER) then
  1103. Begin
  1104. instr.operands[operandnum].ref.index:=actasmregister;
  1105. Consume(AS_REGISTER);
  1106. { check for scaling ... }
  1107. case actasmtoken of
  1108. AS_RPAREN:
  1109. Begin
  1110. Consume_RParen;
  1111. exit;
  1112. end;
  1113. AS_COMMA:
  1114. Begin
  1115. Consume(AS_COMMA);
  1116. Consume_Scale;
  1117. Consume_RParen;
  1118. end;
  1119. else
  1120. Begin
  1121. Message(asmr_e_invalid_reference_syntax);
  1122. RecoverConsume(false);
  1123. end;
  1124. end; {end case }
  1125. end
  1126. { Scaling }
  1127. else
  1128. Begin
  1129. Consume_Scale;
  1130. Consume_RParen;
  1131. exit;
  1132. end;
  1133. end;
  1134. else
  1135. Begin
  1136. Message(asmr_e_invalid_reference_syntax);
  1137. RecoverConsume(false);
  1138. end;
  1139. end;
  1140. end;
  1141. Procedure BuildOperand(var instr: TInstruction);
  1142. Procedure BuildConstOperand(var instr: TInstruction);
  1143. var
  1144. l : longint;
  1145. tempstr : string;
  1146. begin
  1147. BuildConstSymbolExpression(false,false,true,l,tempstr);
  1148. if tempstr<>'' then
  1149. begin
  1150. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1151. instr.operands[operandnum].symofs:=l;
  1152. instr.operands[operandnum].symbol:=newasmsymbol(tempstr);
  1153. end
  1154. else
  1155. begin
  1156. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1157. instr.operands[operandnum].val:=l;
  1158. end;
  1159. end;
  1160. function MaybeBuildReference:boolean;
  1161. { Try to create a reference, if not a reference is found then false
  1162. is returned }
  1163. begin
  1164. MaybeBuildReference:=true;
  1165. case actasmtoken of
  1166. AS_INTNUM,
  1167. AS_MINUS,
  1168. AS_PLUS:
  1169. Begin
  1170. instr.operands[operandnum].ref.offset:=BuildConstExpression(True,False);
  1171. if actasmtoken<>AS_LPAREN then
  1172. Message(asmr_e_invalid_reference_syntax)
  1173. else
  1174. BuildReference(instr);
  1175. end;
  1176. AS_LPAREN:
  1177. BuildReference(instr);
  1178. AS_ID: { only a variable is allowed ... }
  1179. Begin
  1180. { is it a normal variable ? }
  1181. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  1182. begin
  1183. { check for direct symbolic names }
  1184. { only if compiling the system unit }
  1185. if (cs_compilesystem in aktmoduleswitches) then
  1186. begin
  1187. if not SearchDirectVar(instr,actasmpattern,operandnum) then
  1188. Message(asmr_e_invalid_seg_override);
  1189. end
  1190. else
  1191. Message(asmr_e_invalid_seg_override);
  1192. end;
  1193. Consume(actasmtoken);
  1194. case actasmtoken of
  1195. AS_SEPARATOR,
  1196. AS_COMMA: ;
  1197. AS_LPAREN: BuildReference(instr);
  1198. else
  1199. Begin
  1200. Message(asmr_e_invalid_seg_override);
  1201. Consume(actasmtoken);
  1202. end;
  1203. end; {end case }
  1204. end;
  1205. else
  1206. MaybeBuildReference:=false;
  1207. end; { end case }
  1208. end;
  1209. var
  1210. expr,
  1211. tempstr : string;
  1212. tempreg : tregister;
  1213. lab : Pasmlabel;
  1214. hl : plabel;
  1215. tsize,l,
  1216. toffset : longint;
  1217. Begin
  1218. tempstr:='';
  1219. expr:='';
  1220. case actasmtoken of
  1221. AS_LPAREN: { Memory reference or constant expression }
  1222. Begin
  1223. initAsmRef(instr,operandnum);
  1224. BuildReference(instr);
  1225. end;
  1226. AS_DOLLAR: { Constant expression }
  1227. Begin
  1228. Consume(AS_DOLLAR);
  1229. BuildConstOperand(instr);
  1230. end;
  1231. AS_INTNUM,
  1232. AS_MINUS,
  1233. AS_PLUS:
  1234. Begin
  1235. { Constant memory offset }
  1236. { This must absolutely be followed by ( }
  1237. initAsmRef(instr,operandnum);
  1238. instr.operands[operandnum].ref.offset:=BuildConstExpression(True,False);
  1239. if actasmtoken<>AS_LPAREN then
  1240. Message(asmr_e_invalid_reference_syntax)
  1241. else
  1242. BuildReference(instr);
  1243. end;
  1244. AS_STAR: { Call from memory address }
  1245. Begin
  1246. Consume(AS_STAR);
  1247. if actasmtoken=AS_REGISTER then
  1248. begin
  1249. instr.operands[operandnum].operandtype:=OPR_REGISTER;
  1250. instr.operands[operandnum].reg:=actasmregister;
  1251. instr.operands[operandnum].size:=reg_2_opsize[actasmregister];
  1252. Consume(AS_REGISTER);
  1253. end
  1254. else
  1255. begin
  1256. initAsmRef(instr,operandnum);
  1257. if not MaybeBuildReference then
  1258. Message(asmr_e_syn_operand);
  1259. end;
  1260. { this is only allowed for call's and jmp's }
  1261. case instr.opcode of
  1262. A_CALL,
  1263. A_JMP,
  1264. A_Jcc : ;
  1265. else
  1266. Message(asmr_e_syn_operand);
  1267. end;
  1268. end;
  1269. AS_ID: { A constant expression, or a Variable ref. }
  1270. Begin
  1271. { Local label ? }
  1272. if (actasmpattern[1] ='.') and (actasmpattern[2] = 'L') then
  1273. Begin
  1274. delete(actasmpattern,1,2);
  1275. if actasmpattern = '' then
  1276. Message(asmr_e_null_label_ref_not_allowed);
  1277. lab:=labellist.search(actasmpattern);
  1278. { check if the label is already defined }
  1279. { if so, we then check if the plabel is }
  1280. { non-nil, if so we add it to instruction }
  1281. if assigned(lab) then
  1282. Begin
  1283. if assigned(lab^.lab) then
  1284. Begin
  1285. instr.operands[operandnum].operandtype:=OPR_LABINSTR;
  1286. instr.operands[operandnum].hl:=lab^.lab;
  1287. instr.labeled:=TRUE;
  1288. end;
  1289. end
  1290. else
  1291. Begin
  1292. { the label does not exist, create it }
  1293. { emit the opcode, but set that the }
  1294. { label has not been emitted }
  1295. getlabel(hl);
  1296. labellist.insert(actasmpattern,hl,FALSE);
  1297. instr.operands[operandnum].operandtype:=OPR_LABINSTR;
  1298. instr.operands[operandnum].hl:=hl;
  1299. instr.labeled:=TRUE;
  1300. end;
  1301. Consume(AS_ID);
  1302. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1303. Message(asmr_e_syntax_error);
  1304. end
  1305. else
  1306. { probably a variable or normal expression }
  1307. { or a procedure (such as in CALL ID) }
  1308. Begin
  1309. { check if this is a label, if so then }
  1310. { emit it as a label. }
  1311. if SearchLabel(actasmpattern,hl) then
  1312. Begin
  1313. instr.operands[operandnum].operandtype:=OPR_LABINSTR;
  1314. instr.operands[operandnum].hl:=hl;
  1315. instr.labeled:=TRUE;
  1316. Consume(AS_ID);
  1317. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1318. Message(asmr_e_syntax_error);
  1319. end
  1320. else
  1321. { is it a normal variable ? }
  1322. Begin
  1323. { context for scanner }
  1324. initAsmRef(instr,operandnum);
  1325. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  1326. Begin
  1327. { look for special symbols ... }
  1328. if actasmpattern = '__RESULT' then
  1329. SetUpResult(instr,operandnum)
  1330. else
  1331. if actasmpattern = '__SELF' then
  1332. Begin
  1333. if assigned(procinfo._class) then
  1334. Begin
  1335. instr.operands[operandnum].operandtype:=OPR_REFERENCE;
  1336. instr.operands[operandnum].ref.offset:=procinfo.ESI_offset;
  1337. instr.operands[operandnum].ref.base:=procinfo.framepointer;
  1338. end
  1339. else
  1340. Message(asmr_e_cannot_use___SELF_outside_methode);
  1341. end
  1342. else
  1343. if actasmpattern = '__OLDEBP' then
  1344. Begin
  1345. if lexlevel>normal_function_level then
  1346. Begin
  1347. instr.operands[operandnum].operandtype:=OPR_REFERENCE;
  1348. instr.operands[operandnum].ref.offset:=procinfo.framepointer_offset;
  1349. instr.operands[operandnum].ref.base:=procinfo.framepointer;
  1350. end
  1351. else
  1352. Message(asmr_e_cannot_use___OLDEBP_outside_nested_procedure);
  1353. end
  1354. else
  1355. { check for direct symbolic names }
  1356. { only if compiling the system unit }
  1357. if (cs_compilesystem in aktmoduleswitches) then
  1358. begin
  1359. if not SearchDirectVar(instr,actasmpattern,operandnum) then
  1360. Begin
  1361. { not found, finally ... add it anyways ... }
  1362. Message1(asmr_w_id_supposed_external,actasmpattern);
  1363. instr.operands[operandnum].ref.symbol:=newasmsymbol(actasmpattern);
  1364. end;
  1365. end
  1366. else
  1367. Message1(sym_e_unknown_id,actasmpattern);
  1368. end;
  1369. { constant expression? }
  1370. if (instr.operands[operandnum].operandtype=OPR_CONSTANT) then
  1371. begin
  1372. l:=BuildConstExpression(true,false);
  1373. { indexing? }
  1374. if actasmtoken=AS_LPAREN then
  1375. begin
  1376. instr.operands[operandnum].operandtype:=OPR_REFERENCE;
  1377. reset_reference(Instr.Operands[OperandNum].Ref);
  1378. Instr.Operands[OperandNum].Ref.Offset:=l;
  1379. BuildReference(instr);
  1380. end
  1381. else
  1382. Instr.Operands[OperandNum].Val:=l;
  1383. end
  1384. else
  1385. begin
  1386. expr:=actasmpattern;
  1387. Consume(AS_ID);
  1388. if actasmtoken=AS_DOT then
  1389. begin
  1390. BuildRecordOffsetSize(expr,toffset,tsize);
  1391. inc(instr.operands[operandnum].ref.offset,toffset);
  1392. SetOperandSize(instr,operandnum,tsize);
  1393. end;
  1394. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1395. inc(instr.operands[operandnum].ref.offset,BuildConstExpression(true,false));
  1396. if actasmtoken=AS_LPAREN then
  1397. BuildReference(instr);
  1398. end;
  1399. end; { end if }
  1400. end; { end if }
  1401. end;
  1402. AS_REGISTER: { Register, a variable reference or a constant reference }
  1403. Begin
  1404. { save the type of register used. }
  1405. tempreg:=actasmregister;
  1406. Consume(AS_REGISTER);
  1407. if actasmtoken = AS_COLON then
  1408. Begin
  1409. Consume(AS_COLON);
  1410. initAsmRef(instr,operandnum);
  1411. instr.operands[operandnum].ref.segment:=tempreg;
  1412. { This must absolutely be followed by a reference }
  1413. if not MaybeBuildReference then
  1414. Begin
  1415. Message(asmr_e_invalid_seg_override);
  1416. Consume(actasmtoken);
  1417. end;
  1418. end
  1419. { Simple register }
  1420. else if (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1421. Begin
  1422. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  1423. Message(asmr_e_invalid_operand_type);
  1424. instr.operands[operandnum].operandtype:=OPR_REGISTER;
  1425. instr.operands[operandnum].reg:=tempreg;
  1426. instr.operands[operandnum].size:=reg_2_opsize[tempreg];
  1427. end
  1428. else
  1429. Message(asmr_e_syn_operand);
  1430. end;
  1431. AS_SEPARATOR,
  1432. AS_COMMA: ;
  1433. else
  1434. Begin
  1435. Message(asmr_e_syn_operand);
  1436. Consume(actasmtoken);
  1437. end;
  1438. end; { end case }
  1439. end;
  1440. Procedure BuildConstant(maxvalue: longint);
  1441. {*********************************************************************}
  1442. { PROCEDURE BuildConstant }
  1443. { Description: This routine takes care of parsing a DB,DD,or DW }
  1444. { line and adding those to the assembler node. Expressions, range- }
  1445. { checking are fullly taken care of. }
  1446. { maxvalue: $ff -> indicates that this is a DB node. }
  1447. { $ffff -> indicates that this is a DW node. }
  1448. { $ffffffff -> indicates that this is a DD node. }
  1449. {*********************************************************************}
  1450. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1451. {*********************************************************************}
  1452. var
  1453. strlength: byte;
  1454. asmsym,
  1455. expr: string;
  1456. value : longint;
  1457. Begin
  1458. Repeat
  1459. Case actasmtoken of
  1460. AS_STRING:
  1461. Begin
  1462. if maxvalue = $ff then
  1463. strlength:=1
  1464. else
  1465. Message(asmr_e_string_not_allowed_as_const);
  1466. expr:=actasmpattern;
  1467. if length(expr) > 1 then
  1468. Message(asmr_e_string_not_allowed_as_const);
  1469. Consume(AS_STRING);
  1470. Case actasmtoken of
  1471. AS_COMMA: Consume(AS_COMMA);
  1472. AS_SEPARATOR: ;
  1473. else
  1474. Message(asmr_e_invalid_string_expression);
  1475. end; { end case }
  1476. ConcatString(curlist,expr);
  1477. end;
  1478. AS_INTNUM,
  1479. AS_PLUS,
  1480. AS_MINUS,
  1481. AS_LPAREN,
  1482. AS_NOT,
  1483. AS_ID :
  1484. Begin
  1485. BuildConstSymbolExpression(false,false,false,value,asmsym);
  1486. if asmsym<>'' then
  1487. begin
  1488. if maxvalue<>$ffffffff then
  1489. Comment(V_Warning,'32bit constant created for address');
  1490. ConcatConstSymbol(curlist,asmsym,value)
  1491. end
  1492. else
  1493. ConcatConstant(curlist,value,maxvalue);
  1494. end;
  1495. AS_COMMA:
  1496. Consume(AS_COMMA);
  1497. AS_SEPARATOR:
  1498. break;
  1499. else
  1500. begin
  1501. Message(asmr_e_syn_constant);
  1502. RecoverConsume(false);
  1503. end
  1504. end; { end case }
  1505. Until false;
  1506. end;
  1507. Procedure BuildStringConstant(asciiz: boolean);
  1508. {*********************************************************************}
  1509. { PROCEDURE BuildStringConstant }
  1510. { Description: Takes care of a ASCII, or ASCIIZ directive. }
  1511. { asciiz: boolean -> if true then string will be null terminated. }
  1512. {*********************************************************************}
  1513. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1514. { On ENTRY: Token should point to AS_STRING }
  1515. {*********************************************************************}
  1516. var
  1517. expr: string;
  1518. errorflag : boolean;
  1519. Begin
  1520. errorflag:=FALSE;
  1521. Repeat
  1522. Case actasmtoken of
  1523. AS_STRING:
  1524. Begin
  1525. expr:=actasmpattern;
  1526. if asciiz then
  1527. expr:=expr+#0;
  1528. ConcatPasString(curlist,expr);
  1529. Consume(AS_STRING);
  1530. end;
  1531. AS_COMMA:
  1532. begin
  1533. Consume(AS_COMMA);
  1534. end;
  1535. AS_SEPARATOR:
  1536. begin
  1537. break;
  1538. end;
  1539. else
  1540. Begin
  1541. Consume(actasmtoken);
  1542. if not errorflag then
  1543. Message(asmr_e_invalid_string_expression);
  1544. errorflag:=TRUE;
  1545. end;
  1546. end;
  1547. Until false;
  1548. end;
  1549. Procedure BuildOpCode;
  1550. {*********************************************************************}
  1551. { PROCEDURE BuildOpcode; }
  1552. { Description: Parses the intel opcode and operands, and writes it }
  1553. { in the TInstruction object. }
  1554. {*********************************************************************}
  1555. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1556. { On ENTRY: Token should point to AS_OPCODE }
  1557. {*********************************************************************}
  1558. var
  1559. PrefixOp,OverrideOp: tasmop;
  1560. expr : string;
  1561. Begin
  1562. expr:='';
  1563. PrefixOp:=A_None;
  1564. OverrideOp:=A_None;
  1565. { prefix seg opcode / prefix opcode }
  1566. repeat
  1567. if is_prefix(actopcode) then
  1568. begin
  1569. PrefixOp:=ActOpcode;
  1570. instr.opcode:=ActOpcode;
  1571. instr.condition:=ActCondition;
  1572. instr.opsize:=ActOpsize;
  1573. ConcatInstruction(curlist,instr);
  1574. Consume(AS_OPCODE);
  1575. end
  1576. else
  1577. if is_override(actopcode) then
  1578. begin
  1579. OverrideOp:=ActOpcode;
  1580. instr.opcode:=ActOpcode;
  1581. instr.condition:=ActCondition;
  1582. instr.opsize:=ActOpsize;
  1583. ConcatInstruction(curlist,instr);
  1584. Consume(AS_OPCODE);
  1585. end
  1586. else
  1587. break;
  1588. { allow for newline as in gas styled syntax }
  1589. while actasmtoken=AS_SEPARATOR do
  1590. Consume(AS_SEPARATOR);
  1591. until (actasmtoken<>AS_OPCODE);
  1592. { opcode }
  1593. if (actasmtoken <> AS_OPCODE) then
  1594. Begin
  1595. Message(asmr_e_invalid_or_missing_opcode);
  1596. RecoverConsume(true);
  1597. exit;
  1598. end;
  1599. { Fill the instr object with the current state }
  1600. instr.Opcode:=ActOpcode;
  1601. instr.condition:=ActCondition;
  1602. instr.opsize:=ActOpsize;
  1603. { Valid combination of prefix/override and instruction ? }
  1604. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1605. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1606. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1607. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1608. { We are reading operands, so opcode will be an AS_ID }
  1609. operandnum:=1;
  1610. Consume(AS_OPCODE);
  1611. { Zero operand opcode ? }
  1612. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1613. begin
  1614. operandnum:=0;
  1615. exit;
  1616. end;
  1617. { Read the operands }
  1618. repeat
  1619. case actasmtoken of
  1620. AS_COMMA: { Operand delimiter }
  1621. Begin
  1622. if operandnum > MaxOperands then
  1623. Message(asmr_e_too_many_operands)
  1624. else
  1625. Inc(operandnum);
  1626. Consume(AS_COMMA);
  1627. end;
  1628. AS_SEPARATOR,
  1629. AS_END : { End of asm operands for this opcode }
  1630. begin
  1631. break;
  1632. end;
  1633. else
  1634. BuildOperand(instr);
  1635. end; { end case }
  1636. until false;
  1637. end;
  1638. Function Assemble: Ptree;
  1639. {*********************************************************************}
  1640. { PROCEDURE Assemble; }
  1641. { Description: Parses the att assembler syntax, parsing is done }
  1642. { according to GAs rules. }
  1643. {*********************************************************************}
  1644. Var
  1645. hl : plabel;
  1646. labelptr,
  1647. nextlabel : pasmlabel;
  1648. commname : string;
  1649. lastsec : tsection;
  1650. Begin
  1651. Message1(asmr_d_start_reading,'AT&T');
  1652. firsttoken:=TRUE;
  1653. operandnum:=0;
  1654. if assigned(procinfo.retdef) and
  1655. (is_fpu(procinfo.retdef) or
  1656. ret_in_acc(procinfo.retdef)) then
  1657. procinfo.funcret_is_valid:=true;
  1658. { sets up all opcode and register tables in uppercase }
  1659. if not _asmsorted then
  1660. Begin
  1661. SetupTables;
  1662. _asmsorted:=TRUE;
  1663. end;
  1664. curlist:=new(paasmoutput,init);
  1665. lastsec:=sec_code;
  1666. { setup label linked list }
  1667. labellist.init;
  1668. { start tokenizer }
  1669. c:=current_scanner^.asmgetchar;
  1670. gettoken;
  1671. { main loop }
  1672. repeat
  1673. case actasmtoken of
  1674. AS_LLABEL:
  1675. Begin
  1676. labelptr:=labellist.search(actasmpattern);
  1677. if not assigned(labelptr) then
  1678. Begin
  1679. getlabel(hl);
  1680. labellist.insert(actasmpattern,hl,TRUE);
  1681. ConcatLabel(curlist,hl);
  1682. end
  1683. else
  1684. Begin
  1685. { the label has already been inserted into the }
  1686. { label list, either as an instruction label (in}
  1687. { this case it has not been emitted), or as a }
  1688. { duplicate local symbol (in this case it has }
  1689. { already been emitted). }
  1690. if labelptr^.emitted then
  1691. Message1(asmr_e_dup_local_sym,'.L'+labelptr^.name^)
  1692. else
  1693. Begin
  1694. if assigned(labelptr^.lab) then
  1695. ConcatLabel(curlist,labelptr^.lab);
  1696. labelptr^.emitted:=TRUE;
  1697. end;
  1698. end;
  1699. Consume(AS_LLABEL);
  1700. end;
  1701. AS_LABEL:
  1702. Begin
  1703. { when looking for Pascal labels, these must }
  1704. { be in uppercase. }
  1705. if SearchLabel(upper(actasmpattern),hl) then
  1706. ConcatLabel(curlist,hl)
  1707. else
  1708. Begin
  1709. if (cs_compilesystem in aktmoduleswitches) then
  1710. begin
  1711. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1712. { once again we don't know what it represents }
  1713. { so we simply concatenate it }
  1714. ConcatLocal(curlist,actasmpattern);
  1715. end
  1716. else
  1717. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1718. end;
  1719. Consume(AS_LABEL);
  1720. end;
  1721. AS_DW:
  1722. Begin
  1723. Consume(AS_DW);
  1724. BuildConstant($ffff);
  1725. end;
  1726. AS_DATA:
  1727. Begin
  1728. curlist^.Concat(new(pai_section,init(sec_data)));
  1729. lastsec:=sec_data;
  1730. Consume(AS_DATA);
  1731. end;
  1732. AS_TEXT:
  1733. Begin
  1734. curlist^.Concat(new(pai_section,init(sec_code)));
  1735. lastsec:=sec_code;
  1736. Consume(AS_TEXT);
  1737. end;
  1738. AS_DB:
  1739. Begin
  1740. Consume(AS_DB);
  1741. BuildConstant($ff);
  1742. end;
  1743. AS_DD:
  1744. Begin
  1745. Consume(AS_DD);
  1746. BuildConstant($ffffffff);
  1747. end;
  1748. AS_DQ:
  1749. Begin
  1750. Consume(AS_DQ);
  1751. BuildRealConstant(s64comp);
  1752. end;
  1753. AS_SINGLE:
  1754. Begin
  1755. Consume(AS_SINGLE);
  1756. BuildRealConstant(s32real);
  1757. end;
  1758. AS_DOUBLE:
  1759. Begin
  1760. Consume(AS_DOUBLE);
  1761. BuildRealConstant(s64real);
  1762. end;
  1763. AS_EXTENDED:
  1764. Begin
  1765. Consume(AS_EXTENDED);
  1766. BuildRealConstant(s80real);
  1767. end;
  1768. AS_GLOBAL:
  1769. Begin
  1770. Consume(AS_GLOBAL);
  1771. if actasmtoken=AS_ID then
  1772. ConcatPublic(curlist,actasmpattern);
  1773. Consume(AS_ID);
  1774. if actasmtoken<>AS_SEPARATOR then
  1775. Consume(AS_SEPARATOR);
  1776. end;
  1777. AS_ALIGN:
  1778. Begin
  1779. Consume(AS_ALIGN);
  1780. ConcatAlign(curlist,BuildConstExpression(false,false));
  1781. if actasmtoken<>AS_SEPARATOR then
  1782. Consume(AS_SEPARATOR);
  1783. end;
  1784. AS_ASCIIZ:
  1785. Begin
  1786. Consume(AS_ASCIIZ);
  1787. BuildStringConstant(TRUE);
  1788. end;
  1789. AS_ASCII:
  1790. Begin
  1791. Consume(AS_ASCII);
  1792. BuildStringConstant(FALSE);
  1793. end;
  1794. AS_LCOMM:
  1795. Begin
  1796. Consume(AS_LCOMM);
  1797. commname:=actasmpattern;
  1798. Consume(AS_ID);
  1799. Consume(AS_COMMA);
  1800. ConcatLocalBss(actasmpattern,BuildConstExpression(false,false));
  1801. if actasmtoken<>AS_SEPARATOR then
  1802. Consume(AS_SEPARATOR);
  1803. end;
  1804. AS_COMM:
  1805. Begin
  1806. Consume(AS_LCOMM);
  1807. commname:=actasmpattern;
  1808. Consume(AS_ID);
  1809. Consume(AS_COMMA);
  1810. ConcatGlobalBss(actasmpattern,BuildConstExpression(false,false));
  1811. if actasmtoken<>AS_SEPARATOR then
  1812. Consume(AS_SEPARATOR);
  1813. end;
  1814. AS_OPCODE:
  1815. Begin
  1816. instr.init;
  1817. BuildOpcode;
  1818. instr.ops:=operandnum;
  1819. AddReferenceSizes(instr);
  1820. SetInstructionOpsize(instr);
  1821. CheckOperandSizes(instr);
  1822. ConcatInstruction(curlist,instr);
  1823. instr.done;
  1824. operandnum:=0;
  1825. end;
  1826. AS_SEPARATOR:
  1827. Begin
  1828. Consume(AS_SEPARATOR);
  1829. { let us go back to the first operand }
  1830. operandnum:=0;
  1831. end;
  1832. AS_END:
  1833. begin
  1834. break; { end assembly block }
  1835. end;
  1836. else
  1837. Begin
  1838. Message(asmr_e_syntax_error);
  1839. RecoverConsume(false);
  1840. end;
  1841. end;
  1842. until false;
  1843. { check if there were undefined symbols. }
  1844. { if so, then list each of those undefined }
  1845. { labels. }
  1846. if assigned(labellist.First) then
  1847. Begin
  1848. labelptr:=labellist.First;
  1849. While labelptr <> nil do
  1850. Begin
  1851. nextlabel:=labelptr^.next;
  1852. if not labelptr^.emitted then
  1853. Message1(asmr_e_unknown_label_identifier,'.L'+labelptr^.name^);
  1854. labelptr:=nextlabel;
  1855. end;
  1856. end;
  1857. { are we back in the code section? }
  1858. if lastsec<>sec_code then
  1859. begin
  1860. Message(asmr_w_assembler_code_not_returned_to_text);
  1861. curlist^.Concat(new(pai_section,init(sec_code)));
  1862. end;
  1863. assemble:=genasmnode(curlist);
  1864. labellist.done;
  1865. Message1(asmr_d_finish_reading,'AT&T');
  1866. end;
  1867. {*****************************************************************************
  1868. Initialize
  1869. *****************************************************************************}
  1870. var
  1871. old_exit : pointer;
  1872. procedure ra386att_exit;{$ifndef FPC}far;{$endif}
  1873. begin
  1874. if assigned(iasmops) then
  1875. dispose(iasmops);
  1876. if assigned(iasmregs) then
  1877. dispose(iasmregs);
  1878. exitproc:=old_exit;
  1879. end;
  1880. begin
  1881. old_exit:=exitproc;
  1882. exitproc:=@ra386att_exit;
  1883. end.
  1884. {
  1885. $Log$
  1886. Revision 1.46 1999-05-12 00:19:56 peter
  1887. * removed R_DEFAULT_SEG
  1888. * uniform float names
  1889. Revision 1.45 1999/05/06 09:05:25 peter
  1890. * generic write_float and str_float
  1891. * fixed constant float conversions
  1892. Revision 1.44 1999/05/05 22:22:00 peter
  1893. * updated messages
  1894. Revision 1.43 1999/05/04 21:45:01 florian
  1895. * changes to compile it with Delphi 4.0
  1896. Revision 1.42 1999/05/02 14:25:07 peter
  1897. * only allow *<reg/ref> when call/jmp is used
  1898. Revision 1.41 1999/05/01 13:48:39 peter
  1899. * merged nasm compiler
  1900. Revision 1.7 1999/04/29 09:37:47 peter
  1901. * fixed var+const support
  1902. Revision 1.6 1999/04/26 23:26:17 peter
  1903. * redesigned record offset parsing to support nested records
  1904. * normal compiler uses the redesigned createvarinstr()
  1905. Revision 1.5 1999/04/20 11:01:23 peter
  1906. * better tokenpos info
  1907. Revision 1.4 1999/04/14 09:07:45 peter
  1908. * asm reader improvements
  1909. Revision 1.3 1999/03/06 17:24:26 peter
  1910. * rewritten intel parser a lot, especially reference reading
  1911. * size checking added for asm parsers
  1912. Revision 1.2 1999/03/02 02:56:30 peter
  1913. + stabs support for binary writers
  1914. * more fixes and missing updates from the previous commit :(
  1915. Revision 1.1 1999/03/01 15:46:26 peter
  1916. * ag386bin finally make cycles correct
  1917. * prefixes are now also normal opcodes
  1918. }