rautils.pas 46 KB

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