rautils.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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. l : longint;
  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. l:=opr.ref.offset;
  894. opr.typ:=OPR_SYMBOL;
  895. opr.symbol:=newasmsymbol(tprocsym(sym).definition.mangledname);
  896. opr.symofs:=l;
  897. hasvar:=true;
  898. SetupVar:=TRUE;
  899. Exit;
  900. end;
  901. else
  902. begin
  903. Message(asmr_e_unsupported_symbol_type);
  904. exit;
  905. end;
  906. end;
  907. end;
  908. { looks for internal names of variables and routines }
  909. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  910. var
  911. p : tasmsymbol;
  912. begin
  913. SetupDirectVar:=false;
  914. p:=getasmsymbol(hs);
  915. if assigned(p) then
  916. begin
  917. opr.ref.symbol:=p;
  918. hasvar:=true;
  919. SetupDirectVar:=true;
  920. end;
  921. end;
  922. procedure TOperand.InitRef;
  923. {*********************************************************************}
  924. { Description: This routine first check if the opcode is of }
  925. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  926. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  927. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  928. { to point to the default segment. }
  929. {*********************************************************************}
  930. Begin
  931. case opr.typ of
  932. OPR_REFERENCE:
  933. exit;
  934. OPR_NONE: ;
  935. else
  936. Message(asmr_e_invalid_operand_type);
  937. end;
  938. opr.typ := OPR_REFERENCE;
  939. reset_reference(opr.ref);
  940. end;
  941. procedure TOperand.BuildOperand;
  942. begin
  943. abstract;
  944. end;
  945. {****************************************************************************
  946. TInstruction
  947. ****************************************************************************}
  948. constructor TInstruction.create;
  949. Begin
  950. Opcode:=A_NONE;
  951. Opsize:=S_NO;
  952. Condition:=C_NONE;
  953. Ops:=0;
  954. InitOperands;
  955. Labeled:=false;
  956. end;
  957. destructor TInstruction.destroy;
  958. var
  959. i : longint;
  960. Begin
  961. for i:=1 to 3 do
  962. Operands[i].free;
  963. end;
  964. procedure TInstruction.InitOperands;
  965. var
  966. i : longint;
  967. begin
  968. for i:=1 to 3 do
  969. Operands[i].create;
  970. end;
  971. Procedure TInstruction.Swatoperands;
  972. Var
  973. p : toperand;
  974. Begin
  975. case Ops of
  976. 2 :
  977. begin
  978. p:=Operands[1];
  979. Operands[1]:=Operands[2];
  980. Operands[2]:=p;
  981. end;
  982. 3 :
  983. begin
  984. p:=Operands[1];
  985. Operands[1]:=Operands[3];
  986. Operands[3]:=p;
  987. end;
  988. end;
  989. end;
  990. procedure TInstruction.ConcatInstruction(p:TAAsmOutput);
  991. begin
  992. abstract;
  993. end;
  994. procedure TInstruction.BuildOpcode;
  995. begin
  996. abstract;
  997. end;
  998. {***************************************************************************
  999. TLocalLabel
  1000. ***************************************************************************}
  1001. constructor TLocalLabel.create(const n:string);
  1002. begin
  1003. inherited CreateName(n);
  1004. lab:=nil;
  1005. emitted:=false;
  1006. end;
  1007. function TLocalLabel.Gettasmlabel:tasmlabel;
  1008. begin
  1009. if not assigned(lab) then
  1010. begin
  1011. getlabel(lab);
  1012. { this label is forced to be used so it's always written }
  1013. inc(lab.refs);
  1014. end;
  1015. Gettasmlabel:=lab;
  1016. end;
  1017. {***************************************************************************
  1018. TLocalLabelList
  1019. ***************************************************************************}
  1020. procedure LocalLabelEmitted(p:tnamedindexitem);
  1021. begin
  1022. if not TLocalLabel(p).emitted then
  1023. Message1(asmr_e_unknown_label_identifier,p.name);
  1024. end;
  1025. procedure TLocalLabelList.CheckEmitted;
  1026. begin
  1027. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted)
  1028. end;
  1029. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1030. var
  1031. lab : TLocalLabel;
  1032. Begin
  1033. CreateLocalLabel:=true;
  1034. { Check if it already is defined }
  1035. lab:=TLocalLabel(LocalLabellist.Search(s));
  1036. if not assigned(lab) then
  1037. begin
  1038. lab:=TLocalLabel.Create(s);
  1039. LocalLabellist.Insert(lab);
  1040. end;
  1041. { set emitted flag and check for dup syms }
  1042. if emit then
  1043. begin
  1044. if lab.Emitted then
  1045. begin
  1046. Message1(asmr_e_dup_local_sym,lab.Name);
  1047. CreateLocalLabel:=false;
  1048. end;
  1049. lab.Emitted:=true;
  1050. end;
  1051. hl:=lab.Gettasmlabel;
  1052. end;
  1053. {****************************************************************************
  1054. Symbol table helper routines
  1055. ****************************************************************************}
  1056. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1057. var
  1058. i : integer;
  1059. begin
  1060. i:=pos('.',s);
  1061. { allow unit.identifier }
  1062. if i>0 then
  1063. begin
  1064. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1065. if assigned(srsym) then
  1066. begin
  1067. if (srsym.typ=unitsym) and
  1068. (srsym.owner.unitid=0) then
  1069. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1070. else
  1071. srsym:=nil;
  1072. end;
  1073. end
  1074. else
  1075. searchsym(s,srsym,srsymtable);
  1076. end;
  1077. Function SearchType(const hs:string): Boolean;
  1078. var
  1079. srsym : tsym;
  1080. srsymtable : tsymtable;
  1081. begin
  1082. asmsearchsym(hs,srsym,srsymtable);
  1083. SearchType:=assigned(srsym) and
  1084. (srsym.typ=typesym);
  1085. end;
  1086. Function SearchRecordType(const s:string): boolean;
  1087. var
  1088. srsym : tsym;
  1089. srsymtable : tsymtable;
  1090. Begin
  1091. SearchRecordType:=false;
  1092. { Check the constants in symtable }
  1093. asmsearchsym(s,srsym,srsymtable);
  1094. if srsym <> nil then
  1095. Begin
  1096. case srsym.typ of
  1097. typesym :
  1098. begin
  1099. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1100. begin
  1101. SearchRecordType:=true;
  1102. exit;
  1103. end;
  1104. end;
  1105. end;
  1106. end;
  1107. end;
  1108. Function SearchIConstant(const s:string; var l:longint): boolean;
  1109. {**********************************************************************}
  1110. { Description: Searches for a CONSTANT of name s in either the local }
  1111. { symbol list, then in the global symbol list, and returns the value }
  1112. { of that constant in l. Returns TRUE if successfull, if not found, }
  1113. { or if the constant is not of correct type, then returns FALSE }
  1114. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1115. { respectively. }
  1116. {**********************************************************************}
  1117. var
  1118. srsym : tsym;
  1119. srsymtable : tsymtable;
  1120. Begin
  1121. SearchIConstant:=false;
  1122. { check for TRUE or FALSE reserved words first }
  1123. if s = 'TRUE' then
  1124. Begin
  1125. SearchIConstant:=TRUE;
  1126. l:=1;
  1127. exit;
  1128. end;
  1129. if s = 'FALSE' then
  1130. Begin
  1131. SearchIConstant:=TRUE;
  1132. l:=0;
  1133. exit;
  1134. end;
  1135. { Check the constants in symtable }
  1136. asmsearchsym(s,srsym,srsymtable);
  1137. if srsym <> nil then
  1138. Begin
  1139. case srsym.typ of
  1140. constsym :
  1141. begin
  1142. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1143. Begin
  1144. l:=tconstsym(srsym).value;
  1145. SearchIConstant:=TRUE;
  1146. exit;
  1147. end;
  1148. end;
  1149. enumsym:
  1150. Begin
  1151. l:=tenumsym(srsym).value;
  1152. SearchIConstant:=TRUE;
  1153. exit;
  1154. end;
  1155. end;
  1156. end;
  1157. end;
  1158. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1159. { search and returns the offset and size of records/objects of the base }
  1160. { with field name setup in field. }
  1161. { returns FALSE if not found. }
  1162. { used when base is a variable or a typed constant name. }
  1163. var
  1164. st : tsymtable;
  1165. harrdef : tarraydef;
  1166. sym : tsym;
  1167. srsymtable : tsymtable;
  1168. i : longint;
  1169. base : string;
  1170. Begin
  1171. GetRecordOffsetSize:=FALSE;
  1172. Offset:=0;
  1173. Size:=0;
  1174. i:=pos('.',s);
  1175. if i=0 then
  1176. i:=255;
  1177. base:=Copy(s,1,i-1);
  1178. delete(s,1,i);
  1179. if base='SELF' then
  1180. st:=procinfo^._class.symtable
  1181. else
  1182. begin
  1183. asmsearchsym(base,sym,srsymtable);
  1184. st:=nil;
  1185. { we can start with a var,type,typedconst }
  1186. case sym.typ of
  1187. varsym :
  1188. begin
  1189. case tvarsym(sym).vartype.def.deftype of
  1190. recorddef :
  1191. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1192. objectdef :
  1193. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1194. end;
  1195. end;
  1196. typesym :
  1197. begin
  1198. case ttypesym(sym).restype.def.deftype of
  1199. recorddef :
  1200. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1201. objectdef :
  1202. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1203. end;
  1204. end;
  1205. typedconstsym :
  1206. begin
  1207. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1208. recorddef :
  1209. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1210. objectdef :
  1211. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1212. end;
  1213. end;
  1214. end;
  1215. end;
  1216. { now walk all recordsymtables }
  1217. while assigned(st) and (s<>'') do
  1218. begin
  1219. { load next field in base }
  1220. i:=pos('.',s);
  1221. if i=0 then
  1222. i:=255;
  1223. base:=Copy(s,1,i-1);
  1224. delete(s,1,i);
  1225. if st.symtabletype=objectsymtable then
  1226. sym:=search_class_member(tobjectdef(st.defowner),base)
  1227. else
  1228. sym:=tsym(st.search(base));
  1229. if not assigned(sym) then
  1230. begin
  1231. GetRecordOffsetSize:=false;
  1232. exit;
  1233. end;
  1234. st:=nil;
  1235. case sym.typ of
  1236. varsym :
  1237. begin
  1238. inc(Offset,tvarsym(sym).address);
  1239. Size:=tvarsym(sym).getsize;
  1240. case tvarsym(sym).vartype.def.deftype of
  1241. arraydef :
  1242. begin
  1243. { for arrays try to get the element size, take care of
  1244. multiple indexes }
  1245. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1246. while assigned(harrdef.elementtype.def) and
  1247. (harrdef.elementtype.def.deftype=arraydef) do
  1248. harrdef:=tarraydef(harrdef.elementtype.def);
  1249. size:=harrdef.elesize;
  1250. end;
  1251. recorddef :
  1252. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1253. objectdef :
  1254. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1255. end;
  1256. end;
  1257. end;
  1258. end;
  1259. GetRecordOffsetSize:=(s='');
  1260. end;
  1261. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1262. var
  1263. sym : tsym;
  1264. srsymtable : tsymtable;
  1265. hs : string;
  1266. Begin
  1267. hl:=nil;
  1268. SearchLabel:=false;
  1269. { Check for pascal labels, which are case insensetive }
  1270. hs:=upper(s);
  1271. asmsearchsym(hs,sym,srsymtable);
  1272. if sym=nil then
  1273. exit;
  1274. case sym.typ of
  1275. labelsym :
  1276. begin
  1277. hl:=tlabelsym(sym).lab;
  1278. if emit then
  1279. tlabelsym(sym).defined:=true
  1280. else
  1281. tlabelsym(sym).used:=true;
  1282. SearchLabel:=true;
  1283. exit;
  1284. end;
  1285. end;
  1286. end;
  1287. {*************************************************************************}
  1288. { Instruction Generation Utilities }
  1289. {*************************************************************************}
  1290. Procedure ConcatString(p : TAAsmoutput;s:string);
  1291. {*********************************************************************}
  1292. { PROCEDURE ConcatString(s:string); }
  1293. { Description: This routine adds the character chain pointed to in }
  1294. { s to the instruction linked list. }
  1295. {*********************************************************************}
  1296. Var
  1297. pc: PChar;
  1298. Begin
  1299. getmem(pc,length(s)+1);
  1300. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1301. end;
  1302. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1303. {*********************************************************************}
  1304. { PROCEDURE ConcatPasString(s:string); }
  1305. { Description: This routine adds the character chain pointed to in }
  1306. { s to the instruction linked list, contrary to ConcatString it }
  1307. { uses a pascal style string, so it conserves null characters. }
  1308. {*********************************************************************}
  1309. Begin
  1310. p.concat(Tai_string.Create(s));
  1311. end;
  1312. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1313. {*********************************************************************}
  1314. { PROCEDURE ConcatDirect(s:string) }
  1315. { Description: This routine output the string directly to the asm }
  1316. { output, it is only sed when writing special labels in AT&T mode, }
  1317. { and should not be used without due consideration, since it may }
  1318. { cause problems. }
  1319. {*********************************************************************}
  1320. Var
  1321. pc: PChar;
  1322. Begin
  1323. getmem(pc,length(s)+1);
  1324. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1325. end;
  1326. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1327. {*********************************************************************}
  1328. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1329. { Description: This routine adds the value constant to the current }
  1330. { instruction linked list. }
  1331. { maxvalue -> indicates the size of the data to initialize: }
  1332. { $ff -> create a byte node. }
  1333. { $ffff -> create a word node. }
  1334. { $ffffffff -> create a dword node. }
  1335. {*********************************************************************}
  1336. Begin
  1337. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1338. Begin
  1339. Message(asmr_e_constant_out_of_bounds);
  1340. { assuming a value of maxvalue }
  1341. value:=maxvalue;
  1342. end;
  1343. if maxvalue = $ff then
  1344. p.concat(Tai_const.Create_8bit(byte(value)))
  1345. else
  1346. if maxvalue = $ffff then
  1347. p.concat(Tai_const.Create_16bit(word(value)))
  1348. else
  1349. if maxvalue = longint($ffffffff) then
  1350. p.concat(Tai_const.Create_32bit(longint(value)));
  1351. end;
  1352. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1353. begin
  1354. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1355. end;
  1356. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1357. {***********************************************************************}
  1358. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1359. { Description: This routine adds the value constant to the current }
  1360. { instruction linked list. }
  1361. { real_typ -> indicates the type of the real data to initialize: }
  1362. { s32real -> create a single node. }
  1363. { s64real -> create a double node. }
  1364. { s80real -> create an extended node. }
  1365. { s64bit -> create a comp node. }
  1366. { f32bit -> create a fixed node. (not used normally) }
  1367. {***********************************************************************}
  1368. Begin
  1369. case real_typ of
  1370. s32real : p.concat(Tai_real_32bit.Create(value));
  1371. s64real : p.concat(Tai_real_64bit.Create(value));
  1372. s80real : p.concat(Tai_real_80bit.Create(value));
  1373. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1374. end;
  1375. end;
  1376. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1377. {*********************************************************************}
  1378. { PROCEDURE ConcatLabel }
  1379. { Description: This routine either emits a label or a labeled }
  1380. { instruction to the linked list of instructions. }
  1381. {*********************************************************************}
  1382. begin
  1383. p.concat(Tai_label.Create(l));
  1384. end;
  1385. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1386. {*********************************************************************}
  1387. { PROCEDURE ConcatPublic }
  1388. { Description: This routine emits an global definition to the }
  1389. { linked list of instructions.(used by AT&T styled asm) }
  1390. {*********************************************************************}
  1391. begin
  1392. p.concat(Tai_align.Create(l));
  1393. end;
  1394. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1395. {*********************************************************************}
  1396. { PROCEDURE ConcatPublic }
  1397. { Description: This routine emits an global definition to the }
  1398. { linked list of instructions.(used by AT&T styled asm) }
  1399. {*********************************************************************}
  1400. begin
  1401. p.concat(Tai_symbol.Createname_global(s,0));
  1402. end;
  1403. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1404. {*********************************************************************}
  1405. { PROCEDURE ConcatLocal }
  1406. { Description: This routine emits an local definition to the }
  1407. { linked list of instructions. }
  1408. {*********************************************************************}
  1409. begin
  1410. p.concat(Tai_symbol.Createname(s,0));
  1411. end;
  1412. Procedure ConcatGlobalBss(const s : string;size : longint);
  1413. {*********************************************************************}
  1414. { PROCEDURE ConcatGlobalBss }
  1415. { Description: This routine emits an global datablock to the }
  1416. { linked list of instructions. }
  1417. {*********************************************************************}
  1418. begin
  1419. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1420. end;
  1421. Procedure ConcatLocalBss(const s : string;size : longint);
  1422. {*********************************************************************}
  1423. { PROCEDURE ConcatLocalBss }
  1424. { Description: This routine emits a local datablcok to the }
  1425. { linked list of instructions. }
  1426. {*********************************************************************}
  1427. begin
  1428. bssSegment.concat(Tai_datablock.Create(s,size));
  1429. end;
  1430. end.
  1431. {
  1432. $Log$
  1433. Revision 1.22 2001-08-12 17:57:07 peter
  1434. * under development flag for targets
  1435. Revision 1.21 2001/08/06 21:40:48 peter
  1436. * funcret moved from tprocinfo to tprocdef
  1437. Revision 1.20 2001/04/18 22:01:58 peter
  1438. * registration of targets and assemblers
  1439. Revision 1.19 2001/04/13 20:06:05 peter
  1440. * allow unit.identifier in asm readers
  1441. Revision 1.18 2001/04/13 01:22:13 peter
  1442. * symtable change to classes
  1443. * range check generation and errors fixed, make cycle DEBUG=1 works
  1444. * memory leaks fixed
  1445. Revision 1.17 2001/04/02 21:20:34 peter
  1446. * resulttype rewrite
  1447. Revision 1.16 2001/03/11 22:58:50 peter
  1448. * getsym redesign, removed the globals srsym,srsymtable
  1449. Revision 1.15 2001/02/26 19:44:54 peter
  1450. * merged generic m68k updates from fixes branch
  1451. Revision 1.14 2000/12/25 00:07:28 peter
  1452. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1453. tlinkedlist objects)
  1454. Revision 1.13 2000/12/07 17:19:43 jonas
  1455. * new constant handling: from now on, hex constants >$7fffffff are
  1456. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1457. and became $ffffffff80000000), all constants in the longint range
  1458. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1459. are cardinals and the rest are int64's.
  1460. * added lots of longint typecast to prevent range check errors in the
  1461. compiler and rtl
  1462. * type casts of symbolic ordinal constants are now preserved
  1463. * fixed bug where the original resulttype wasn't restored correctly
  1464. after doing a 64bit rangecheck
  1465. Revision 1.12 2000/11/29 00:30:38 florian
  1466. * unused units removed from uses clause
  1467. * some changes for widestrings
  1468. Revision 1.11 2000/11/06 22:30:30 peter
  1469. * more fixes
  1470. Revision 1.10 2000/11/04 14:25:21 florian
  1471. + merged Attila's changes for interfaces, not tested yet
  1472. Revision 1.9 2000/10/31 22:30:13 peter
  1473. * merged asm result patch part 2
  1474. Revision 1.8 2000/10/31 22:02:51 peter
  1475. * symtable splitted, no real code changes
  1476. Revision 1.7 2000/10/08 10:26:33 peter
  1477. * merged @result fix from Pierre
  1478. Revision 1.6 2000/09/24 21:19:51 peter
  1479. * delphi compile fixes
  1480. Revision 1.5 2000/09/24 15:06:26 peter
  1481. * use defines.inc
  1482. Revision 1.4 2000/08/27 16:11:52 peter
  1483. * moved some util functions from globals,cobjects to cutils
  1484. * splitted files into finput,fmodule
  1485. Revision 1.3 2000/08/06 10:42:29 peter
  1486. * merged patches name generation in lib and asm constant eval
  1487. Revision 1.2 2000/07/13 11:32:48 michael
  1488. + removed logs
  1489. }