asmutils.pas 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. {
  2. $Id$
  3. Copyright (c) 1998 Carl Eric Codere
  4. This unit implements some support routines for assembler parsing
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit AsmUtils;
  18. {*************************************************************************}
  19. { This unit implements some objects as well as utilities which will be }
  20. { used by all inline assembler parsers (non-processor specific). }
  21. { }
  22. { Main routines/objects herein: }
  23. { o Object TExprParse is a simple expression parser to resolve assembler }
  24. { expressions. (Based generally on some code by Thai Tran from SWAG). }
  25. { o Object TInstruction is a simple object used for instructions }
  26. { o Record TOperand is a simple record used to store information on }
  27. { each operand. }
  28. { o String conversion routines from octal,binary and hex to decimal. }
  29. { o A linked list object/record for local labels }
  30. { o Routines for retrieving symbols (local and global) }
  31. { o Object for a linked list of strings (with duplicate strings not }
  32. { allowed). }
  33. { o Non-processor dependant routines for adding instructions to the }
  34. { instruction list. }
  35. {*************************************************************************}
  36. {--------------------------------------------------------------------}
  37. { LEFT TO DO: }
  38. { o Fix the remaining bugs in the expression parser, such as with }
  39. { 4+-3 }
  40. { o Add support for local typed constants search. }
  41. { o Add support for private/protected fields in method assembler }
  42. { routines. }
  43. {--------------------------------------------------------------------}
  44. Interface
  45. Uses
  46. symtable,aasm,hcodegen,verbose,systems,globals,files,strings,
  47. cobjects,
  48. {$ifdef i386}
  49. i386;
  50. {$endif}
  51. {$ifdef m68k}
  52. m68k;
  53. {$endif}
  54. Const
  55. RPNMax = 10; { I think you only need 4, but just to be safe }
  56. OpMax = 25;
  57. maxoperands = 3; { Maximum operands for assembler instructions }
  58. Type
  59. {---------------------------------------------------------------------}
  60. { Label Management types }
  61. {---------------------------------------------------------------------}
  62. PAsmLabel = ^TAsmLabel;
  63. PString = ^String;
  64. { Each local label has this structure associated with it }
  65. TAsmLabel = record
  66. name: PString; { pointer to a pascal string name of label }
  67. lab: PLabel; { pointer to a label as defined in FPC }
  68. emitted: boolean; { as the label itself been emitted ? }
  69. next: PAsmLabel; { next node }
  70. end;
  71. TAsmLabelList = Object
  72. public
  73. First: PAsmLabel;
  74. Constructor Init;
  75. Destructor Done;
  76. Procedure Insert(s:string; lab: PLabel; emitted: boolean);
  77. Function Search(const s: string): PAsmLabel;
  78. private
  79. Last: PAsmLabel;
  80. Function NewPasStr(s:string): PString;
  81. end;
  82. {---------------------------------------------------------------------}
  83. { Instruction management types }
  84. {---------------------------------------------------------------------}
  85. toperandtype = (OPR_NONE,OPR_REFERENCE,OPR_CONSTANT,OPR_REGISTER,OPR_LABINSTR,
  86. OPR_REGLIST,OPR_SYMBOL);
  87. { When the TReference field isintvalue = TRUE }
  88. { then offset points to an ABSOLUTE address }
  89. { otherwise isintvalue should always be false }
  90. { Special cases: }
  91. { For the M68k Target, size is UNUSED, the }
  92. { opcode determines the size of the }
  93. { instruction. }
  94. { DIVS/DIVU/MULS/MULU of the form dn,dn:dn }
  95. { is stored as three operands!! }
  96. { Each instruction operand can be of this type }
  97. TOperand = record
  98. size: topsize;
  99. opinfo: longint; { ao_xxxx flags }
  100. case operandtype:toperandtype of
  101. { the size of the opr_none field should be at least equal to each }
  102. { other field as to facilitate initialization. }
  103. OPR_NONE: (l: array[1..sizeof(treference)] of byte);
  104. OPR_REFERENCE: (ref:treference);
  105. OPR_CONSTANT: (val: longint);
  106. OPR_REGISTER: (reg:tregister);
  107. OPR_LABINSTR: (hl: plabel);
  108. { Register list such as in the movem instruction }
  109. OPR_REGLIST: (list: set of tregister);
  110. OPR_SYMBOL : (symbol:pstring);
  111. end;
  112. TInstruction = object
  113. public
  114. operands: array[1..maxoperands] of TOperand;
  115. { if numops = zero, a size may still be valid in operands[1] }
  116. { it still should be checked. }
  117. numops: byte;
  118. { set to TRUE if the instruction is labeled. }
  119. labeled: boolean;
  120. { This is used for instructions such A_CMPSB... etc, to determine }
  121. { the size of the instruction. }
  122. stropsize: topsize;
  123. procedure init;
  124. { sets up the prefix field with the instruction pointed to in s }
  125. procedure addprefix(tok: tasmop);
  126. { sets up the instruction with the instruction pointed to in s }
  127. procedure addinstr(tok: tasmop);
  128. { get the current instruction of this object }
  129. function getinstruction: tasmop;
  130. { get the current prefix of this instruction }
  131. function getprefix: tasmop;
  132. private
  133. prefix: tasmop;
  134. instruction: tasmop;
  135. end;
  136. {---------------------------------------------------------------------}
  137. { Expression parser types }
  138. {---------------------------------------------------------------------}
  139. { expression parser error codes }
  140. texpr_error =
  141. (zero_divide, { divide by zero. }
  142. stack_overflow, { stack overflow. }
  143. stack_underflow, { stack underflow. }
  144. invalid_number, { invalid conversion }
  145. invalid_op); { invalid operator }
  146. TExprOperator = record
  147. ch: char; { operator }
  148. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  149. end;
  150. String15 = String[15];
  151. {**********************************************************************}
  152. { The following operators are supported: }
  153. { '+' : addition }
  154. { '-' : subtraction }
  155. { '*' : multiplication }
  156. { '/' : modulo division }
  157. { '^' : exclusive or }
  158. { '<' : shift left }
  159. { '>' : shift right }
  160. { '&' : bitwise and }
  161. { '|' : bitwise or }
  162. { '~' : bitwise complement }
  163. { '%' : modulo division }
  164. { nnn: longint numbers }
  165. { ( and ) parenthesis }
  166. {**********************************************************************}
  167. TExprParse = Object
  168. public
  169. Constructor Init;
  170. Destructor Done;
  171. Function Evaluate(Expr: String): longint;
  172. Procedure Error(anerror: texpr_error); virtual;
  173. Function Priority(_Operator: Char): Integer; virtual;
  174. private
  175. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  176. RPNTop : Integer;
  177. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  178. OpTop : Integer;
  179. Procedure RPNPush(Num: Longint);
  180. Function RPNPop: Longint;
  181. Procedure RPNCalc(token: String15; prefix: boolean);
  182. Procedure OpPush(_Operator: char; prefix: boolean);
  183. { In reality returns TExprOperaotr }
  184. Procedure OpPop(var _Operator:TExprOperator);
  185. end;
  186. {---------------------------------------------------------------------}
  187. { String routines }
  188. {---------------------------------------------------------------------}
  189. {*********************************************************************}
  190. { PROCEDURE PadZero; }
  191. { Description: Makes sure that the string specified is of the given }
  192. { length, by padding it with binary zeros, or truncating if necessary}
  193. { Remark: The return value is determined BEFORE any eventual padding.}
  194. { Return Value: TRUE = if length of string s was <= then n }
  195. { FALSE = if length of string s was > then n }
  196. {*********************************************************************}
  197. Function PadZero(Var s: String; n: byte): Boolean;
  198. { Converts an Hex digit string to a Decimal string }
  199. { Returns '' if there was an error. }
  200. Function HexToDec(const S:String): String;
  201. { Converts a binary digit string to a Decimal string }
  202. { Returns '' if there was an error. }
  203. Function BinaryToDec(const S:String): String;
  204. { Converts an octal digit string to a Decimal string }
  205. { Returns '' if there was an error. }
  206. Function OctalToDec(const S:String): String;
  207. { Converts a string containing C styled escape sequences to }
  208. { a pascal style string. }
  209. Function EscapeToPascal(const s:string): string;
  210. Procedure ConcatPasString(p : paasmoutput;s:string);
  211. { Writes the string s directly to the assembler output }
  212. Procedure ConcatDirect(p : paasmoutput;s:string);
  213. {---------------------------------------------------------------------}
  214. { Symbol helper routines }
  215. {---------------------------------------------------------------------}
  216. Function GetTypeOffset(const base: string; const field: string;
  217. Var Offset: longint):boolean;
  218. Function GetVarOffset(const base: string; const field: string;
  219. Var Offset: longint):boolean;
  220. Function SearchIConstant(const s:string; var l:longint): boolean;
  221. Function SearchLabel(const s: string; var hl: plabel): boolean;
  222. Function CreateVarInstr(var Instr: TInstruction; const hs:string;
  223. operandnum:byte):boolean;
  224. {*********************************************************************}
  225. { FUNCTION NewPasStr(s:string): PString }
  226. { Description: This routine allocates a string on the heap and }
  227. { returns a pointer to the allocated string. }
  228. { }
  229. { Remarks: The string allocated should not be modified, since it's }
  230. { length will be less then 255. }
  231. { Remarks: It is assumed that HeapError will be called if an }
  232. { allocation fails. }
  233. {*********************************************************************}
  234. Function newpasstr(s: string): Pointer;
  235. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  236. {$ifdef i386}
  237. Procedure FWaitWarning;
  238. {$endif}
  239. {---------------------------------------------------------------------}
  240. { Instruction generation routines }
  241. {---------------------------------------------------------------------}
  242. { swaps in the case of a 2/3 operand opcode the destination and the }
  243. { source as to put it in AT&T style instruction format. }
  244. Procedure SwapOperands(Var instr: TInstruction);
  245. Procedure ConcatLabel(p : paasmoutput;op : tasmop;var l : plabel);
  246. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  247. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  248. Procedure ConcatString(p : paasmoutput;s:string);
  249. Procedure ConcatPublic(p:paasmoutput;const s : string);
  250. Procedure ConcatLocal(p:paasmoutput;const s : string);
  251. Procedure ConcatGlobalBss(const s : string;size : longint);
  252. Procedure ConcatLocalBss(const s : string;size : longint);
  253. { add to list of external labels }
  254. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  255. { add to internal list of labels }
  256. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  257. Implementation
  258. {*************************************************************************}
  259. { Expression Parser }
  260. {*************************************************************************}
  261. Constructor TExprParse.Init;
  262. Begin
  263. end;
  264. Procedure TExprParse.Error(anerror:texpr_error);
  265. var
  266. t : tmsgconst;
  267. Begin
  268. case anerror of
  269. zero_divide: t:=assem_f_ev_zero_divide;
  270. stack_overflow: t:=assem_f_ev_stack_overflow;
  271. stack_underflow: t:=assem_f_ev_stack_underflow;
  272. invalid_number: t:=assem_f_ev_invalid_number;
  273. invalid_op: t:=assem_f_ev_invalid_op;
  274. else
  275. t:=assem_f_ev_unknown;
  276. end;
  277. Message(t);
  278. end;
  279. Procedure TExprParse.RPNPush(Num : longint); { Add an operand to the top of the RPN stack }
  280. begin
  281. if RPNTop < RPNMax then
  282. begin
  283. Inc(RPNTop);
  284. RPNStack[RPNTop] := Num;
  285. end
  286. else
  287. Error(stack_overflow); { Put some error handler here }
  288. end;
  289. Function TExprParse.RPNPop : longint; { Get the operand at the top of the RPN stack }
  290. begin
  291. if RPNTop > 0 then
  292. begin
  293. RPNPop := RPNStack[RPNTop];
  294. Dec(RPNTop);
  295. end
  296. else { Put some error handler here }
  297. Error(stack_underflow);
  298. end;
  299. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  300. Var
  301. Temp : longint;
  302. LocalError : Integer;
  303. begin
  304. { Write(Token, ' '); This just outputs the RPN expression }
  305. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  306. Case Token[1] of { Handle operators }
  307. '+' : Begin
  308. if prefix then
  309. else
  310. RPNPush(RPNPop + RPNPop);
  311. end;
  312. '-' : Begin
  313. if prefix then
  314. RPNPush(-(RPNPop))
  315. else
  316. RPNPush(RPNPop - RPNPop);
  317. end;
  318. '*' : RPNPush(RPNPop * RPNPop);
  319. '&' : RPNPush(RPNPop AND RPNPop);
  320. '|' : RPNPush(RPNPop OR RPNPop);
  321. '~' : RPNPush(NOT RPNPop);
  322. '<' : RPNPush(RPNPop SHL RPNPop);
  323. '>' : RPNPush(RPNPop SHR RPNPop);
  324. '%' : begin
  325. Temp := RPNPop;
  326. if Temp <> 0 then
  327. RPNPush(RPNPop mod Temp)
  328. else Error(zero_divide); { Handle divide by zero error }
  329. end;
  330. '^' : RPNPush(RPNPop XOR RPNPop);
  331. '/' :
  332. begin
  333. Temp := RPNPop;
  334. if Temp <> 0 then
  335. RPNPush(RPNPop div Temp)
  336. else Error(zero_divide);{ Handle divide by 0 error }
  337. end;
  338. end
  339. else
  340. begin { Convert String to number and add to stack }
  341. if token='-2147483648' then
  342. begin
  343. temp:=$80000000;
  344. localerror:=0;
  345. end
  346. else
  347. Val(Token, Temp, LocalError);
  348. if LocalError = 0 then
  349. RPNPush(Temp)
  350. else Error(invalid_number);{ Handle error }
  351. end;
  352. end;
  353. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean); { Add an operator onto top of the stack }
  354. begin
  355. if OpTop < OpMax then
  356. begin
  357. Inc(OpTop);
  358. OpStack[OpTop].ch := _Operator;
  359. OpStack[OpTop].is_prefix := prefix;
  360. end
  361. else Error(stack_overflow); { Put some error handler here }
  362. end;
  363. Procedure TExprParse.OpPop(var _Operator:TExprOperator); { Get operator at the top of the stack }
  364. begin
  365. if OpTop > 0 then
  366. begin
  367. _Operator := OpStack[OpTop];
  368. Dec(OpTop);
  369. end
  370. else Error(stack_underflow); { Put some error handler here }
  371. end;
  372. Function TExprParse.Priority(_Operator : Char) : Integer; { Return priority of operator }
  373. { The greater the priority, the higher the precedence }
  374. begin
  375. Case _Operator OF
  376. '(' : Priority := 0;
  377. '+', '-' : Priority := 1;
  378. '*', '/','%','<','>' : Priority := 2;
  379. '|','&','^','~': Priority := 0;
  380. else Error(invalid_op);{ More error handling }
  381. end;
  382. end;
  383. Function TExprParse.Evaluate(Expr : String):longint;
  384. Var
  385. I : Integer;
  386. Token : String15;
  387. opr: TExprOperator;
  388. begin
  389. OpTop := 0; { Reset stacks }
  390. RPNTop := 0;
  391. Token := '';
  392. For I := 1 to Length(Expr) DO
  393. begin
  394. if Expr[I] in ['0'..'9'] then
  395. begin { Build multi-digit numbers }
  396. Token := Token + Expr[I];
  397. if I = Length(Expr) then { Send last one to calculator }
  398. RPNCalc(Token,false);
  399. end
  400. else
  401. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  402. begin
  403. if Token <> '' then
  404. begin { Send last built number to calc. }
  405. RPNCalc(Token,false);
  406. Token := '';
  407. end;
  408. Case Expr[I] OF
  409. '(' : OpPush('(',false);
  410. ')' : begin
  411. While OpStack[OpTop].ch <> '(' DO
  412. Begin
  413. OpPop(opr);
  414. RPNCalc(opr.ch,opr.is_prefix);
  415. end;
  416. OpPop(opr); { Pop off and ignore the '(' }
  417. end;
  418. '+','-','~' : Begin
  419. { workaround for -2147483648 }
  420. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  421. begin
  422. token:='-';
  423. expr[i]:='+';
  424. end;
  425. { if start of expression then surely a prefix }
  426. { or if previous char was also an operator }
  427. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  428. OpPush(Expr[I],true)
  429. else
  430. Begin
  431. { Evaluate all higher priority operators }
  432. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  433. Begin
  434. OpPop(opr);
  435. RPNCalc(opr.ch,opr.is_prefix);
  436. end;
  437. OpPush(Expr[I],false);
  438. End;
  439. end;
  440. '*', '/',
  441. '^','|','&',
  442. '%','<','>' : begin
  443. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  444. Begin
  445. OpPop(opr);
  446. RPNCalc(opr.ch,opr.is_prefix);
  447. end;
  448. OpPush(Expr[I],false);
  449. end;
  450. end; { Case }
  451. end
  452. else
  453. Error(invalid_op); { Handle bad input error }
  454. end;
  455. { Pop off the remaining operators }
  456. While OpTop > 0 do
  457. Begin
  458. OpPop(opr);
  459. RPNCalc(opr.ch,opr.is_prefix);
  460. end;
  461. { The result is stored on the top of the stack }
  462. Evaluate := RPNPop;
  463. end;
  464. Destructor TExprParse.Done;
  465. Begin
  466. end;
  467. {*************************************************************************}
  468. { String conversions/utils }
  469. {*************************************************************************}
  470. Function newpasstr(s: string): Pointer;
  471. Var
  472. StrPtr: PString;
  473. Begin
  474. GetMem(StrPtr, length(s)+1);
  475. Move(s,StrPtr^,length(s)+1);
  476. newpasstr:= Strptr;
  477. end;
  478. Function EscapeToPascal(const s:string): string;
  479. { converts a C styled string - which contains escape }
  480. { characters to a pascal style string. }
  481. var
  482. i,j: word;
  483. str: string;
  484. temp: string;
  485. value: byte;
  486. code: integer;
  487. Begin
  488. str:='';
  489. i:=1;
  490. j:=1;
  491. repeat
  492. if s[i] = '\' then
  493. Begin
  494. Inc(i);
  495. if i > 255 then
  496. Begin
  497. EscapeToPascal:=str;
  498. exit;
  499. end;
  500. case s[i] of
  501. '\': insert('\',str,j);
  502. 'b': insert(#08,str,j);
  503. 'f': insert(#12,str,j);
  504. 'n': insert(#10,str,j);
  505. 'r': insert(#13,str,j);
  506. 't': insert(#09,str,j);
  507. '"': insert('"',str,j);
  508. { octal number }
  509. '0'..'7': Begin
  510. temp:=s[i];
  511. temp:=temp+s[i+1];
  512. temp:=temp+s[i+2];
  513. inc(i,2);
  514. val(octaltodec(temp),value,code);
  515. if (code <> 0) then
  516. Message(assem_w_invalid_numeric);
  517. insert(chr(value),str,j);
  518. end;
  519. { hexadecimal number }
  520. 'x': Begin
  521. temp:=s[i+1];
  522. temp:=temp+s[i+2];
  523. inc(i,2);
  524. val(hextodec(temp),value,code);
  525. if (code <> 0) then
  526. Message(assem_w_invalid_numeric);
  527. insert(chr(value),str,j);
  528. end;
  529. else
  530. Begin
  531. Message1(assem_e_escape_seq_ignored,s[i]);
  532. insert(s[i],str,j);
  533. end;
  534. end; {end case }
  535. Inc(i);
  536. end
  537. else
  538. Begin
  539. Insert(s[i],str,j);
  540. Inc(i);
  541. if i > 255 then
  542. Begin
  543. EscapeToPascal:=str;
  544. exit;
  545. end;
  546. end;
  547. Inc(j);
  548. until (i > length(s)) or (j > 255);
  549. EscapeToPascal:=str;
  550. end;
  551. Function OctalToDec(const S:String): String;
  552. { Converts an octal string to a Decimal string }
  553. { Returns '' if there was an error. }
  554. var vs: longint;
  555. c: byte;
  556. st: string;
  557. Begin
  558. vs := 0;
  559. for c:=1 to length(s) do
  560. begin
  561. case s[c] of
  562. '0': vs:=vs shl 3;
  563. '1': vs:=vs shl 3+1;
  564. '2': vs:=vs shl 3+2;
  565. '3': vs:=vs shl 3+3;
  566. '4': vs:=vs shl 3+4;
  567. '5': vs:=vs shl 3+5;
  568. '6': vs:=vs shl 3+6;
  569. '7': vs:=vs shl 3+7;
  570. else
  571. begin
  572. OctalToDec := '';
  573. exit;
  574. end;
  575. end;
  576. end;
  577. str(vs,st);
  578. OctalToDec := st;
  579. end;
  580. Function BinaryToDec(const S:String): String;
  581. { Converts a binary string to a Decimal string }
  582. { Returns '' if there was an error. }
  583. var vs: longint;
  584. c: byte;
  585. st: string;
  586. Begin
  587. vs := 0;
  588. for c:=1 to length(s) do
  589. begin
  590. if s[c] = '0' then
  591. vs:=vs shl 1
  592. else
  593. if s[c]='1' then
  594. vs:=vs shl 1+1
  595. else
  596. begin
  597. BinaryToDec := '';
  598. exit;
  599. end;
  600. end;
  601. str(vs,st);
  602. BinaryToDec := st;
  603. end;
  604. Function HexToDec(const S:String): String;
  605. var vs: longint;
  606. c: byte;
  607. st: string;
  608. Begin
  609. vs := 0;
  610. for c:=1 to length(s) do
  611. begin
  612. case upcase(s[c]) of
  613. '0': vs:=vs shl 4;
  614. '1': vs:=vs shl 4+1;
  615. '2': vs:=vs shl 4+2;
  616. '3': vs:=vs shl 4+3;
  617. '4': vs:=vs shl 4+4;
  618. '5': vs:=vs shl 4+5;
  619. '6': vs:=vs shl 4+6;
  620. '7': vs:=vs shl 4+7;
  621. '8': vs:=vs shl 4+8;
  622. '9': vs:=vs shl 4+9;
  623. 'A': vs:=vs shl 4+10;
  624. 'B': vs:=vs shl 4+11;
  625. 'C': vs:=vs shl 4+12;
  626. 'D': vs:=vs shl 4+13;
  627. 'E': vs:=vs shl 4+14;
  628. 'F': vs:=vs shl 4+15;
  629. else
  630. begin
  631. HexToDec := '';
  632. exit;
  633. end;
  634. end;
  635. end;
  636. str(vs,st);
  637. HexToDec := st;
  638. end;
  639. Function PadZero(Var s: String; n: byte): Boolean;
  640. Begin
  641. PadZero := TRUE;
  642. { Do some error checking first }
  643. if Length(s) = n then
  644. exit
  645. else
  646. if Length(s) > n then
  647. Begin
  648. PadZero := FALSE;
  649. delete(s,n+1,length(s));
  650. exit;
  651. end
  652. else
  653. PadZero := TRUE;
  654. { Fill it up with the specified character }
  655. fillchar(s[length(s)+1],n-1,#0);
  656. s[0] := chr(n);
  657. end;
  658. {*************************************************************************}
  659. { Instruction utilities }
  660. {*************************************************************************}
  661. Procedure TInstruction.init;
  662. var
  663. k: integer;
  664. Begin
  665. numops := 0;
  666. labeled := FALSE;
  667. stropsize := S_NO;
  668. prefix := A_NONE;
  669. instruction := A_NONE;
  670. for k:=1 to maxoperands do
  671. begin
  672. operands[k].size := S_NO;
  673. operands[k].operandtype := OPR_NONE;
  674. { init to zeros }
  675. fillchar(operands[k].l, sizeof(operands[k].l),#0);
  676. end;
  677. end;
  678. Procedure TInstruction.addprefix(tok: tasmop);
  679. Begin
  680. if tok = A_NONE then
  681. Message(assem_e_syn_prefix_not_found);
  682. if Prefix = A_NONE then
  683. Prefix := tok
  684. else
  685. Message(assem_e_syn_try_add_more_prefix);
  686. end;
  687. Procedure TInstruction.addinstr(tok: tasmop);
  688. Begin
  689. if tok = A_NONE then
  690. Message(assem_e_syn_opcode_not_found);
  691. Instruction := tok;
  692. end;
  693. function TInstruction.getinstruction: tasmop;
  694. Begin
  695. getinstruction := Instruction;
  696. end;
  697. { get the current prefix of this instruction }
  698. function TInstruction.getprefix: tasmop;
  699. Begin
  700. getprefix := prefix;
  701. end;
  702. {*************************************************************************}
  703. { Local label utilities }
  704. {*************************************************************************}
  705. Constructor TAsmLabelList.Init;
  706. Begin
  707. First := nil;
  708. Last := nil;
  709. end;
  710. Procedure TAsmLabelList.Insert(s:string; lab: PLabel; emitted: boolean);
  711. {*********************************************************************}
  712. { Description: Insert a node at the end of the list with lab and }
  713. { and the name in s. The name is allocated on the heap. }
  714. { Duplicates are not allowed. }
  715. { Indicate in emitted if this label itself has been emitted, or is it}
  716. { a simple labeled instruction? }
  717. {*********************************************************************}
  718. Begin
  719. if search(s) = nil then
  720. Begin
  721. if First = nil then
  722. Begin
  723. New(First);
  724. Last := First;
  725. end
  726. else
  727. Begin
  728. New(Last^.Next);
  729. Last := Last^.Next;
  730. end;
  731. Last^.name := NewPasStr(s);
  732. Last^.Lab := lab;
  733. Last^.Next := nil;
  734. Last^.emitted := emitted;
  735. end;
  736. end;
  737. Function TAsmLabelList.Search(const s: string): PAsmLabel;
  738. {*********************************************************************}
  739. { Description: This routine searches for a label named s in the }
  740. { linked list, returns a pointer to the label if found, otherwise }
  741. { returns nil. }
  742. {*********************************************************************}
  743. Var
  744. asmlab: PAsmLabel;
  745. Begin
  746. asmlab := First;
  747. if First = nil then
  748. Begin
  749. Search := nil;
  750. exit;
  751. end;
  752. While (asmlab^.name^ <> s) and (asmlab^.Next <> nil) do
  753. asmlab := asmlab^.Next;
  754. if asmlab^.name^ = s then
  755. search := asmlab
  756. else
  757. search := nil;
  758. end;
  759. Destructor TAsmLabelList.Done;
  760. {*********************************************************************}
  761. { Description: This routine takes care of deallocating all nodes }
  762. { in the linked list, as well as deallocating the string pointers }
  763. { of these nodes. }
  764. { }
  765. { Remark: The PLabel field is NOT freed, the compiler takes care of }
  766. { this. }
  767. {*********************************************************************}
  768. Var
  769. temp: PAsmLabel;
  770. temp1: PAsmLabel;
  771. Begin
  772. temp := First;
  773. while temp <> nil do
  774. Begin
  775. Freemem(Temp^.name, length(Temp^.name^)+1);
  776. Temp1 := Temp^.Next;
  777. Dispose(Temp);
  778. Temp := Temp1;
  779. { The plabel could be deleted here, but let us not do }
  780. { it, FPC will do it instead. }
  781. end;
  782. end;
  783. Function TAsmLabelList.newpasstr(s: string): PString;
  784. {*********************************************************************}
  785. { FUNCTION NewPasStr(s:string): PString }
  786. { Description: This routine allocates a string on the heap and }
  787. { returns a pointer to the allocated string. }
  788. { }
  789. { Remarks: The string allocated should not be modified, since it's }
  790. { length will be less then 255. }
  791. { Remarks: It is assumed that HeapError will be called if an }
  792. { allocation fails. }
  793. {*********************************************************************}
  794. Var
  795. StrPtr: PString;
  796. Begin
  797. GetMem(StrPtr, length(s)+1);
  798. Move(s,StrPtr^,length(s)+1);
  799. newpasstr:= Strptr;
  800. end;
  801. {*************************************************************************}
  802. { Symbol table helper routines }
  803. {*************************************************************************}
  804. Procedure SwapOperands(Var instr: TInstruction);
  805. Var
  806. tempopr: TOperand;
  807. Begin
  808. if instr.numops = 2 then
  809. Begin
  810. tempopr := instr.operands[1];
  811. instr.operands[1] := instr.operands[2];
  812. instr.operands[2] := tempopr;
  813. end
  814. else
  815. if instr.numops = 3 then
  816. Begin
  817. tempopr := instr.operands[1];
  818. instr.operands[1] := instr.operands[3];
  819. instr.operands[3] := tempopr;
  820. end;
  821. end;
  822. Function SearchIConstant(const s:string; var l:longint): boolean;
  823. {**********************************************************************}
  824. { Description: Searches for a CONSTANT of name s in either the local }
  825. { symbol list, then in the global symbol list, and returns the value }
  826. { of that constant in l. Returns TRUE if successfull, if not found, }
  827. { or if the constant is not of correct type, then returns FALSE }
  828. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  829. { respectively. }
  830. {**********************************************************************}
  831. var
  832. sym: psym;
  833. Begin
  834. SearchIConstant := FALSE;
  835. { check for TRUE or FALSE reserved words first }
  836. if s = 'TRUE' then
  837. Begin
  838. SearchIConstant := TRUE;
  839. l := 1;
  840. end
  841. else
  842. if s = 'FALSE' then
  843. Begin
  844. SearchIConstant := TRUE;
  845. l := 0;
  846. end
  847. else
  848. if assigned(aktprocsym) then
  849. Begin
  850. if assigned(aktprocsym^.definition) then
  851. Begin
  852. { Check the local constants }
  853. if assigned(aktprocsym^.definition^.localst) then
  854. sym := aktprocsym^.definition^.localst^.search(s)
  855. else
  856. sym := nil;
  857. if assigned(sym) then
  858. Begin
  859. if (sym^.typ = constsym) and (pconstsym(sym)^.consttype in
  860. [constord,constint,constchar,constbool]) then
  861. Begin
  862. l:=pconstsym(sym)^.value;
  863. SearchIConstant := TRUE;
  864. exit;
  865. end;
  866. end;
  867. end;
  868. end;
  869. { Check the global constants }
  870. getsym(s,false);
  871. if srsym <> nil then
  872. Begin
  873. if (srsym^.typ=constsym) and (pconstsym(srsym)^.consttype in
  874. [constord,constint,constchar,constbool]) then
  875. Begin
  876. l:=pconstsym(srsym)^.value;
  877. SearchIConstant := TRUE;
  878. exit;
  879. end;
  880. end;
  881. end;
  882. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  883. {**********************************************************************}
  884. { Description: This routine changes the correct fields and correct }
  885. { offset in the reference, so that it points to the __RESULT or }
  886. { @Result variable (depending on the inline asm). }
  887. { Resturns a reference with all correct offset correctly set up. }
  888. { The Operand should already point to a treference on entry. }
  889. {**********************************************************************}
  890. Begin
  891. { replace by correct offset. }
  892. if assigned(procinfo.retdef) and
  893. (procinfo.retdef<>pdef(voiddef)) then
  894. begin
  895. instr.operands[operandnum].ref.offset := procinfo.retoffset;
  896. instr.operands[operandnum].ref.base := procinfo.framepointer;
  897. { always assume that the result is valid. }
  898. procinfo.funcret_is_valid:=true;
  899. end
  900. else
  901. Message(assem_e_invalid_symbol_ref);
  902. end;
  903. {$ifdef i386}
  904. Procedure FWaitWarning;
  905. begin
  906. if (target_info.target=target_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  907. Message(assem_w_fwait_emu_prob);
  908. end;
  909. {$endif i386}
  910. Function GetVarOffset(const base: string; const field: string;
  911. Var Offset: longint):boolean;
  912. { search and returns the offset of records/objects of the base }
  913. { with field name setup in field. }
  914. { returns 0 if not found. }
  915. { used when base is a variable or a typed constant name. }
  916. var
  917. sym:psym;
  918. p: psym;
  919. Begin
  920. GetVarOffset := FALSE;
  921. Offset := 0;
  922. { local list }
  923. if assigned(aktprocsym) then
  924. begin
  925. if assigned(aktprocsym^.definition^.localst) then
  926. sym:=aktprocsym^.definition^.localst^.search(base)
  927. else
  928. sym:=nil;
  929. if assigned(sym) then
  930. begin
  931. { field of local record variable. }
  932. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  933. begin
  934. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  935. if assigned(pvarsym(p)) then
  936. Begin
  937. Offset := pvarsym(p)^.address;
  938. GetVarOffset := TRUE;
  939. Exit;
  940. end;
  941. end;
  942. end
  943. else
  944. begin
  945. { field of local record parameter to routine. }
  946. if assigned(aktprocsym^.definition^.parast) then
  947. sym:=aktprocsym^.definition^.parast^.search(base)
  948. else
  949. sym:=nil;
  950. if assigned(sym) then
  951. begin
  952. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef)
  953. then
  954. begin
  955. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  956. if assigned(p) then
  957. Begin
  958. Offset := pvarsym(p)^.address;
  959. GetVarOffset := TRUE;
  960. Exit;
  961. end;
  962. end; { endif }
  963. end; {endif }
  964. end; { endif }
  965. end;
  966. { not found.. .now look for global variables. }
  967. getsym(base,false);
  968. sym:=srsym;
  969. if assigned(sym) then
  970. Begin
  971. { field of global record variable. }
  972. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  973. begin
  974. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  975. if assigned(p) then
  976. Begin
  977. Offset := pvarsym(p)^.address;
  978. GetVarOffset := TRUE;
  979. Exit;
  980. end;
  981. end
  982. else
  983. { field of global record type constant. }
  984. if (sym^.typ=typedconstsym) and (ptypedconstsym(sym)^.definition^.deftype=recorddef)
  985. then
  986. begin
  987. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  988. if assigned(p) then
  989. Begin
  990. Offset := pvarsym(p)^.address;
  991. GetVarOffset := TRUE;
  992. Exit;
  993. end;
  994. end
  995. end; { end looking for global variables .. }
  996. end;
  997. Function GetTypeOffset(const base: string; const field: string;
  998. Var Offset: longint):boolean;
  999. { search and returns the offset of records/objects of the base }
  1000. { with field name setup in field. }
  1001. { returns 0 if not found. }
  1002. { used when base is a variable or a typed constant name. }
  1003. var
  1004. sym:psym;
  1005. p: psym;
  1006. Begin
  1007. Offset := 0;
  1008. GetTypeOffset := FALSE;
  1009. { local list }
  1010. if assigned(aktprocsym) then
  1011. begin
  1012. if assigned(aktprocsym^.definition^.localst) then
  1013. sym:=aktprocsym^.definition^.localst^.search(base)
  1014. else
  1015. sym:=nil;
  1016. if assigned(sym) then
  1017. begin
  1018. { field of local record type. }
  1019. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1020. begin
  1021. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1022. if assigned(p) then
  1023. Begin
  1024. Offset := pvarsym(p)^.address;
  1025. GetTypeOffset := TRUE;
  1026. Exit;
  1027. end;
  1028. end;
  1029. end
  1030. else
  1031. begin
  1032. { field of local record type to routine. }
  1033. if assigned(aktprocsym^.definition^.parast) then
  1034. sym:=aktprocsym^.definition^.parast^.search(base)
  1035. else
  1036. sym:=nil;
  1037. if assigned(sym) then
  1038. begin
  1039. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef)
  1040. then
  1041. begin
  1042. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1043. if assigned(p) then
  1044. Begin
  1045. Offset := pvarsym(p)^.address;
  1046. GetTypeOffset := TRUE;
  1047. Exit;
  1048. end;
  1049. end; { endif }
  1050. end; {endif }
  1051. end; { endif }
  1052. end;
  1053. { not found.. .now look for global types. }
  1054. getsym(base,false);
  1055. sym:=srsym;
  1056. if assigned(sym) then
  1057. Begin
  1058. { field of global record types. }
  1059. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1060. begin
  1061. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1062. if assigned(p) then
  1063. Begin
  1064. Offset := pvarsym(p)^.address;
  1065. GetTypeOffset := TRUE;
  1066. Exit;
  1067. end
  1068. end
  1069. else
  1070. { public field names of objects }
  1071. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=objectdef)then
  1072. begin
  1073. if assigned(pobjectdef(ptypesym(sym)^.definition)^.publicsyms) then
  1074. Begin
  1075. p:=pobjectdef(ptypesym(sym)^.definition)^.publicsyms^.search(field);
  1076. if assigned(p) then
  1077. Begin
  1078. Offset := pvarsym(p)^.address;
  1079. GetTypeOffset := TRUE;
  1080. Exit;
  1081. end
  1082. end;
  1083. end;
  1084. end; { end looking for global variables .. }
  1085. end;
  1086. Function CreateVarInstr(var Instr: TInstruction; const hs:string;operandnum:byte): Boolean;
  1087. { search and sets up the correct fields in the Instr record }
  1088. { for the NON-constant identifier passed to the routine. }
  1089. { if not found returns FALSE. }
  1090. var
  1091. sym : psym;
  1092. l : longint;
  1093. Begin
  1094. CreateVarInstr := FALSE;
  1095. { are we in a routine ? }
  1096. if assigned(aktprocsym) then
  1097. begin
  1098. { search the local list for the name of this variable. }
  1099. if assigned(aktprocsym^.definition^.localst) then
  1100. sym:=aktprocsym^.definition^.localst^.search(hs)
  1101. else
  1102. sym:=nil;
  1103. if assigned(sym) then
  1104. begin
  1105. case sym^.typ of
  1106. typedconstsym,
  1107. varsym : begin
  1108. { we always assume in asm statements that }
  1109. { that the variable is valid. }
  1110. pvarsym(sym)^.is_valid:=1;
  1111. if pvarsym(sym)^.owner^.symtabletype=staticsymtable then
  1112. begin
  1113. if assigned(instr.operands[operandnum].ref.symbol) then
  1114. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1115. instr.operands[operandnum].ref.symbol:=newpasstr(pvarsym(sym)^.mangledname);
  1116. end
  1117. else
  1118. begin
  1119. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1120. instr.operands[operandnum].ref.offset := -(pvarsym(sym)^.address);
  1121. end;
  1122. { the current size is NOT overriden if it already }
  1123. { exists, such as in the case of a byte ptr, in }
  1124. { front of the identifier. }
  1125. if instr.operands[operandnum].size = S_NO then
  1126. Begin
  1127. case pvarsym(sym)^.getsize of
  1128. 1: instr.operands[operandnum].size := S_B;
  1129. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1130. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1131. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1132. extended_size: instr.operands[operandnum].size := S_FX;
  1133. else
  1134. { this is in the case where the instruction is LEA }
  1135. { or something like that, in that case size is not }
  1136. { important. }
  1137. instr.operands[operandnum].size := S_NO;
  1138. end; { end case }
  1139. end;
  1140. { ok, finished for thir variable. }
  1141. CreateVarInstr := TRUE;
  1142. Exit;
  1143. end;
  1144. procsym : begin
  1145. { free the memory before changing the symbol name. }
  1146. if assigned(instr.operands[operandnum].ref.symbol) then
  1147. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1148. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1149. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1150. CreateVarInstr := TRUE;
  1151. Exit;
  1152. end
  1153. else
  1154. begin
  1155. Message(assem_e_unsupported_symbol_type);
  1156. exit;
  1157. end;
  1158. end;
  1159. end;
  1160. { now check for parameters passed to routine }
  1161. if assigned(aktprocsym^.definition^.parast) then
  1162. sym:=aktprocsym^.definition^.parast^.search(hs)
  1163. else
  1164. sym:=nil;
  1165. if assigned(sym) then
  1166. begin
  1167. case sym^.typ of
  1168. varsym : begin
  1169. l:=pvarsym(sym)^.address;
  1170. { set offset }
  1171. inc(l,aktprocsym^.definition^.parast^.call_offset);
  1172. pvarsym(sym)^.is_valid:=1;
  1173. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1174. instr.operands[operandnum].ref.offset := l;
  1175. { the current size is NOT overriden if it already }
  1176. { exists, such as in the case of a byte ptr, in }
  1177. { front of the identifier. }
  1178. if instr.operands[operandnum].size = S_NO then
  1179. Begin
  1180. case pvarsym(sym)^.getsize of
  1181. 1: instr.operands[operandnum].size := S_B;
  1182. 2: instr.operands[operandnum].size := S_W;
  1183. 4: instr.operands[operandnum].size := S_L;
  1184. 8: instr.operands[operandnum].size := S_IQ;
  1185. extended_size: instr.operands[operandnum].size := S_FX;
  1186. else
  1187. { this is in the case where the instruction is LEA }
  1188. { or something like that, in that case size is not }
  1189. { important. }
  1190. instr.operands[operandnum].size := S_NO;
  1191. end; { end case }
  1192. end; { endif }
  1193. CreateVarInstr := TRUE;
  1194. Exit;
  1195. end;
  1196. else
  1197. begin
  1198. Message(assem_e_unsupported_symbol_type);
  1199. exit;
  1200. end;
  1201. end; { case }
  1202. end; { endif }
  1203. end;
  1204. { not found.. .now look for global variables. }
  1205. getsym(hs,false);
  1206. sym:=srsym;
  1207. if assigned(sym) then
  1208. Begin
  1209. case sym^.typ of
  1210. varsym,
  1211. typedconstsym : Begin
  1212. { free the memory before changing the symbol name. }
  1213. if assigned(instr.operands[operandnum].ref.symbol) then
  1214. FreeMem(instr.operands[operandnum].ref.symbol,
  1215. length(instr.operands[operandnum].ref.symbol^)+1);
  1216. instr.operands[operandnum].ref.symbol:=newpasstr(sym^.mangledname);
  1217. { the current size is NOT overriden if it already }
  1218. { exists, such as in the case of a byte ptr, in }
  1219. { front of the identifier. }
  1220. if (instr.operands[operandnum].size = S_NO) and (sym^.typ = varsym) then
  1221. Begin
  1222. case pvarsym(sym)^.getsize of
  1223. 1: instr.operands[operandnum].size := S_B;
  1224. 2: instr.operands[operandnum].size := S_W;
  1225. 4: instr.operands[operandnum].size := S_L;
  1226. 8: instr.operands[operandnum].size := S_IQ;
  1227. else
  1228. { this is in the case where the instruction is LEA }
  1229. { or something like that, in that case size is not }
  1230. { important. }
  1231. instr.operands[operandnum].size := S_NO;
  1232. end;
  1233. end
  1234. else
  1235. if (instr.operands[operandnum].size = S_NO) and (sym^.typ = typedconstsym) then
  1236. Begin
  1237. { only these are valid sizes, otherwise prefixes are }
  1238. { required. }
  1239. case ptypedconstsym(sym)^.definition^.size of
  1240. 1: instr.operands[operandnum].size := S_B;
  1241. 2: instr.operands[operandnum].size := S_W;
  1242. 4: instr.operands[operandnum].size := S_L;
  1243. 8: instr.operands[operandnum].size := S_IQ;
  1244. else
  1245. { this is in the case where the instruction is LEA }
  1246. { or something like that, in that case size is not }
  1247. { important. }
  1248. instr.operands[operandnum].size := S_NO;
  1249. end;
  1250. end; { endif }
  1251. CreateVarInstr := TRUE;
  1252. Exit;
  1253. end;
  1254. procsym : begin
  1255. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  1256. Message(assem_w_calling_overload_func);
  1257. { free the memory before changing the symbol name. }
  1258. if assigned(instr.operands[operandnum].ref.symbol) then
  1259. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1260. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1261. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1262. CreateVarInstr := TRUE;
  1263. Exit;
  1264. end;
  1265. else
  1266. begin
  1267. Message(assem_e_unsupported_symbol_type);
  1268. exit;
  1269. end;
  1270. end; {case}
  1271. end; { end looking for global variables .. }
  1272. end;
  1273. Function SearchLabel(const s: string; var hl: plabel): boolean;
  1274. {**********************************************************************}
  1275. { Description: Searches for a pascal label definition, first in the }
  1276. { local symbol list and then in the global symbol list. If found then }
  1277. { return pointer to label and return true, otherwise returns false. }
  1278. {**********************************************************************}
  1279. var
  1280. sym: psym;
  1281. Begin
  1282. SearchLabel := FALSE;
  1283. if assigned(aktprocsym) then
  1284. Begin
  1285. { Check the local constants }
  1286. if assigned(aktprocsym^.definition) then
  1287. Begin
  1288. if assigned(aktprocsym^.definition^.localst) then
  1289. sym := aktprocsym^.definition^.localst^.search(s)
  1290. else
  1291. sym := nil;
  1292. if assigned(sym) then
  1293. Begin
  1294. if (sym^.typ = labelsym) then
  1295. Begin
  1296. hl:=plabelsym(sym)^.number;
  1297. SearchLabel := TRUE;
  1298. exit;
  1299. end;
  1300. end;
  1301. end;
  1302. end;
  1303. { Check the global label symbols... }
  1304. getsym(s,false);
  1305. if srsym <> nil then
  1306. Begin
  1307. if (srsym^.typ=labelsym) then
  1308. Begin
  1309. hl:=plabelsym(srsym)^.number;
  1310. SearchLabel:= TRUE;
  1311. exit;
  1312. end;
  1313. end;
  1314. end;
  1315. {*************************************************************************}
  1316. { Instruction Generation Utilities }
  1317. {*************************************************************************}
  1318. Procedure ConcatString(p : paasmoutput;s:string);
  1319. {*********************************************************************}
  1320. { PROCEDURE ConcatString(s:string); }
  1321. { Description: This routine adds the character chain pointed to in }
  1322. { s to the instruction linked list. }
  1323. {*********************************************************************}
  1324. Var
  1325. pc: PChar;
  1326. Begin
  1327. getmem(pc,length(s)+1);
  1328. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1329. end;
  1330. Procedure ConcatPasString(p : paasmoutput;s:string);
  1331. {*********************************************************************}
  1332. { PROCEDURE ConcatPasString(s:string); }
  1333. { Description: This routine adds the character chain pointed to in }
  1334. { s to the instruction linked list, contrary to ConcatString it }
  1335. { uses a pascal style string, so it conserves null characters. }
  1336. {*********************************************************************}
  1337. Begin
  1338. p^.concat(new(pai_string,init(s)));
  1339. end;
  1340. Procedure ConcatDirect(p : paasmoutput;s:string);
  1341. {*********************************************************************}
  1342. { PROCEDURE ConcatDirect(s:string) }
  1343. { Description: This routine output the string directly to the asm }
  1344. { output, it is only sed when writing special labels in AT&T mode, }
  1345. { and should not be used without due consideration, since it may }
  1346. { cause problems. }
  1347. {*********************************************************************}
  1348. Var
  1349. pc: PChar;
  1350. Begin
  1351. getmem(pc,length(s)+1);
  1352. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1353. end;
  1354. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1355. {*********************************************************************}
  1356. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1357. { Description: This routine adds the value constant to the current }
  1358. { instruction linked list. }
  1359. { maxvalue -> indicates the size of the data to initialize: }
  1360. { $ff -> create a byte node. }
  1361. { $ffff -> create a word node. }
  1362. { $ffffffff -> create a dword node. }
  1363. {*********************************************************************}
  1364. Begin
  1365. if value > maxvalue then
  1366. Begin
  1367. Message(assem_e_constant_out_of_bounds);
  1368. { assuming a value of maxvalue }
  1369. value := maxvalue;
  1370. end;
  1371. if maxvalue = $ff then
  1372. p^.concat(new(pai_const,init_8bit(byte(value))))
  1373. else
  1374. if maxvalue = $ffff then
  1375. p^.concat(new(pai_const,init_16bit(word(value))))
  1376. else
  1377. if maxvalue = $ffffffff then
  1378. p^.concat(new(pai_const,init_32bit(longint(value))));
  1379. end;
  1380. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1381. {***********************************************************************}
  1382. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1383. { Description: This routine adds the value constant to the current }
  1384. { instruction linked list. }
  1385. { real_typ -> indicates the type of the real data to initialize: }
  1386. { s32real -> create a single node. }
  1387. { s64real -> create a double node. }
  1388. { s80real -> create an extended node. }
  1389. { s64bit -> create a comp node. }
  1390. { f32bit -> create a fixed node. (not used normally) }
  1391. {***********************************************************************}
  1392. Begin
  1393. case real_typ of
  1394. s32real : p^.concat(new(pai_single,init(value)));
  1395. s64real : p^.concat(new(pai_double,init(value)));
  1396. s80real : p^.concat(new(pai_extended,init(value)));
  1397. s64bit : p^.concat(new(pai_comp,init(value)));
  1398. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1399. end;
  1400. end;
  1401. Procedure ConcatLabel(p: paasmoutput;op : tasmop;var l : plabel);
  1402. {*********************************************************************}
  1403. { PROCEDURE ConcatLabel }
  1404. { Description: This routine either emits a label or a labeled }
  1405. { instruction to the linked list of instructions. }
  1406. {*********************************************************************}
  1407. begin
  1408. if op=A_LABEL then
  1409. p^.concat(new(pai_label,init(l)))
  1410. else
  1411. p^.concat(new(pai_labeled,init(op,l)))
  1412. end;
  1413. procedure ConcatPublic(p:paasmoutput;const s : string);
  1414. {*********************************************************************}
  1415. { PROCEDURE ConcatPublic }
  1416. { Description: This routine emits an global definition to the }
  1417. { linked list of instructions.(used by AT&T styled asm) }
  1418. {*********************************************************************}
  1419. begin
  1420. p^.concat(new(pai_symbol,init_global(s)));
  1421. { concat_internal(s,EXT_NEAR); done in aasm }
  1422. end;
  1423. procedure ConcatLocal(p:paasmoutput;const s : string);
  1424. {*********************************************************************}
  1425. { PROCEDURE ConcatLocal }
  1426. { Description: This routine emits an local definition to the }
  1427. { linked list of instructions. }
  1428. {*********************************************************************}
  1429. begin
  1430. p^.concat(new(pai_symbol,init(s)));
  1431. { concat_internal(s,EXT_NEAR); done in aasm }
  1432. end;
  1433. Procedure ConcatGlobalBss(const s : string;size : longint);
  1434. {*********************************************************************}
  1435. { PROCEDURE ConcatGlobalBss }
  1436. { Description: This routine emits an global datablock to the }
  1437. { linked list of instructions. }
  1438. {*********************************************************************}
  1439. begin
  1440. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1441. { concat_internal(s,EXT_NEAR); done in aasm }
  1442. end;
  1443. Procedure ConcatLocalBss(const s : string;size : longint);
  1444. {*********************************************************************}
  1445. { PROCEDURE ConcatLocalBss }
  1446. { Description: This routine emits a local datablcok to the }
  1447. { linked list of instructions. }
  1448. {*********************************************************************}
  1449. begin
  1450. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1451. { concat_internal(s,EXT_NEAR); done in aasm }
  1452. end;
  1453. { add to list of external labels }
  1454. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  1455. {*********************************************************************}
  1456. { PROCEDURE ConcatExternal }
  1457. { Description: This routine emits an external definition to the }
  1458. { linked list of instructions.(used by AT&T styled asm) }
  1459. {*********************************************************************}
  1460. { check if in internal list and remove it there }
  1461. var p : pai_external;
  1462. begin
  1463. p:=search_assembler_symbol(internals,s,typ);
  1464. if p<>nil then internals^.remove(p);
  1465. concat_external(s,typ);
  1466. end;
  1467. { add to internal list of labels }
  1468. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  1469. {*********************************************************************}
  1470. { PROCEDURE ConcatInternal }
  1471. { Description: This routine emits an internal definition of a symbol }
  1472. { (used by AT&T styled asm for undefined labels) }
  1473. {*********************************************************************}
  1474. begin
  1475. concat_internal(s,typ);
  1476. end;
  1477. end.
  1478. {
  1479. $Log$
  1480. Revision 1.7 1998-08-18 20:51:32 peter
  1481. * fixed bug 42
  1482. Revision 1.6 1998/08/10 14:49:40 peter
  1483. + localswitches, moduleswitches, globalswitches splitting
  1484. Revision 1.5 1998/07/14 21:46:38 peter
  1485. * updated messages file
  1486. Revision 1.4 1998/06/04 23:51:31 peter
  1487. * m68k compiles
  1488. + .def file creation moved to gendef.pas so it could also be used
  1489. for win32
  1490. Revision 1.3 1998/05/31 14:13:30 peter
  1491. * fixed call bugs with assembler readers
  1492. + OPR_SYMBOL to hold a symbol in the asm parser
  1493. * fixed staticsymtable vars which were acessed through %ebp instead of
  1494. name
  1495. Revision 1.2 1998/04/29 10:33:43 pierre
  1496. + added some code for ansistring (not complete nor working yet)
  1497. * corrected operator overloading
  1498. * corrected nasm output
  1499. + started inline procedures
  1500. + added starstarn : use ** for exponentiation (^ gave problems)
  1501. + started UseTokenInfo cond to get accurate positions
  1502. }