rautils.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere and Peter Vreman
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. {$i defines.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasm,cpubase,
  24. symconst,symbase,symtype,symdef;
  25. Const
  26. RPNMax = 10; { I think you only need 4, but just to be safe }
  27. OpMax = 25;
  28. maxoperands = 3; { Maximum operands for assembler instructions }
  29. {---------------------------------------------------------------------
  30. Local Label Management
  31. ---------------------------------------------------------------------}
  32. Type
  33. { Each local label has this structure associated with it }
  34. TLocalLabel = class(TNamedIndexItem)
  35. Emitted : boolean;
  36. constructor Create(const n:string);
  37. function Gettasmlabel:tasmlabel;
  38. private
  39. lab : tasmlabel;
  40. end;
  41. TLocalLabelList = class(TDictionary)
  42. procedure CheckEmitted;
  43. end;
  44. var
  45. LocalLabelList : TLocalLabelList;
  46. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  47. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  48. {---------------------------------------------------------------------
  49. Instruction management
  50. ---------------------------------------------------------------------}
  51. type
  52. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,
  53. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST);
  54. TOprRec = record
  55. case typ:TOprType of
  56. OPR_NONE : ();
  57. OPR_CONSTANT : (val:longint);
  58. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  59. OPR_REFERENCE : (ref:treference);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (reglist:pregisterlist);
  63. {$else not m68k}
  64. OPR_REGLIST : ();
  65. {$endif m68k}
  66. end;
  67. TOperand = class
  68. size : topsize;
  69. hastype, { if the operand has typecasted variable }
  70. hasvar : boolean; { if the operand is loaded with a variable }
  71. opr : TOprRec;
  72. constructor create;
  73. destructor destroy;override;
  74. Procedure BuildOperand;virtual;
  75. Procedure SetSize(_size:longint;force:boolean);
  76. Procedure SetCorrectSize(opcode:tasmop);virtual;
  77. Function SetupResult:boolean;virtual;
  78. Function SetupSelf:boolean;
  79. Function SetupOldEBP:boolean;
  80. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  81. Function SetupDirectVar(const hs:string): Boolean;
  82. Procedure InitRef;
  83. end;
  84. TInstruction = class
  85. opcode : tasmop;
  86. opsize : topsize;
  87. condition : tasmcond;
  88. ops : byte;
  89. labeled : boolean;
  90. operands : array[1..maxoperands] of toperand;
  91. constructor create;
  92. destructor destroy;override;
  93. Procedure InitOperands;virtual;
  94. Procedure BuildOpcode;virtual;
  95. procedure ConcatInstruction(p:TAAsmoutput);virtual;
  96. Procedure Swatoperands;
  97. end;
  98. {---------------------------------------------------------------------}
  99. { Expression parser types }
  100. {---------------------------------------------------------------------}
  101. TExprOperator = record
  102. ch: char; { operator }
  103. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  104. end;
  105. String15 = String[15];
  106. {**********************************************************************}
  107. { The following operators are supported: }
  108. { '+' : addition }
  109. { '-' : subtraction }
  110. { '*' : multiplication }
  111. { '/' : modulo division }
  112. { '^' : exclusive or }
  113. { '<' : shift left }
  114. { '>' : shift right }
  115. { '&' : bitwise and }
  116. { '|' : bitwise or }
  117. { '~' : bitwise complement }
  118. { '%' : modulo division }
  119. { nnn: longint numbers }
  120. { ( and ) parenthesis }
  121. {**********************************************************************}
  122. TExprParse = class
  123. public
  124. Constructor create;
  125. Destructor Destroy;override;
  126. Function Evaluate(Expr: String): longint;
  127. Function Priority(_Operator: Char): longint;
  128. private
  129. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  130. RPNTop : longint;
  131. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  132. OpTop : longint;
  133. Procedure RPNPush(Num: Longint);
  134. Function RPNPop: Longint;
  135. Procedure RPNCalc(token: String15; prefix: boolean);
  136. Procedure OpPush(_Operator: char; prefix: boolean);
  137. { In reality returns TExprOperaotr }
  138. Procedure OpPop(var _Operator:TExprOperator);
  139. end;
  140. { Evaluate an expression string to a longint }
  141. Function CalculateExpression(const expression: string): longint;
  142. {---------------------------------------------------------------------}
  143. { String routines }
  144. {---------------------------------------------------------------------}
  145. Function ValDecimal(const S:String):longint;
  146. Function ValOctal(const S:String):longint;
  147. Function ValBinary(const S:String):longint;
  148. Function ValHexaDecimal(const S:String):longint;
  149. Function PadZero(Var s: String; n: byte): Boolean;
  150. Function EscapeToPascal(const s:string): string;
  151. {---------------------------------------------------------------------
  152. Symbol helper routines
  153. ---------------------------------------------------------------------}
  154. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  155. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  156. Function SearchType(const hs:string): Boolean;
  157. Function SearchRecordType(const s:string): boolean;
  158. Function SearchIConstant(const s:string; var l:longint): boolean;
  159. {---------------------------------------------------------------------
  160. Instruction generation routines
  161. ---------------------------------------------------------------------}
  162. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  163. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  164. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  165. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  166. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  167. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  168. Procedure ConcatString(p : TAAsmoutput;s:string);
  169. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  170. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  171. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  172. Procedure ConcatGlobalBss(const s : string;size : longint);
  173. Procedure ConcatLocalBss(const s : string;size : longint);
  174. Implementation
  175. uses
  176. {$ifdef delphi}
  177. sysutils,
  178. {$else}
  179. strings,
  180. {$endif}
  181. types,systems,verbose,globals,
  182. symsym,symtable,cpuasm
  183. {$ifdef NEWCG}
  184. ,cgbase;
  185. {$else}
  186. ,hcodegen;
  187. {$endif}
  188. {*************************************************************************
  189. TExprParse
  190. *************************************************************************}
  191. Constructor TExprParse.create;
  192. Begin
  193. end;
  194. Procedure TExprParse.RPNPush(Num : longint);
  195. { Add an operand to the top of the RPN stack }
  196. begin
  197. if RPNTop < RPNMax then
  198. begin
  199. Inc(RPNTop);
  200. RPNStack[RPNTop]:=Num;
  201. end
  202. else
  203. Message(asmr_e_expr_illegal);
  204. end;
  205. Function TExprParse.RPNPop : longint;
  206. { Get the operand at the top of the RPN stack }
  207. begin
  208. if RPNTop > 0 then
  209. begin
  210. RPNPop:=RPNStack[RPNTop];
  211. Dec(RPNTop);
  212. end
  213. else
  214. Message(asmr_e_expr_illegal);
  215. end;
  216. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  217. Var
  218. Temp : longint;
  219. n1,n2 : longint;
  220. LocalError : Integer;
  221. begin
  222. { Handle operators }
  223. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  224. Case Token[1] of
  225. '+' :
  226. Begin
  227. if not prefix then
  228. RPNPush(RPNPop + RPNPop);
  229. end;
  230. '-' :
  231. Begin
  232. if prefix then
  233. RPNPush(-(RPNPop))
  234. else
  235. begin
  236. n1:=RPNPop;
  237. n2:=RPNPop;
  238. RPNPush(n2 - n1);
  239. end;
  240. end;
  241. '*' : RPNPush(RPNPop * RPNPop);
  242. '&' :
  243. begin
  244. n1:=RPNPop;
  245. n2:=RPNPop;
  246. RPNPush(n2 and n1);
  247. end;
  248. '|' :
  249. begin
  250. n1:=RPNPop;
  251. n2:=RPNPop;
  252. RPNPush(n2 or n1);
  253. end;
  254. '~' : RPNPush(NOT RPNPop);
  255. '<' :
  256. begin
  257. n1:=RPNPop;
  258. n2:=RPNPop;
  259. RPNPush(n2 SHL n1);
  260. end;
  261. '>' :
  262. begin
  263. n1:=RPNPop;
  264. n2:=RPNPop;
  265. RPNPush(n2 SHR n1);
  266. end;
  267. '%' :
  268. begin
  269. Temp:=RPNPop;
  270. if Temp <> 0 then
  271. RPNPush(RPNPop mod Temp)
  272. else
  273. begin
  274. Message(asmr_e_expr_zero_divide);
  275. { push 1 for error recovery }
  276. RPNPush(1);
  277. end;
  278. end;
  279. '^' : RPNPush(RPNPop XOR RPNPop);
  280. '/' :
  281. begin
  282. Temp:=RPNPop;
  283. if Temp <> 0 then
  284. RPNPush(RPNPop div Temp)
  285. else
  286. begin
  287. Message(asmr_e_expr_zero_divide);
  288. { push 1 for error recovery }
  289. RPNPush(1);
  290. end;
  291. end;
  292. end
  293. else
  294. begin
  295. { Convert String to number and add to stack }
  296. Val(Token, Temp, LocalError);
  297. if LocalError = 0 then
  298. RPNPush(Temp)
  299. else
  300. begin
  301. Message(asmr_e_expr_illegal);
  302. { push 1 for error recovery }
  303. RPNPush(1);
  304. end;
  305. end;
  306. end;
  307. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  308. { Add an operator onto top of the stack }
  309. begin
  310. if OpTop < OpMax then
  311. begin
  312. Inc(OpTop);
  313. OpStack[OpTop].ch:=_Operator;
  314. OpStack[OpTop].is_prefix:=prefix;
  315. end
  316. else
  317. Message(asmr_e_expr_illegal);
  318. end;
  319. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  320. { Get operator at the top of the stack }
  321. begin
  322. if OpTop > 0 then
  323. begin
  324. _Operator:=OpStack[OpTop];
  325. Dec(OpTop);
  326. end
  327. else
  328. Message(asmr_e_expr_illegal);
  329. end;
  330. Function TExprParse.Priority(_Operator : Char) : longint;
  331. { Return priority of operator }
  332. { The greater the priority, the higher the precedence }
  333. begin
  334. Case _Operator OF
  335. '(' :
  336. Priority:=0;
  337. '+', '-' :
  338. Priority:=1;
  339. '*', '/','%','<','>' :
  340. Priority:=2;
  341. '|','&','^','~' :
  342. Priority:=0;
  343. else
  344. Message(asmr_e_expr_illegal);
  345. end;
  346. end;
  347. Function TExprParse.Evaluate(Expr : String):longint;
  348. Var
  349. I : LongInt;
  350. Token : String15;
  351. opr : TExprOperator;
  352. begin
  353. Evaluate:=0;
  354. { Reset stacks }
  355. OpTop :=0;
  356. RPNTop:=0;
  357. Token :='';
  358. { nothing to do ? }
  359. if Expr='' then
  360. exit;
  361. For I:=1 to Length(Expr) DO
  362. begin
  363. if Expr[I] in ['0'..'9'] then
  364. begin { Build multi-digit numbers }
  365. Token:=Token + Expr[I];
  366. if I = Length(Expr) then { Send last one to calculator }
  367. RPNCalc(Token,false);
  368. end
  369. else
  370. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  371. begin
  372. if Token <> '' then
  373. begin { Send last built number to calc. }
  374. RPNCalc(Token,false);
  375. Token:='';
  376. end;
  377. Case Expr[I] OF
  378. '(' : OpPush('(',false);
  379. ')' : begin
  380. While OpStack[OpTop].ch <> '(' DO
  381. Begin
  382. OpPop(opr);
  383. RPNCalc(opr.ch,opr.is_prefix);
  384. end;
  385. OpPop(opr); { Pop off and ignore the '(' }
  386. end;
  387. '+','-','~' : Begin
  388. { workaround for -2147483648 }
  389. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  390. begin
  391. token:='-';
  392. expr[i]:='+';
  393. end;
  394. { if start of expression then surely a prefix }
  395. { or if previous char was also an operator }
  396. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  397. OpPush(Expr[I],true)
  398. else
  399. Begin
  400. { Evaluate all higher priority operators }
  401. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  402. Begin
  403. OpPop(opr);
  404. RPNCalc(opr.ch,opr.is_prefix);
  405. end;
  406. OpPush(Expr[I],false);
  407. End;
  408. end;
  409. '*', '/',
  410. '^','|','&',
  411. '%','<','>' : begin
  412. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  413. Begin
  414. OpPop(opr);
  415. RPNCalc(opr.ch,opr.is_prefix);
  416. end;
  417. OpPush(Expr[I],false);
  418. end;
  419. end; { Case }
  420. end
  421. else
  422. Message(asmr_e_expr_illegal); { Handle bad input error }
  423. end;
  424. { Pop off the remaining operators }
  425. While OpTop > 0 do
  426. Begin
  427. OpPop(opr);
  428. RPNCalc(opr.ch,opr.is_prefix);
  429. end;
  430. { The result is stored on the top of the stack }
  431. Evaluate:=RPNPop;
  432. end;
  433. Destructor TExprParse.Destroy;
  434. Begin
  435. end;
  436. Function CalculateExpression(const expression: string): longint;
  437. var
  438. expr: TExprParse;
  439. Begin
  440. expr:=TExprParse.create;
  441. CalculateExpression:=expr.Evaluate(expression);
  442. expr.Free;
  443. end;
  444. {*************************************************************************}
  445. { String conversions/utils }
  446. {*************************************************************************}
  447. Function EscapeToPascal(const s:string): string;
  448. { converts a C styled string - which contains escape }
  449. { characters to a pascal style string. }
  450. var
  451. i,len : longint;
  452. hs : string;
  453. temp : string;
  454. c : char;
  455. Begin
  456. hs:='';
  457. len:=0;
  458. i:=0;
  459. while (i<length(s)) and (len<255) do
  460. begin
  461. Inc(i);
  462. if (s[i]='\') and (i<length(s)) then
  463. Begin
  464. inc(i);
  465. case s[i] of
  466. '\' :
  467. c:='\';
  468. 'b':
  469. c:=#8;
  470. 'f':
  471. c:=#12;
  472. 'n':
  473. c:=#10;
  474. 'r':
  475. c:=#13;
  476. 't':
  477. c:=#9;
  478. '"':
  479. c:='"';
  480. '0'..'7':
  481. Begin
  482. temp:=s[i];
  483. temp:=temp+s[i+1];
  484. temp:=temp+s[i+2];
  485. inc(i,2);
  486. c:=chr(ValOctal(temp));
  487. end;
  488. 'x':
  489. Begin
  490. temp:=s[i+1];
  491. temp:=temp+s[i+2];
  492. inc(i,2);
  493. c:=chr(ValHexaDecimal(temp));
  494. end;
  495. else
  496. Begin
  497. Message1(asmr_e_escape_seq_ignored,s[i]);
  498. c:=s[i];
  499. end;
  500. end;
  501. end
  502. else
  503. c:=s[i];
  504. inc(len);
  505. hs[len]:=c;
  506. end;
  507. hs[0]:=chr(len);
  508. EscapeToPascal:=hs;
  509. end;
  510. Function ValDecimal(const S:String):longint;
  511. { Converts a decimal string to longint }
  512. var
  513. vs,c : longint;
  514. Begin
  515. vs:=0;
  516. for c:=1 to length(s) do
  517. begin
  518. vs:=vs*10;
  519. if s[c] in ['0'..'9'] then
  520. inc(vs,ord(s[c])-ord('0'))
  521. else
  522. begin
  523. Message1(asmr_e_error_converting_decimal,s);
  524. ValDecimal:=0;
  525. exit;
  526. end;
  527. end;
  528. ValDecimal:=vs;
  529. end;
  530. Function ValOctal(const S:String):longint;
  531. { Converts an octal string to longint }
  532. var
  533. vs,c : longint;
  534. Begin
  535. vs:=0;
  536. for c:=1 to length(s) do
  537. begin
  538. vs:=vs shl 3;
  539. if s[c] in ['0'..'7'] then
  540. inc(vs,ord(s[c])-ord('0'))
  541. else
  542. begin
  543. Message1(asmr_e_error_converting_octal,s);
  544. ValOctal:=0;
  545. exit;
  546. end;
  547. end;
  548. ValOctal:=vs;
  549. end;
  550. Function ValBinary(const S:String):longint;
  551. { Converts a binary string to longint }
  552. var
  553. vs,c : longint;
  554. Begin
  555. vs:=0;
  556. for c:=1 to length(s) do
  557. begin
  558. vs:=vs shl 1;
  559. if s[c] in ['0'..'1'] then
  560. inc(vs,ord(s[c])-ord('0'))
  561. else
  562. begin
  563. Message1(asmr_e_error_converting_binary,s);
  564. ValBinary:=0;
  565. exit;
  566. end;
  567. end;
  568. ValBinary:=vs;
  569. end;
  570. Function ValHexadecimal(const S:String):longint;
  571. { Converts a binary string to longint }
  572. var
  573. vs,c : longint;
  574. Begin
  575. vs:=0;
  576. for c:=1 to length(s) do
  577. begin
  578. vs:=vs shl 4;
  579. case s[c] of
  580. '0'..'9' :
  581. inc(vs,ord(s[c])-ord('0'));
  582. 'A'..'F' :
  583. inc(vs,ord(s[c])-ord('A')+10);
  584. 'a'..'f' :
  585. inc(vs,ord(s[c])-ord('a')+10);
  586. else
  587. begin
  588. Message1(asmr_e_error_converting_hexadecimal,s);
  589. ValHexadecimal:=0;
  590. exit;
  591. end;
  592. end;
  593. end;
  594. ValHexadecimal:=vs;
  595. end;
  596. Function PadZero(Var s: String; n: byte): Boolean;
  597. Begin
  598. PadZero:=TRUE;
  599. { Do some error checking first }
  600. if Length(s) = n then
  601. exit
  602. else
  603. if Length(s) > n then
  604. Begin
  605. PadZero:=FALSE;
  606. delete(s,n+1,length(s));
  607. exit;
  608. end
  609. else
  610. PadZero:=TRUE;
  611. { Fill it up with the specified character }
  612. fillchar(s[length(s)+1],n-1,#0);
  613. s[0]:=chr(n);
  614. end;
  615. {****************************************************************************
  616. TOperand
  617. ****************************************************************************}
  618. constructor TOperand.Create;
  619. begin
  620. size:=S_NO;
  621. hastype:=false;
  622. hasvar:=false;
  623. FillChar(Opr,sizeof(Opr),0);
  624. end;
  625. destructor TOperand.destroy;
  626. begin
  627. end;
  628. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  629. begin
  630. end;
  631. Procedure TOperand.SetSize(_size:longint;force:boolean);
  632. begin
  633. if force or
  634. ((size = S_NO) and (_size<=extended_size)) then
  635. Begin
  636. case _size of
  637. 1 : size:=S_B;
  638. 2 : size:=S_W{ could be S_IS};
  639. 4 : size:=S_L{ could be S_IL or S_FS};
  640. 8 : size:=S_IQ{ could be S_D or S_FL};
  641. extended_size : size:=S_FX;
  642. end;
  643. end;
  644. end;
  645. Function TOperand.SetupResult:boolean;
  646. Begin
  647. SetupResult:=false;
  648. { replace by correct offset. }
  649. if assigned(procinfo^.returntype.def) and
  650. (not is_void(procinfo^.returntype.def)) then
  651. begin
  652. if (procinfo^.return_offset=0) and ((m_tp in aktmodeswitches) or
  653. (m_delphi in aktmodeswitches)) then
  654. begin
  655. Message(asmr_e_cannot_use_RESULT_here);
  656. exit;
  657. end;
  658. opr.ref.offset:=procinfo^.return_offset;
  659. opr.ref.base:= procinfo^.framepointer;
  660. opr.ref.options:=ref_parafixup;
  661. { always assume that the result is valid. }
  662. procinfo^.funcret_state:=vs_assigned;
  663. SetupResult:=true;
  664. end
  665. else
  666. Message(asmr_e_void_function);
  667. end;
  668. Function TOperand.SetupSelf:boolean;
  669. Begin
  670. SetupSelf:=false;
  671. if assigned(procinfo^._class) then
  672. Begin
  673. opr.typ:=OPR_REFERENCE;
  674. opr.ref.offset:=procinfo^.selfpointer_offset;
  675. opr.ref.base:=procinfo^.framepointer;
  676. opr.ref.options:=ref_selffixup;
  677. SetupSelf:=true;
  678. end
  679. else
  680. Message(asmr_e_cannot_use_SELF_outside_a_method);
  681. end;
  682. Function TOperand.SetupOldEBP:boolean;
  683. Begin
  684. SetupOldEBP:=false;
  685. if lexlevel>normal_function_level then
  686. Begin
  687. opr.typ:=OPR_REFERENCE;
  688. opr.ref.offset:=procinfo^.framepointer_offset;
  689. opr.ref.base:=procinfo^.framepointer;
  690. opr.ref.options:=ref_parafixup;
  691. SetupOldEBP:=true;
  692. end
  693. else
  694. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  695. end;
  696. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  697. { search and sets up the correct fields in the Instr record }
  698. { for the NON-constant identifier passed to the routine. }
  699. { if not found returns FALSE. }
  700. var
  701. sym : tsym;
  702. srsymtable : tsymtable;
  703. harrdef : tarraydef;
  704. Begin
  705. SetupVar:=false;
  706. asmsearchsym(s,sym,srsymtable);
  707. if sym = nil then
  708. exit;
  709. case sym.typ of
  710. varsym :
  711. begin
  712. { we always assume in asm statements that }
  713. { that the variable is valid. }
  714. tvarsym(sym).varstate:=vs_used;
  715. inc(tvarsym(sym).refs);
  716. case tvarsym(sym).owner.symtabletype of
  717. objectsymtable :
  718. begin
  719. { this is not allowed, because we don't know if the self
  720. register is still free, and loading it first is also
  721. not possible, because this could break code }
  722. { Be TP/Delphi compatible in Delphi or TP modes }
  723. if (m_tp in aktmodeswitches) then
  724. begin
  725. opr.typ:=OPR_CONSTANT;
  726. opr.val:=tvarsym(sym).address;
  727. end
  728. { I do not agree here people using method vars should ensure
  729. that %esi is valid there }
  730. else
  731. begin
  732. opr.ref.base:=self_pointer;
  733. opr.ref.offset:=tvarsym(sym).address;
  734. end;
  735. hasvar:=true;
  736. SetupVar:=true;
  737. Exit;
  738. end;
  739. globalsymtable,
  740. staticsymtable :
  741. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname);
  742. parasymtable :
  743. begin
  744. { if we only want the offset we don't have to care
  745. the base will be zeroed after ! }
  746. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  747. { this below is wrong because there are two parast
  748. for global functions one of interface the second of
  749. implementation
  750. if (tvarsym(sym).owner=procinfo^.def.parast) or }
  751. GetOffset then
  752. begin
  753. opr.ref.base:=procinfo^.framepointer;
  754. end
  755. else
  756. begin
  757. if (procinfo^.framepointer=stack_pointer) and
  758. assigned(procinfo^.parent) and
  759. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  760. { same problem as above !!
  761. (procinfo^.parent^.sym.definition.parast=tvarsym(sym).owner) and }
  762. (lexlevel>normal_function_level) then
  763. opr.ref.base:=procinfo^.parent^.framepointer
  764. else
  765. message1(asmr_e_local_para_unreachable,s);
  766. end;
  767. opr.ref.offset:=tvarsym(sym).address;
  768. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  769. begin
  770. opr.ref.offsetfixup:=aktprocsym.definition.parast.address_fixup;
  771. opr.ref.options:=ref_parafixup;
  772. end
  773. else
  774. begin
  775. opr.ref.offsetfixup:=0;
  776. opr.ref.options:=ref_none;
  777. end;
  778. if (tvarsym(sym).varspez=vs_var) or
  779. ((tvarsym(sym).varspez=vs_const) and
  780. push_addr_param(tvarsym(sym).vartype.def)) then
  781. SetSize(target_info.size_of_pointer,false);
  782. end;
  783. localsymtable :
  784. begin
  785. if (vo_is_external in tvarsym(sym).varoptions) then
  786. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname)
  787. else
  788. begin
  789. { if we only want the offset we don't have to care
  790. the base will be zeroed after ! }
  791. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  792. {if (tvarsym(sym).owner=procinfo^.def.localst) or}
  793. GetOffset then
  794. opr.ref.base:=procinfo^.framepointer
  795. else
  796. begin
  797. if (procinfo^.framepointer=stack_pointer) and
  798. assigned(procinfo^.parent) and
  799. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  800. {(procinfo^.parent^.sym.definition.localst=tvarsym(sym).owner) and}
  801. (lexlevel>normal_function_level) then
  802. opr.ref.base:=procinfo^.parent^.framepointer
  803. else
  804. message1(asmr_e_local_para_unreachable,s);
  805. end;
  806. opr.ref.offset:=-(tvarsym(sym).address);
  807. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  808. begin
  809. opr.ref.offsetfixup:=aktprocsym.definition.localst.address_fixup;
  810. opr.ref.options:=ref_localfixup;
  811. end
  812. else
  813. begin
  814. opr.ref.offsetfixup:=0;
  815. opr.ref.options:=ref_none;
  816. end;
  817. end;
  818. if (tvarsym(sym).varspez in [vs_var,vs_out]) or
  819. ((tvarsym(sym).varspez=vs_const) and
  820. push_addr_param(tvarsym(sym).vartype.def)) then
  821. SetSize(target_info.size_of_pointer,false);
  822. end;
  823. end;
  824. case tvarsym(sym).vartype.def.deftype of
  825. orddef,
  826. enumdef,
  827. pointerdef,
  828. floatdef :
  829. SetSize(tvarsym(sym).getsize,false);
  830. arraydef :
  831. begin
  832. { for arrays try to get the element size, take care of
  833. multiple indexes }
  834. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  835. while assigned(harrdef.elementtype.def) and
  836. (harrdef.elementtype.def.deftype=arraydef) do
  837. harrdef:=tarraydef(harrdef.elementtype.def);
  838. SetSize(harrdef.elesize,false);
  839. end;
  840. end;
  841. hasvar:=true;
  842. SetupVar:=true;
  843. Exit;
  844. end;
  845. typedconstsym :
  846. begin
  847. opr.ref.symbol:=newasmsymbol(ttypedconstsym(sym).mangledname);
  848. case ttypedconstsym(sym).typedconsttype.def.deftype of
  849. orddef,
  850. enumdef,
  851. pointerdef,
  852. floatdef :
  853. SetSize(ttypedconstsym(sym).getsize,false);
  854. arraydef :
  855. begin
  856. { for arrays try to get the element size, take care of
  857. multiple indexes }
  858. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  859. while assigned(harrdef.elementtype.def) and
  860. (harrdef.elementtype.def.deftype=arraydef) do
  861. harrdef:=tarraydef(harrdef.elementtype.def);
  862. SetSize(harrdef.elesize,false);
  863. end;
  864. end;
  865. hasvar:=true;
  866. SetupVar:=true;
  867. Exit;
  868. end;
  869. constsym :
  870. begin
  871. if tconstsym(sym).consttyp in [constint,constchar,constbool] then
  872. begin
  873. opr.typ:=OPR_CONSTANT;
  874. opr.val:=tconstsym(sym).value;
  875. SetupVar:=true;
  876. Exit;
  877. end;
  878. end;
  879. typesym :
  880. begin
  881. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  882. begin
  883. opr.typ:=OPR_CONSTANT;
  884. opr.val:=0;
  885. SetupVar:=TRUE;
  886. Exit;
  887. end;
  888. end;
  889. procsym :
  890. begin
  891. if assigned(tprocsym(sym).definition.nextoverloaded) then
  892. Message(asmr_w_calling_overload_func);
  893. opr.typ:=OPR_SYMBOL;
  894. opr.symbol:=newasmsymbol(tprocsym(sym).definition.mangledname);
  895. hasvar:=true;
  896. SetupVar:=TRUE;
  897. Exit;
  898. end;
  899. else
  900. begin
  901. Message(asmr_e_unsupported_symbol_type);
  902. exit;
  903. end;
  904. end;
  905. end;
  906. { looks for internal names of variables and routines }
  907. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  908. var
  909. p : tasmsymbol;
  910. begin
  911. SetupDirectVar:=false;
  912. p:=getasmsymbol(hs);
  913. if assigned(p) then
  914. begin
  915. opr.ref.symbol:=p;
  916. hasvar:=true;
  917. SetupDirectVar:=true;
  918. end;
  919. end;
  920. procedure TOperand.InitRef;
  921. {*********************************************************************}
  922. { Description: This routine first check if the opcode is of }
  923. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  924. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  925. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  926. { to point to the default segment. }
  927. {*********************************************************************}
  928. Begin
  929. case opr.typ of
  930. OPR_REFERENCE:
  931. exit;
  932. OPR_NONE: ;
  933. else
  934. Message(asmr_e_invalid_operand_type);
  935. end;
  936. opr.typ := OPR_REFERENCE;
  937. reset_reference(opr.ref);
  938. end;
  939. procedure TOperand.BuildOperand;
  940. begin
  941. abstract;
  942. end;
  943. {****************************************************************************
  944. TInstruction
  945. ****************************************************************************}
  946. constructor TInstruction.create;
  947. Begin
  948. Opcode:=A_NONE;
  949. Opsize:=S_NO;
  950. Condition:=C_NONE;
  951. Ops:=0;
  952. InitOperands;
  953. Labeled:=false;
  954. end;
  955. destructor TInstruction.destroy;
  956. var
  957. i : longint;
  958. Begin
  959. for i:=1 to 3 do
  960. Operands[i].free;
  961. end;
  962. procedure TInstruction.InitOperands;
  963. var
  964. i : longint;
  965. begin
  966. for i:=1 to 3 do
  967. Operands[i].create;
  968. end;
  969. Procedure TInstruction.Swatoperands;
  970. Var
  971. p : toperand;
  972. Begin
  973. case Ops of
  974. 2 :
  975. begin
  976. p:=Operands[1];
  977. Operands[1]:=Operands[2];
  978. Operands[2]:=p;
  979. end;
  980. 3 :
  981. begin
  982. p:=Operands[1];
  983. Operands[1]:=Operands[3];
  984. Operands[3]:=p;
  985. end;
  986. end;
  987. end;
  988. procedure TInstruction.ConcatInstruction(p:TAAsmOutput);
  989. begin
  990. abstract;
  991. end;
  992. procedure TInstruction.BuildOpcode;
  993. begin
  994. abstract;
  995. end;
  996. {***************************************************************************
  997. TLocalLabel
  998. ***************************************************************************}
  999. constructor TLocalLabel.create(const n:string);
  1000. begin
  1001. inherited CreateName(n);
  1002. lab:=nil;
  1003. emitted:=false;
  1004. end;
  1005. function TLocalLabel.Gettasmlabel:tasmlabel;
  1006. begin
  1007. if not assigned(lab) then
  1008. begin
  1009. getlabel(lab);
  1010. { this label is forced to be used so it's always written }
  1011. inc(lab.refs);
  1012. end;
  1013. Gettasmlabel:=lab;
  1014. end;
  1015. {***************************************************************************
  1016. TLocalLabelList
  1017. ***************************************************************************}
  1018. procedure LocalLabelEmitted(p:tnamedindexitem);
  1019. begin
  1020. if not TLocalLabel(p).emitted then
  1021. Message1(asmr_e_unknown_label_identifier,p.name);
  1022. end;
  1023. procedure TLocalLabelList.CheckEmitted;
  1024. begin
  1025. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted)
  1026. end;
  1027. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1028. var
  1029. lab : TLocalLabel;
  1030. Begin
  1031. CreateLocalLabel:=true;
  1032. { Check if it already is defined }
  1033. lab:=TLocalLabel(LocalLabellist.Search(s));
  1034. if not assigned(lab) then
  1035. begin
  1036. lab:=TLocalLabel.Create(s);
  1037. LocalLabellist.Insert(lab);
  1038. end;
  1039. { set emitted flag and check for dup syms }
  1040. if emit then
  1041. begin
  1042. if lab.Emitted then
  1043. begin
  1044. Message1(asmr_e_dup_local_sym,lab.Name);
  1045. CreateLocalLabel:=false;
  1046. end;
  1047. lab.Emitted:=true;
  1048. end;
  1049. hl:=lab.Gettasmlabel;
  1050. end;
  1051. {****************************************************************************
  1052. Symbol table helper routines
  1053. ****************************************************************************}
  1054. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1055. var
  1056. i : integer;
  1057. begin
  1058. i:=pos('.',s);
  1059. { allow unit.identifier }
  1060. if i>0 then
  1061. begin
  1062. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1063. if assigned(srsym) then
  1064. begin
  1065. if (srsym.typ=unitsym) and
  1066. (srsym.owner.unitid=0) then
  1067. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1068. else
  1069. srsym:=nil;
  1070. end;
  1071. end
  1072. else
  1073. searchsym(s,srsym,srsymtable);
  1074. end;
  1075. Function SearchType(const hs:string): Boolean;
  1076. var
  1077. srsym : tsym;
  1078. srsymtable : tsymtable;
  1079. begin
  1080. asmsearchsym(hs,srsym,srsymtable);
  1081. SearchType:=assigned(srsym) and
  1082. (srsym.typ=typesym);
  1083. end;
  1084. Function SearchRecordType(const s:string): boolean;
  1085. var
  1086. srsym : tsym;
  1087. srsymtable : tsymtable;
  1088. Begin
  1089. SearchRecordType:=false;
  1090. { Check the constants in symtable }
  1091. asmsearchsym(s,srsym,srsymtable);
  1092. if srsym <> nil then
  1093. Begin
  1094. case srsym.typ of
  1095. typesym :
  1096. begin
  1097. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1098. begin
  1099. SearchRecordType:=true;
  1100. exit;
  1101. end;
  1102. end;
  1103. end;
  1104. end;
  1105. end;
  1106. Function SearchIConstant(const s:string; var l:longint): boolean;
  1107. {**********************************************************************}
  1108. { Description: Searches for a CONSTANT of name s in either the local }
  1109. { symbol list, then in the global symbol list, and returns the value }
  1110. { of that constant in l. Returns TRUE if successfull, if not found, }
  1111. { or if the constant is not of correct type, then returns FALSE }
  1112. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1113. { respectively. }
  1114. {**********************************************************************}
  1115. var
  1116. srsym : tsym;
  1117. srsymtable : tsymtable;
  1118. Begin
  1119. SearchIConstant:=false;
  1120. { check for TRUE or FALSE reserved words first }
  1121. if s = 'TRUE' then
  1122. Begin
  1123. SearchIConstant:=TRUE;
  1124. l:=1;
  1125. exit;
  1126. end;
  1127. if s = 'FALSE' then
  1128. Begin
  1129. SearchIConstant:=TRUE;
  1130. l:=0;
  1131. exit;
  1132. end;
  1133. { Check the constants in symtable }
  1134. asmsearchsym(s,srsym,srsymtable);
  1135. if srsym <> nil then
  1136. Begin
  1137. case srsym.typ of
  1138. constsym :
  1139. begin
  1140. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1141. Begin
  1142. l:=tconstsym(srsym).value;
  1143. SearchIConstant:=TRUE;
  1144. exit;
  1145. end;
  1146. end;
  1147. enumsym:
  1148. Begin
  1149. l:=tenumsym(srsym).value;
  1150. SearchIConstant:=TRUE;
  1151. exit;
  1152. end;
  1153. end;
  1154. end;
  1155. end;
  1156. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1157. { search and returns the offset and size of records/objects of the base }
  1158. { with field name setup in field. }
  1159. { returns FALSE if not found. }
  1160. { used when base is a variable or a typed constant name. }
  1161. var
  1162. st : tsymtable;
  1163. harrdef : tarraydef;
  1164. sym : tsym;
  1165. srsymtable : tsymtable;
  1166. i : longint;
  1167. base : string;
  1168. Begin
  1169. GetRecordOffsetSize:=FALSE;
  1170. Offset:=0;
  1171. Size:=0;
  1172. i:=pos('.',s);
  1173. if i=0 then
  1174. i:=255;
  1175. base:=Copy(s,1,i-1);
  1176. delete(s,1,i);
  1177. if base='SELF' then
  1178. st:=procinfo^._class.symtable
  1179. else
  1180. begin
  1181. asmsearchsym(base,sym,srsymtable);
  1182. st:=nil;
  1183. { we can start with a var,type,typedconst }
  1184. case sym.typ of
  1185. varsym :
  1186. begin
  1187. case tvarsym(sym).vartype.def.deftype of
  1188. recorddef :
  1189. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1190. objectdef :
  1191. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1192. end;
  1193. end;
  1194. typesym :
  1195. begin
  1196. case ttypesym(sym).restype.def.deftype of
  1197. recorddef :
  1198. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1199. objectdef :
  1200. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1201. end;
  1202. end;
  1203. typedconstsym :
  1204. begin
  1205. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1206. recorddef :
  1207. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1208. objectdef :
  1209. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1210. end;
  1211. end;
  1212. end;
  1213. end;
  1214. { now walk all recordsymtables }
  1215. while assigned(st) and (s<>'') do
  1216. begin
  1217. { load next field in base }
  1218. i:=pos('.',s);
  1219. if i=0 then
  1220. i:=255;
  1221. base:=Copy(s,1,i-1);
  1222. delete(s,1,i);
  1223. if st.symtabletype=objectsymtable then
  1224. sym:=search_class_member(tobjectdef(st.defowner),base)
  1225. else
  1226. sym:=tsym(st.search(base));
  1227. if not assigned(sym) then
  1228. begin
  1229. GetRecordOffsetSize:=false;
  1230. exit;
  1231. end;
  1232. st:=nil;
  1233. case sym.typ of
  1234. varsym :
  1235. begin
  1236. inc(Offset,tvarsym(sym).address);
  1237. Size:=tvarsym(sym).getsize;
  1238. case tvarsym(sym).vartype.def.deftype of
  1239. arraydef :
  1240. begin
  1241. { for arrays try to get the element size, take care of
  1242. multiple indexes }
  1243. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1244. while assigned(harrdef.elementtype.def) and
  1245. (harrdef.elementtype.def.deftype=arraydef) do
  1246. harrdef:=tarraydef(harrdef.elementtype.def);
  1247. size:=harrdef.elesize;
  1248. end;
  1249. recorddef :
  1250. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1251. objectdef :
  1252. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1253. end;
  1254. end;
  1255. end;
  1256. end;
  1257. GetRecordOffsetSize:=(s='');
  1258. end;
  1259. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1260. var
  1261. sym : tsym;
  1262. srsymtable : tsymtable;
  1263. hs : string;
  1264. Begin
  1265. hl:=nil;
  1266. SearchLabel:=false;
  1267. { Check for pascal labels, which are case insensetive }
  1268. hs:=upper(s);
  1269. asmsearchsym(hs,sym,srsymtable);
  1270. if sym=nil then
  1271. exit;
  1272. case sym.typ of
  1273. labelsym :
  1274. begin
  1275. hl:=tlabelsym(sym).lab;
  1276. if emit then
  1277. tlabelsym(sym).defined:=true
  1278. else
  1279. tlabelsym(sym).used:=true;
  1280. SearchLabel:=true;
  1281. exit;
  1282. end;
  1283. end;
  1284. end;
  1285. {*************************************************************************}
  1286. { Instruction Generation Utilities }
  1287. {*************************************************************************}
  1288. Procedure ConcatString(p : TAAsmoutput;s:string);
  1289. {*********************************************************************}
  1290. { PROCEDURE ConcatString(s:string); }
  1291. { Description: This routine adds the character chain pointed to in }
  1292. { s to the instruction linked list. }
  1293. {*********************************************************************}
  1294. Var
  1295. pc: PChar;
  1296. Begin
  1297. getmem(pc,length(s)+1);
  1298. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1299. end;
  1300. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1301. {*********************************************************************}
  1302. { PROCEDURE ConcatPasString(s:string); }
  1303. { Description: This routine adds the character chain pointed to in }
  1304. { s to the instruction linked list, contrary to ConcatString it }
  1305. { uses a pascal style string, so it conserves null characters. }
  1306. {*********************************************************************}
  1307. Begin
  1308. p.concat(Tai_string.Create(s));
  1309. end;
  1310. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1311. {*********************************************************************}
  1312. { PROCEDURE ConcatDirect(s:string) }
  1313. { Description: This routine output the string directly to the asm }
  1314. { output, it is only sed when writing special labels in AT&T mode, }
  1315. { and should not be used without due consideration, since it may }
  1316. { cause problems. }
  1317. {*********************************************************************}
  1318. Var
  1319. pc: PChar;
  1320. Begin
  1321. getmem(pc,length(s)+1);
  1322. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1323. end;
  1324. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1325. {*********************************************************************}
  1326. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1327. { Description: This routine adds the value constant to the current }
  1328. { instruction linked list. }
  1329. { maxvalue -> indicates the size of the data to initialize: }
  1330. { $ff -> create a byte node. }
  1331. { $ffff -> create a word node. }
  1332. { $ffffffff -> create a dword node. }
  1333. {*********************************************************************}
  1334. Begin
  1335. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1336. Begin
  1337. Message(asmr_e_constant_out_of_bounds);
  1338. { assuming a value of maxvalue }
  1339. value:=maxvalue;
  1340. end;
  1341. if maxvalue = $ff then
  1342. p.concat(Tai_const.Create_8bit(byte(value)))
  1343. else
  1344. if maxvalue = $ffff then
  1345. p.concat(Tai_const.Create_16bit(word(value)))
  1346. else
  1347. if maxvalue = longint($ffffffff) then
  1348. p.concat(Tai_const.Create_32bit(longint(value)));
  1349. end;
  1350. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1351. begin
  1352. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1353. end;
  1354. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1355. {***********************************************************************}
  1356. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1357. { Description: This routine adds the value constant to the current }
  1358. { instruction linked list. }
  1359. { real_typ -> indicates the type of the real data to initialize: }
  1360. { s32real -> create a single node. }
  1361. { s64real -> create a double node. }
  1362. { s80real -> create an extended node. }
  1363. { s64bit -> create a comp node. }
  1364. { f32bit -> create a fixed node. (not used normally) }
  1365. {***********************************************************************}
  1366. Begin
  1367. case real_typ of
  1368. s32real : p.concat(Tai_real_32bit.Create(value));
  1369. s64real : p.concat(Tai_real_64bit.Create(value));
  1370. s80real : p.concat(Tai_real_80bit.Create(value));
  1371. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1372. end;
  1373. end;
  1374. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1375. {*********************************************************************}
  1376. { PROCEDURE ConcatLabel }
  1377. { Description: This routine either emits a label or a labeled }
  1378. { instruction to the linked list of instructions. }
  1379. {*********************************************************************}
  1380. begin
  1381. p.concat(Tai_label.Create(l));
  1382. end;
  1383. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1384. {*********************************************************************}
  1385. { PROCEDURE ConcatPublic }
  1386. { Description: This routine emits an global definition to the }
  1387. { linked list of instructions.(used by AT&T styled asm) }
  1388. {*********************************************************************}
  1389. begin
  1390. p.concat(Tai_align.Create(l));
  1391. end;
  1392. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1393. {*********************************************************************}
  1394. { PROCEDURE ConcatPublic }
  1395. { Description: This routine emits an global definition to the }
  1396. { linked list of instructions.(used by AT&T styled asm) }
  1397. {*********************************************************************}
  1398. begin
  1399. p.concat(Tai_symbol.Createname_global(s,0));
  1400. end;
  1401. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1402. {*********************************************************************}
  1403. { PROCEDURE ConcatLocal }
  1404. { Description: This routine emits an local definition to the }
  1405. { linked list of instructions. }
  1406. {*********************************************************************}
  1407. begin
  1408. p.concat(Tai_symbol.Createname(s,0));
  1409. end;
  1410. Procedure ConcatGlobalBss(const s : string;size : longint);
  1411. {*********************************************************************}
  1412. { PROCEDURE ConcatGlobalBss }
  1413. { Description: This routine emits an global datablock to the }
  1414. { linked list of instructions. }
  1415. {*********************************************************************}
  1416. begin
  1417. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1418. end;
  1419. Procedure ConcatLocalBss(const s : string;size : longint);
  1420. {*********************************************************************}
  1421. { PROCEDURE ConcatLocalBss }
  1422. { Description: This routine emits a local datablcok to the }
  1423. { linked list of instructions. }
  1424. {*********************************************************************}
  1425. begin
  1426. bssSegment.concat(Tai_datablock.Create(s,size));
  1427. end;
  1428. end.
  1429. {
  1430. $Log$
  1431. Revision 1.20 2001-04-18 22:01:58 peter
  1432. * registration of targets and assemblers
  1433. Revision 1.19 2001/04/13 20:06:05 peter
  1434. * allow unit.identifier in asm readers
  1435. Revision 1.18 2001/04/13 01:22:13 peter
  1436. * symtable change to classes
  1437. * range check generation and errors fixed, make cycle DEBUG=1 works
  1438. * memory leaks fixed
  1439. Revision 1.17 2001/04/02 21:20:34 peter
  1440. * resulttype rewrite
  1441. Revision 1.16 2001/03/11 22:58:50 peter
  1442. * getsym redesign, removed the globals srsym,srsymtable
  1443. Revision 1.15 2001/02/26 19:44:54 peter
  1444. * merged generic m68k updates from fixes branch
  1445. Revision 1.14 2000/12/25 00:07:28 peter
  1446. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1447. tlinkedlist objects)
  1448. Revision 1.13 2000/12/07 17:19:43 jonas
  1449. * new constant handling: from now on, hex constants >$7fffffff are
  1450. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1451. and became $ffffffff80000000), all constants in the longint range
  1452. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1453. are cardinals and the rest are int64's.
  1454. * added lots of longint typecast to prevent range check errors in the
  1455. compiler and rtl
  1456. * type casts of symbolic ordinal constants are now preserved
  1457. * fixed bug where the original resulttype wasn't restored correctly
  1458. after doing a 64bit rangecheck
  1459. Revision 1.12 2000/11/29 00:30:38 florian
  1460. * unused units removed from uses clause
  1461. * some changes for widestrings
  1462. Revision 1.11 2000/11/06 22:30:30 peter
  1463. * more fixes
  1464. Revision 1.10 2000/11/04 14:25:21 florian
  1465. + merged Attila's changes for interfaces, not tested yet
  1466. Revision 1.9 2000/10/31 22:30:13 peter
  1467. * merged asm result patch part 2
  1468. Revision 1.8 2000/10/31 22:02:51 peter
  1469. * symtable splitted, no real code changes
  1470. Revision 1.7 2000/10/08 10:26:33 peter
  1471. * merged @result fix from Pierre
  1472. Revision 1.6 2000/09/24 21:19:51 peter
  1473. * delphi compile fixes
  1474. Revision 1.5 2000/09/24 15:06:26 peter
  1475. * use defines.inc
  1476. Revision 1.4 2000/08/27 16:11:52 peter
  1477. * moved some util functions from globals,cobjects to cutils
  1478. * splitted files into finput,fmodule
  1479. Revision 1.3 2000/08/06 10:42:29 peter
  1480. * merged patches name generation in lib and asm constant eval
  1481. Revision 1.2 2000/07/13 11:32:48 michael
  1482. + removed logs
  1483. }