rautils.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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 (not is_void(aktprocsym.definition.rettype.def)) then
  650. begin
  651. if (procinfo^.return_offset=0) and ((m_tp in aktmodeswitches) or
  652. (m_delphi in aktmodeswitches)) then
  653. begin
  654. Message(asmr_e_cannot_use_RESULT_here);
  655. exit;
  656. end;
  657. opr.ref.offset:=procinfo^.return_offset;
  658. opr.ref.base:= procinfo^.framepointer;
  659. opr.ref.options:=ref_parafixup;
  660. { always assume that the result is valid. }
  661. tfuncretsym(aktprocsym.definition.funcretsym).funcretstate:=vs_assigned;
  662. SetupResult:=true;
  663. end
  664. else
  665. Message(asmr_e_void_function);
  666. end;
  667. Function TOperand.SetupSelf:boolean;
  668. Begin
  669. SetupSelf:=false;
  670. if assigned(procinfo^._class) then
  671. Begin
  672. opr.typ:=OPR_REFERENCE;
  673. opr.ref.offset:=procinfo^.selfpointer_offset;
  674. opr.ref.base:=procinfo^.framepointer;
  675. opr.ref.options:=ref_selffixup;
  676. SetupSelf:=true;
  677. end
  678. else
  679. Message(asmr_e_cannot_use_SELF_outside_a_method);
  680. end;
  681. Function TOperand.SetupOldEBP:boolean;
  682. Begin
  683. SetupOldEBP:=false;
  684. if lexlevel>normal_function_level then
  685. Begin
  686. opr.typ:=OPR_REFERENCE;
  687. opr.ref.offset:=procinfo^.framepointer_offset;
  688. opr.ref.base:=procinfo^.framepointer;
  689. opr.ref.options:=ref_parafixup;
  690. SetupOldEBP:=true;
  691. end
  692. else
  693. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  694. end;
  695. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  696. { search and sets up the correct fields in the Instr record }
  697. { for the NON-constant identifier passed to the routine. }
  698. { if not found returns FALSE. }
  699. var
  700. sym : tsym;
  701. srsymtable : tsymtable;
  702. harrdef : tarraydef;
  703. Begin
  704. SetupVar:=false;
  705. asmsearchsym(s,sym,srsymtable);
  706. if sym = nil then
  707. exit;
  708. case sym.typ of
  709. varsym :
  710. begin
  711. { we always assume in asm statements that }
  712. { that the variable is valid. }
  713. tvarsym(sym).varstate:=vs_used;
  714. inc(tvarsym(sym).refs);
  715. case tvarsym(sym).owner.symtabletype of
  716. objectsymtable :
  717. begin
  718. { this is not allowed, because we don't know if the self
  719. register is still free, and loading it first is also
  720. not possible, because this could break code }
  721. { Be TP/Delphi compatible in Delphi or TP modes }
  722. if (m_tp in aktmodeswitches) then
  723. begin
  724. opr.typ:=OPR_CONSTANT;
  725. opr.val:=tvarsym(sym).address;
  726. end
  727. { I do not agree here people using method vars should ensure
  728. that %esi is valid there }
  729. else
  730. begin
  731. opr.ref.base:=self_pointer;
  732. opr.ref.offset:=tvarsym(sym).address;
  733. end;
  734. hasvar:=true;
  735. SetupVar:=true;
  736. Exit;
  737. end;
  738. globalsymtable,
  739. staticsymtable :
  740. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname);
  741. parasymtable :
  742. begin
  743. { if we only want the offset we don't have to care
  744. the base will be zeroed after ! }
  745. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  746. { this below is wrong because there are two parast
  747. for global functions one of interface the second of
  748. implementation
  749. if (tvarsym(sym).owner=procinfo^.def.parast) or }
  750. GetOffset then
  751. begin
  752. opr.ref.base:=procinfo^.framepointer;
  753. end
  754. else
  755. begin
  756. if (procinfo^.framepointer=stack_pointer) and
  757. assigned(procinfo^.parent) and
  758. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  759. { same problem as above !!
  760. (procinfo^.parent^.sym.definition.parast=tvarsym(sym).owner) and }
  761. (lexlevel>normal_function_level) then
  762. opr.ref.base:=procinfo^.parent^.framepointer
  763. else
  764. message1(asmr_e_local_para_unreachable,s);
  765. end;
  766. opr.ref.offset:=tvarsym(sym).address;
  767. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  768. begin
  769. opr.ref.offsetfixup:=aktprocsym.definition.parast.address_fixup;
  770. opr.ref.options:=ref_parafixup;
  771. end
  772. else
  773. begin
  774. opr.ref.offsetfixup:=0;
  775. opr.ref.options:=ref_none;
  776. end;
  777. if (tvarsym(sym).varspez=vs_var) or
  778. ((tvarsym(sym).varspez=vs_const) and
  779. push_addr_param(tvarsym(sym).vartype.def)) then
  780. SetSize(target_info.size_of_pointer,false);
  781. end;
  782. localsymtable :
  783. begin
  784. if (vo_is_external in tvarsym(sym).varoptions) then
  785. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname)
  786. else
  787. begin
  788. { if we only want the offset we don't have to care
  789. the base will be zeroed after ! }
  790. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  791. {if (tvarsym(sym).owner=procinfo^.def.localst) or}
  792. GetOffset then
  793. opr.ref.base:=procinfo^.framepointer
  794. else
  795. begin
  796. if (procinfo^.framepointer=stack_pointer) and
  797. assigned(procinfo^.parent) and
  798. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  799. {(procinfo^.parent^.sym.definition.localst=tvarsym(sym).owner) and}
  800. (lexlevel>normal_function_level) then
  801. opr.ref.base:=procinfo^.parent^.framepointer
  802. else
  803. message1(asmr_e_local_para_unreachable,s);
  804. end;
  805. opr.ref.offset:=-(tvarsym(sym).address);
  806. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  807. begin
  808. opr.ref.offsetfixup:=aktprocsym.definition.localst.address_fixup;
  809. opr.ref.options:=ref_localfixup;
  810. end
  811. else
  812. begin
  813. opr.ref.offsetfixup:=0;
  814. opr.ref.options:=ref_none;
  815. end;
  816. end;
  817. if (tvarsym(sym).varspez in [vs_var,vs_out]) or
  818. ((tvarsym(sym).varspez=vs_const) and
  819. push_addr_param(tvarsym(sym).vartype.def)) then
  820. SetSize(target_info.size_of_pointer,false);
  821. end;
  822. end;
  823. case tvarsym(sym).vartype.def.deftype of
  824. orddef,
  825. enumdef,
  826. pointerdef,
  827. floatdef :
  828. SetSize(tvarsym(sym).getsize,false);
  829. arraydef :
  830. begin
  831. { for arrays try to get the element size, take care of
  832. multiple indexes }
  833. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  834. while assigned(harrdef.elementtype.def) and
  835. (harrdef.elementtype.def.deftype=arraydef) do
  836. harrdef:=tarraydef(harrdef.elementtype.def);
  837. SetSize(harrdef.elesize,false);
  838. end;
  839. end;
  840. hasvar:=true;
  841. SetupVar:=true;
  842. Exit;
  843. end;
  844. typedconstsym :
  845. begin
  846. opr.ref.symbol:=newasmsymbol(ttypedconstsym(sym).mangledname);
  847. case ttypedconstsym(sym).typedconsttype.def.deftype of
  848. orddef,
  849. enumdef,
  850. pointerdef,
  851. floatdef :
  852. SetSize(ttypedconstsym(sym).getsize,false);
  853. arraydef :
  854. begin
  855. { for arrays try to get the element size, take care of
  856. multiple indexes }
  857. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  858. while assigned(harrdef.elementtype.def) and
  859. (harrdef.elementtype.def.deftype=arraydef) do
  860. harrdef:=tarraydef(harrdef.elementtype.def);
  861. SetSize(harrdef.elesize,false);
  862. end;
  863. end;
  864. hasvar:=true;
  865. SetupVar:=true;
  866. Exit;
  867. end;
  868. constsym :
  869. begin
  870. if tconstsym(sym).consttyp in [constint,constchar,constbool] then
  871. begin
  872. opr.typ:=OPR_CONSTANT;
  873. opr.val:=tconstsym(sym).value;
  874. SetupVar:=true;
  875. Exit;
  876. end;
  877. end;
  878. typesym :
  879. begin
  880. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  881. begin
  882. opr.typ:=OPR_CONSTANT;
  883. opr.val:=0;
  884. SetupVar:=TRUE;
  885. Exit;
  886. end;
  887. end;
  888. procsym :
  889. begin
  890. if assigned(tprocsym(sym).definition.nextoverloaded) then
  891. Message(asmr_w_calling_overload_func);
  892. opr.typ:=OPR_SYMBOL;
  893. opr.symbol:=newasmsymbol(tprocsym(sym).definition.mangledname);
  894. hasvar:=true;
  895. SetupVar:=TRUE;
  896. Exit;
  897. end;
  898. else
  899. begin
  900. Message(asmr_e_unsupported_symbol_type);
  901. exit;
  902. end;
  903. end;
  904. end;
  905. { looks for internal names of variables and routines }
  906. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  907. var
  908. p : tasmsymbol;
  909. begin
  910. SetupDirectVar:=false;
  911. p:=getasmsymbol(hs);
  912. if assigned(p) then
  913. begin
  914. opr.ref.symbol:=p;
  915. hasvar:=true;
  916. SetupDirectVar:=true;
  917. end;
  918. end;
  919. procedure TOperand.InitRef;
  920. {*********************************************************************}
  921. { Description: This routine first check if the opcode is of }
  922. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  923. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  924. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  925. { to point to the default segment. }
  926. {*********************************************************************}
  927. Begin
  928. case opr.typ of
  929. OPR_REFERENCE:
  930. exit;
  931. OPR_NONE: ;
  932. else
  933. Message(asmr_e_invalid_operand_type);
  934. end;
  935. opr.typ := OPR_REFERENCE;
  936. reset_reference(opr.ref);
  937. end;
  938. procedure TOperand.BuildOperand;
  939. begin
  940. abstract;
  941. end;
  942. {****************************************************************************
  943. TInstruction
  944. ****************************************************************************}
  945. constructor TInstruction.create;
  946. Begin
  947. Opcode:=A_NONE;
  948. Opsize:=S_NO;
  949. Condition:=C_NONE;
  950. Ops:=0;
  951. InitOperands;
  952. Labeled:=false;
  953. end;
  954. destructor TInstruction.destroy;
  955. var
  956. i : longint;
  957. Begin
  958. for i:=1 to 3 do
  959. Operands[i].free;
  960. end;
  961. procedure TInstruction.InitOperands;
  962. var
  963. i : longint;
  964. begin
  965. for i:=1 to 3 do
  966. Operands[i].create;
  967. end;
  968. Procedure TInstruction.Swatoperands;
  969. Var
  970. p : toperand;
  971. Begin
  972. case Ops of
  973. 2 :
  974. begin
  975. p:=Operands[1];
  976. Operands[1]:=Operands[2];
  977. Operands[2]:=p;
  978. end;
  979. 3 :
  980. begin
  981. p:=Operands[1];
  982. Operands[1]:=Operands[3];
  983. Operands[3]:=p;
  984. end;
  985. end;
  986. end;
  987. procedure TInstruction.ConcatInstruction(p:TAAsmOutput);
  988. begin
  989. abstract;
  990. end;
  991. procedure TInstruction.BuildOpcode;
  992. begin
  993. abstract;
  994. end;
  995. {***************************************************************************
  996. TLocalLabel
  997. ***************************************************************************}
  998. constructor TLocalLabel.create(const n:string);
  999. begin
  1000. inherited CreateName(n);
  1001. lab:=nil;
  1002. emitted:=false;
  1003. end;
  1004. function TLocalLabel.Gettasmlabel:tasmlabel;
  1005. begin
  1006. if not assigned(lab) then
  1007. begin
  1008. getlabel(lab);
  1009. { this label is forced to be used so it's always written }
  1010. inc(lab.refs);
  1011. end;
  1012. Gettasmlabel:=lab;
  1013. end;
  1014. {***************************************************************************
  1015. TLocalLabelList
  1016. ***************************************************************************}
  1017. procedure LocalLabelEmitted(p:tnamedindexitem);
  1018. begin
  1019. if not TLocalLabel(p).emitted then
  1020. Message1(asmr_e_unknown_label_identifier,p.name);
  1021. end;
  1022. procedure TLocalLabelList.CheckEmitted;
  1023. begin
  1024. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted)
  1025. end;
  1026. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1027. var
  1028. lab : TLocalLabel;
  1029. Begin
  1030. CreateLocalLabel:=true;
  1031. { Check if it already is defined }
  1032. lab:=TLocalLabel(LocalLabellist.Search(s));
  1033. if not assigned(lab) then
  1034. begin
  1035. lab:=TLocalLabel.Create(s);
  1036. LocalLabellist.Insert(lab);
  1037. end;
  1038. { set emitted flag and check for dup syms }
  1039. if emit then
  1040. begin
  1041. if lab.Emitted then
  1042. begin
  1043. Message1(asmr_e_dup_local_sym,lab.Name);
  1044. CreateLocalLabel:=false;
  1045. end;
  1046. lab.Emitted:=true;
  1047. end;
  1048. hl:=lab.Gettasmlabel;
  1049. end;
  1050. {****************************************************************************
  1051. Symbol table helper routines
  1052. ****************************************************************************}
  1053. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1054. var
  1055. i : integer;
  1056. begin
  1057. i:=pos('.',s);
  1058. { allow unit.identifier }
  1059. if i>0 then
  1060. begin
  1061. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1062. if assigned(srsym) then
  1063. begin
  1064. if (srsym.typ=unitsym) and
  1065. (srsym.owner.unitid=0) then
  1066. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1067. else
  1068. srsym:=nil;
  1069. end;
  1070. end
  1071. else
  1072. searchsym(s,srsym,srsymtable);
  1073. end;
  1074. Function SearchType(const hs:string): Boolean;
  1075. var
  1076. srsym : tsym;
  1077. srsymtable : tsymtable;
  1078. begin
  1079. asmsearchsym(hs,srsym,srsymtable);
  1080. SearchType:=assigned(srsym) and
  1081. (srsym.typ=typesym);
  1082. end;
  1083. Function SearchRecordType(const s:string): boolean;
  1084. var
  1085. srsym : tsym;
  1086. srsymtable : tsymtable;
  1087. Begin
  1088. SearchRecordType:=false;
  1089. { Check the constants in symtable }
  1090. asmsearchsym(s,srsym,srsymtable);
  1091. if srsym <> nil then
  1092. Begin
  1093. case srsym.typ of
  1094. typesym :
  1095. begin
  1096. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1097. begin
  1098. SearchRecordType:=true;
  1099. exit;
  1100. end;
  1101. end;
  1102. end;
  1103. end;
  1104. end;
  1105. Function SearchIConstant(const s:string; var l:longint): boolean;
  1106. {**********************************************************************}
  1107. { Description: Searches for a CONSTANT of name s in either the local }
  1108. { symbol list, then in the global symbol list, and returns the value }
  1109. { of that constant in l. Returns TRUE if successfull, if not found, }
  1110. { or if the constant is not of correct type, then returns FALSE }
  1111. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1112. { respectively. }
  1113. {**********************************************************************}
  1114. var
  1115. srsym : tsym;
  1116. srsymtable : tsymtable;
  1117. Begin
  1118. SearchIConstant:=false;
  1119. { check for TRUE or FALSE reserved words first }
  1120. if s = 'TRUE' then
  1121. Begin
  1122. SearchIConstant:=TRUE;
  1123. l:=1;
  1124. exit;
  1125. end;
  1126. if s = 'FALSE' then
  1127. Begin
  1128. SearchIConstant:=TRUE;
  1129. l:=0;
  1130. exit;
  1131. end;
  1132. { Check the constants in symtable }
  1133. asmsearchsym(s,srsym,srsymtable);
  1134. if srsym <> nil then
  1135. Begin
  1136. case srsym.typ of
  1137. constsym :
  1138. begin
  1139. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1140. Begin
  1141. l:=tconstsym(srsym).value;
  1142. SearchIConstant:=TRUE;
  1143. exit;
  1144. end;
  1145. end;
  1146. enumsym:
  1147. Begin
  1148. l:=tenumsym(srsym).value;
  1149. SearchIConstant:=TRUE;
  1150. exit;
  1151. end;
  1152. end;
  1153. end;
  1154. end;
  1155. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1156. { search and returns the offset and size of records/objects of the base }
  1157. { with field name setup in field. }
  1158. { returns FALSE if not found. }
  1159. { used when base is a variable or a typed constant name. }
  1160. var
  1161. st : tsymtable;
  1162. harrdef : tarraydef;
  1163. sym : tsym;
  1164. srsymtable : tsymtable;
  1165. i : longint;
  1166. base : string;
  1167. Begin
  1168. GetRecordOffsetSize:=FALSE;
  1169. Offset:=0;
  1170. Size:=0;
  1171. i:=pos('.',s);
  1172. if i=0 then
  1173. i:=255;
  1174. base:=Copy(s,1,i-1);
  1175. delete(s,1,i);
  1176. if base='SELF' then
  1177. st:=procinfo^._class.symtable
  1178. else
  1179. begin
  1180. asmsearchsym(base,sym,srsymtable);
  1181. st:=nil;
  1182. { we can start with a var,type,typedconst }
  1183. case sym.typ of
  1184. varsym :
  1185. begin
  1186. case tvarsym(sym).vartype.def.deftype of
  1187. recorddef :
  1188. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1189. objectdef :
  1190. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1191. end;
  1192. end;
  1193. typesym :
  1194. begin
  1195. case ttypesym(sym).restype.def.deftype of
  1196. recorddef :
  1197. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1198. objectdef :
  1199. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1200. end;
  1201. end;
  1202. typedconstsym :
  1203. begin
  1204. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1205. recorddef :
  1206. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1207. objectdef :
  1208. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1209. end;
  1210. end;
  1211. end;
  1212. end;
  1213. { now walk all recordsymtables }
  1214. while assigned(st) and (s<>'') do
  1215. begin
  1216. { load next field in base }
  1217. i:=pos('.',s);
  1218. if i=0 then
  1219. i:=255;
  1220. base:=Copy(s,1,i-1);
  1221. delete(s,1,i);
  1222. if st.symtabletype=objectsymtable then
  1223. sym:=search_class_member(tobjectdef(st.defowner),base)
  1224. else
  1225. sym:=tsym(st.search(base));
  1226. if not assigned(sym) then
  1227. begin
  1228. GetRecordOffsetSize:=false;
  1229. exit;
  1230. end;
  1231. st:=nil;
  1232. case sym.typ of
  1233. varsym :
  1234. begin
  1235. inc(Offset,tvarsym(sym).address);
  1236. Size:=tvarsym(sym).getsize;
  1237. case tvarsym(sym).vartype.def.deftype of
  1238. arraydef :
  1239. begin
  1240. { for arrays try to get the element size, take care of
  1241. multiple indexes }
  1242. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1243. while assigned(harrdef.elementtype.def) and
  1244. (harrdef.elementtype.def.deftype=arraydef) do
  1245. harrdef:=tarraydef(harrdef.elementtype.def);
  1246. size:=harrdef.elesize;
  1247. end;
  1248. recorddef :
  1249. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1250. objectdef :
  1251. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1252. end;
  1253. end;
  1254. end;
  1255. end;
  1256. GetRecordOffsetSize:=(s='');
  1257. end;
  1258. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1259. var
  1260. sym : tsym;
  1261. srsymtable : tsymtable;
  1262. hs : string;
  1263. Begin
  1264. hl:=nil;
  1265. SearchLabel:=false;
  1266. { Check for pascal labels, which are case insensetive }
  1267. hs:=upper(s);
  1268. asmsearchsym(hs,sym,srsymtable);
  1269. if sym=nil then
  1270. exit;
  1271. case sym.typ of
  1272. labelsym :
  1273. begin
  1274. hl:=tlabelsym(sym).lab;
  1275. if emit then
  1276. tlabelsym(sym).defined:=true
  1277. else
  1278. tlabelsym(sym).used:=true;
  1279. SearchLabel:=true;
  1280. exit;
  1281. end;
  1282. end;
  1283. end;
  1284. {*************************************************************************}
  1285. { Instruction Generation Utilities }
  1286. {*************************************************************************}
  1287. Procedure ConcatString(p : TAAsmoutput;s:string);
  1288. {*********************************************************************}
  1289. { PROCEDURE ConcatString(s:string); }
  1290. { Description: This routine adds the character chain pointed to in }
  1291. { s to the instruction linked list. }
  1292. {*********************************************************************}
  1293. Var
  1294. pc: PChar;
  1295. Begin
  1296. getmem(pc,length(s)+1);
  1297. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1298. end;
  1299. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1300. {*********************************************************************}
  1301. { PROCEDURE ConcatPasString(s:string); }
  1302. { Description: This routine adds the character chain pointed to in }
  1303. { s to the instruction linked list, contrary to ConcatString it }
  1304. { uses a pascal style string, so it conserves null characters. }
  1305. {*********************************************************************}
  1306. Begin
  1307. p.concat(Tai_string.Create(s));
  1308. end;
  1309. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1310. {*********************************************************************}
  1311. { PROCEDURE ConcatDirect(s:string) }
  1312. { Description: This routine output the string directly to the asm }
  1313. { output, it is only sed when writing special labels in AT&T mode, }
  1314. { and should not be used without due consideration, since it may }
  1315. { cause problems. }
  1316. {*********************************************************************}
  1317. Var
  1318. pc: PChar;
  1319. Begin
  1320. getmem(pc,length(s)+1);
  1321. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1322. end;
  1323. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1324. {*********************************************************************}
  1325. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1326. { Description: This routine adds the value constant to the current }
  1327. { instruction linked list. }
  1328. { maxvalue -> indicates the size of the data to initialize: }
  1329. { $ff -> create a byte node. }
  1330. { $ffff -> create a word node. }
  1331. { $ffffffff -> create a dword node. }
  1332. {*********************************************************************}
  1333. Begin
  1334. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1335. Begin
  1336. Message(asmr_e_constant_out_of_bounds);
  1337. { assuming a value of maxvalue }
  1338. value:=maxvalue;
  1339. end;
  1340. if maxvalue = $ff then
  1341. p.concat(Tai_const.Create_8bit(byte(value)))
  1342. else
  1343. if maxvalue = $ffff then
  1344. p.concat(Tai_const.Create_16bit(word(value)))
  1345. else
  1346. if maxvalue = longint($ffffffff) then
  1347. p.concat(Tai_const.Create_32bit(longint(value)));
  1348. end;
  1349. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1350. begin
  1351. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1352. end;
  1353. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1354. {***********************************************************************}
  1355. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1356. { Description: This routine adds the value constant to the current }
  1357. { instruction linked list. }
  1358. { real_typ -> indicates the type of the real data to initialize: }
  1359. { s32real -> create a single node. }
  1360. { s64real -> create a double node. }
  1361. { s80real -> create an extended node. }
  1362. { s64bit -> create a comp node. }
  1363. { f32bit -> create a fixed node. (not used normally) }
  1364. {***********************************************************************}
  1365. Begin
  1366. case real_typ of
  1367. s32real : p.concat(Tai_real_32bit.Create(value));
  1368. s64real : p.concat(Tai_real_64bit.Create(value));
  1369. s80real : p.concat(Tai_real_80bit.Create(value));
  1370. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1371. end;
  1372. end;
  1373. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1374. {*********************************************************************}
  1375. { PROCEDURE ConcatLabel }
  1376. { Description: This routine either emits a label or a labeled }
  1377. { instruction to the linked list of instructions. }
  1378. {*********************************************************************}
  1379. begin
  1380. p.concat(Tai_label.Create(l));
  1381. end;
  1382. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1383. {*********************************************************************}
  1384. { PROCEDURE ConcatPublic }
  1385. { Description: This routine emits an global definition to the }
  1386. { linked list of instructions.(used by AT&T styled asm) }
  1387. {*********************************************************************}
  1388. begin
  1389. p.concat(Tai_align.Create(l));
  1390. end;
  1391. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1392. {*********************************************************************}
  1393. { PROCEDURE ConcatPublic }
  1394. { Description: This routine emits an global definition to the }
  1395. { linked list of instructions.(used by AT&T styled asm) }
  1396. {*********************************************************************}
  1397. begin
  1398. p.concat(Tai_symbol.Createname_global(s,0));
  1399. end;
  1400. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1401. {*********************************************************************}
  1402. { PROCEDURE ConcatLocal }
  1403. { Description: This routine emits an local definition to the }
  1404. { linked list of instructions. }
  1405. {*********************************************************************}
  1406. begin
  1407. p.concat(Tai_symbol.Createname(s,0));
  1408. end;
  1409. Procedure ConcatGlobalBss(const s : string;size : longint);
  1410. {*********************************************************************}
  1411. { PROCEDURE ConcatGlobalBss }
  1412. { Description: This routine emits an global datablock to the }
  1413. { linked list of instructions. }
  1414. {*********************************************************************}
  1415. begin
  1416. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1417. end;
  1418. Procedure ConcatLocalBss(const s : string;size : longint);
  1419. {*********************************************************************}
  1420. { PROCEDURE ConcatLocalBss }
  1421. { Description: This routine emits a local datablcok to the }
  1422. { linked list of instructions. }
  1423. {*********************************************************************}
  1424. begin
  1425. bssSegment.concat(Tai_datablock.Create(s,size));
  1426. end;
  1427. end.
  1428. {
  1429. $Log$
  1430. Revision 1.21 2001-08-06 21:40:48 peter
  1431. * funcret moved from tprocinfo to tprocdef
  1432. Revision 1.20 2001/04/18 22:01:58 peter
  1433. * registration of targets and assemblers
  1434. Revision 1.19 2001/04/13 20:06:05 peter
  1435. * allow unit.identifier in asm readers
  1436. Revision 1.18 2001/04/13 01:22:13 peter
  1437. * symtable change to classes
  1438. * range check generation and errors fixed, make cycle DEBUG=1 works
  1439. * memory leaks fixed
  1440. Revision 1.17 2001/04/02 21:20:34 peter
  1441. * resulttype rewrite
  1442. Revision 1.16 2001/03/11 22:58:50 peter
  1443. * getsym redesign, removed the globals srsym,srsymtable
  1444. Revision 1.15 2001/02/26 19:44:54 peter
  1445. * merged generic m68k updates from fixes branch
  1446. Revision 1.14 2000/12/25 00:07:28 peter
  1447. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1448. tlinkedlist objects)
  1449. Revision 1.13 2000/12/07 17:19:43 jonas
  1450. * new constant handling: from now on, hex constants >$7fffffff are
  1451. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1452. and became $ffffffff80000000), all constants in the longint range
  1453. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1454. are cardinals and the rest are int64's.
  1455. * added lots of longint typecast to prevent range check errors in the
  1456. compiler and rtl
  1457. * type casts of symbolic ordinal constants are now preserved
  1458. * fixed bug where the original resulttype wasn't restored correctly
  1459. after doing a 64bit rangecheck
  1460. Revision 1.12 2000/11/29 00:30:38 florian
  1461. * unused units removed from uses clause
  1462. * some changes for widestrings
  1463. Revision 1.11 2000/11/06 22:30:30 peter
  1464. * more fixes
  1465. Revision 1.10 2000/11/04 14:25:21 florian
  1466. + merged Attila's changes for interfaces, not tested yet
  1467. Revision 1.9 2000/10/31 22:30:13 peter
  1468. * merged asm result patch part 2
  1469. Revision 1.8 2000/10/31 22:02:51 peter
  1470. * symtable splitted, no real code changes
  1471. Revision 1.7 2000/10/08 10:26:33 peter
  1472. * merged @result fix from Pierre
  1473. Revision 1.6 2000/09/24 21:19:51 peter
  1474. * delphi compile fixes
  1475. Revision 1.5 2000/09/24 15:06:26 peter
  1476. * use defines.inc
  1477. Revision 1.4 2000/08/27 16:11:52 peter
  1478. * moved some util functions from globals,cobjects to cutils
  1479. * splitted files into finput,fmodule
  1480. Revision 1.3 2000/08/06 10:42:29 peter
  1481. * merged patches name generation in lib and asm constant eval
  1482. Revision 1.2 2000/07/13 11:32:48 michael
  1483. + removed logs
  1484. }