qcc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Copyright (C) 1996-1997 Id Software, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. See file, 'COPYING', for details.
  14. */
  15. #include "cmdlib.h"
  16. #include <stdio.h>
  17. #include <setjmp.h>
  18. #include "pr_comp.h"
  19. /*
  20. TODO:
  21. "stopped at 10 errors"
  22. other pointer types for models and clients?
  23. compact string heap?
  24. allways initialize all variables to something safe
  25. the def->type->type arrangement is really silly.
  26. return type checking
  27. parm count type checking
  28. immediate overflow checking
  29. pass the first two parms in call->b and call->c
  30. */
  31. /*
  32. comments
  33. --------
  34. // comments discard text until the end of line
  35. / * * / comments discard all enclosed text (spaced out on this line because this documentation is in a regular C comment block, and typing them in normally causes a parse error)
  36. code structure
  37. --------------
  38. A definition is:
  39. <type> <name> [ = <immediate>] {, <name> [ = <immediate>] };
  40. types
  41. -----
  42. simple types: void, float, vector, string, or entity
  43. float width, height;
  44. string name;
  45. entity self, other;
  46. vector types:
  47. vector org; // also creates org_x, org_y, and org_z float defs
  48. A function type is specified as: simpletype ( type name {,type name} )
  49. The names are ignored except when the function is initialized.
  50. void() think;
  51. entity() FindTarget;
  52. void(vector destination, float speed, void() callback) SUB_CalcMove;
  53. void(...) dprint; // variable argument builtin
  54. A field type is specified as: .type
  55. .vector origin;
  56. .string netname;
  57. .void() think, touch, use;
  58. names
  59. -----
  60. Names are a maximum of 64 characters, must begin with A-Z,a-z, or _, and can continue with those characters or 0-9.
  61. There are two levels of scoping: global, and function. The parameter list of a function and any vars declared inside a function with the "local" statement are only visible within that function,
  62. immediates
  63. ----------
  64. Float immediates must begin with 0-9 or minus sign. .5 is illegal.
  65. A parsing ambiguity is present with negative constants. "a-5" will be parsed as "a", then "-5", causing an error. Seperate the - from the digits with a space "a - 5" to get the proper behavior.
  66. 12
  67. 1.6
  68. 0.5
  69. -100
  70. Vector immediates are three float immediates enclosed in single quotes.
  71. '0 0 0'
  72. '20.5 -10 0.00001'
  73. String immediates are characters enclosed in double quotes. The string cannot contain explicit newlines, but the escape character \n can embed one. The \" escape can be used to include a quote in the string.
  74. "maps/jrwiz1.bsp"
  75. "sound/nin/pain.wav"
  76. "ouch!\n"
  77. Code immediates are statements enclosed in {} braces.
  78. statement:
  79. { <multiple statements> }
  80. <expression>;
  81. local <type> <name> [ = <immediate>] {, <name> [ = <immediate>] };
  82. return <expression>;
  83. if ( <expression> ) <statement> [ else <statement> ];
  84. while ( <expression> ) <statement>;
  85. do <statement> while ( <expression> );
  86. <function name> ( <function parms> );
  87. expression:
  88. combiations of names and these operators with standard C precedence:
  89. "&&", "||", "<=", ">=","==", "!=", "!", "*", "/", "-", "+", "=", ".", "<", ">", "&", "|"
  90. Parenthesis can be used to alter order of operation.
  91. The & and | operations perform integral bit ops on floats
  92. A built in function immediate is a number sign followed by an integer.
  93. #1
  94. #12
  95. compilation
  96. -----------
  97. Source files are processed sequentially without dumping any state, so if a defs file is the first one processed, the definitions will be available to all other files.
  98. The language is strongly typed and there are no casts.
  99. Anything that is initialized is assumed to be constant, and will have immediates folded into it. If you change the value, your program will malfunction. All uninitialized globals will be saved to savegame files.
  100. Functions cannot have more than eight parameters.
  101. Error recovery during compilation is minimal. It will skip to the next global definition, so you will never see more than one error at a time in a given function. All compilation aborts after ten error messages.
  102. Names can be defined multiple times until they are defined with an initialization, allowing functions to be prototyped before their definition.
  103. void() MyFunction; // the prototype
  104. void() MyFunction = // the initialization
  105. {
  106. dprint ("we're here\n");
  107. };
  108. entities and fields
  109. -------------------
  110. execution
  111. ---------
  112. Code execution is initiated by C code in quake from two main places: the timed think routines for periodic control, and the touch function when two objects impact each other.
  113. There are three global variables that are set before beginning code execution:
  114. entity world; // the server's world object, which holds all global
  115. // state for the server, like the deathmatch flags
  116. // and the body ques.
  117. entity self; // the entity the function is executing for
  118. entity other; // the other object in an impact, not used for thinks
  119. float time; // the current game time. Note that because the
  120. // entities in the world are simulated sequentially,
  121. // time is NOT strictly increasing. An impact late
  122. // in one entity's time slice may set time higher
  123. // than the think function of the next entity.
  124. // The difference is limited to 0.1 seconds.
  125. Execution is also caused by a few uncommon events, like the addition of a new client to an existing server.
  126. There is a runnaway counter that stops a program if 100000 statements are executed, assuming it is in an infinite loop.
  127. It is acceptable to change the system set global variables. This is usually done to pose as another entity by changing self and calling a function.
  128. The interpretation is fairly efficient, but it is still over an order of magnitude slower than compiled C code. All time consuming operations should be made into built in functions.
  129. A profile counter is kept for each function, and incremented for each interpreted instruction inside that function. The "profile" console command in Quake will dump out the top 10 functions, then clear all the counters. The "profile all" command will dump sorted stats for every function that has been executed.
  130. afunc ( 4, bfunc(1,2,3));
  131. will fail because there is a shared parameter marshaling area, which will cause the 1 from bfunc to overwrite the 4 allready placed in parm0. When a function is called, it copies the parms from the globals into it's privately scoped variables, so there is no collision when calling another function.
  132. total = factorial(3) + factorial(4);
  133. Will fail because the return value from functions is held in a single global area. If this really gets on your nerves, tell me and I can work around it at a slight performance and space penalty by allocating a new register for the function call and copying it out.
  134. built in functions
  135. ------------------
  136. void(string text) dprint;
  137. Prints the string to the server console.
  138. void(entity client, string text) cprint;
  139. Prints a message to a specific client.
  140. void(string text) bprint;
  141. Broadcast prints a message to all clients on the current server.
  142. entity() spawn;
  143. Returns a totally empty entity. You can manually set everything up, or just set the origin and call one of the existing entity setup functions.
  144. entity(entity start, .string field, string match) find;
  145. Searches the server entity list beginning at start, looking for an entity that has entity.field = match. To start at the beginning of the list, pass world. World is returned when the end of the list is reached.
  146. <FIXME: define all the other functions...>
  147. gotchas
  148. -------
  149. The && and || operators DO NOT EARLY OUT like C!
  150. Don't confuse single quoted vectors with double quoted strings
  151. The function declaration syntax takes a little getting used to.
  152. Don't forget the ; after the trailing brace of a function initialization.
  153. Don't forget the "local" before defining local variables.
  154. There are no ++ / -- operators, or operate/assign operators.
  155. */
  156. //=============================================================================
  157. // offsets are allways multiplied by 4 before using
  158. typedef int gofs_t; // offset in global data block
  159. typedef struct function_s function_t;
  160. #define MAX_PARMS 8
  161. typedef struct type_s
  162. {
  163. etype_t type;
  164. struct def_s *def; // a def that points to this type
  165. struct type_s *next;
  166. // function types are more complex
  167. struct type_s *aux_type; // return type or field type
  168. int num_parms; // -1 = variable args
  169. struct type_s *parm_types[MAX_PARMS]; // only [num_parms] allocated
  170. } type_t;
  171. typedef struct def_s
  172. {
  173. type_t *type;
  174. char *name;
  175. struct def_s *next;
  176. gofs_t ofs;
  177. struct def_s *scope; // function the var was defined in, or NULL
  178. int initialized; // 1 when a declaration included "= immediate"
  179. } def_t;
  180. //============================================================================
  181. // pr_loc.h -- program local defs
  182. #define MAX_ERRORS 10
  183. #define MAX_NAME 64 // chars long
  184. #define MAX_REGS 16384
  185. //=============================================================================
  186. typedef union eval_s
  187. {
  188. string_t string;
  189. float _float;
  190. float vector[3];
  191. func_t function;
  192. int _int;
  193. union eval_s *ptr;
  194. } eval_t;
  195. extern int type_size[8];
  196. extern def_t *def_for_type[8];
  197. extern type_t type_void, type_string, type_float, type_vector, type_entity, type_field, type_function, type_pointer, type_floatfield;
  198. extern def_t def_void, def_string, def_float, def_vector, def_entity, def_field, def_function, def_pointer;
  199. struct function_s
  200. {
  201. int builtin; // if non 0, call an internal function
  202. int code; // first statement
  203. char *file; // source file with definition
  204. int file_line;
  205. struct def_s *def;
  206. int parm_ofs[MAX_PARMS]; // allways contiguous, right?
  207. };
  208. //
  209. // output generated by prog parsing
  210. //
  211. typedef struct
  212. {
  213. char *memory;
  214. int max_memory;
  215. int current_memory;
  216. type_t *types;
  217. def_t def_head; // unused head of linked list
  218. def_t *def_tail; // add new defs after this and move it
  219. int size_fields;
  220. } pr_info_t;
  221. extern pr_info_t pr;
  222. typedef struct
  223. {
  224. char *name;
  225. char *opname;
  226. float priority;
  227. boolean right_associative;
  228. def_t *type_a, *type_b, *type_c;
  229. } opcode_t;
  230. //============================================================================
  231. extern opcode_t pr_opcodes[99]; // sized by initialization
  232. extern boolean pr_dumpasm;
  233. extern def_t *pr_global_defs[MAX_REGS]; // to find def for a global variable
  234. typedef enum {
  235. tt_eof, // end of file reached
  236. tt_name, // an alphanumeric name token
  237. tt_punct, // code punctuation
  238. tt_immediate, // string, float, vector
  239. } token_type_t;
  240. extern char pr_token[2048];
  241. extern token_type_t pr_token_type;
  242. extern type_t *pr_immediate_type;
  243. extern eval_t pr_immediate;
  244. void PR_PrintStatement (dstatement_t *s);
  245. void PR_Lex (void);
  246. // reads the next token into pr_token and classifies its type
  247. type_t *PR_ParseType (void);
  248. char *PR_ParseName (void);
  249. boolean PR_Check (char *string);
  250. void PR_Expect (char *string);
  251. void PR_ParseError (char *error, ...);
  252. extern jmp_buf pr_parse_abort; // longjump with this on parse error
  253. extern int pr_source_line;
  254. extern char *pr_file_p;
  255. void *PR_Malloc (int size);
  256. #define OFS_NULL 0
  257. #define OFS_RETURN 1
  258. #define OFS_PARM0 4 // leave 3 ofs for each parm to hold vectors
  259. #define OFS_PARM1 7
  260. #define OFS_PARM2 10
  261. #define OFS_PARM3 13
  262. #define OFS_PARM4 16
  263. #define RESERVED_OFS 28
  264. extern def_t *pr_scope;
  265. extern int pr_error_count;
  266. void PR_NewLine (void);
  267. def_t *PR_GetDef (type_t *type, char *name, def_t *scope, boolean allocate);
  268. void PR_PrintDefs (void);
  269. void PR_SkipToSemicolon (void);
  270. extern char pr_parm_names[MAX_PARMS][MAX_NAME];
  271. extern boolean pr_trace;
  272. #define G_FLOAT(o) (pr_globals[o])
  273. #define G_INT(o) (*(int *)&pr_globals[o])
  274. #define G_VECTOR(o) (&pr_globals[o])
  275. #define G_STRING(o) (strings + *(string_t *)&pr_globals[o])
  276. #define G_FUNCTION(o) (*(func_t *)&pr_globals[o])
  277. char *PR_ValueString (etype_t type, void *val);
  278. void PR_ClearGrabMacros (void);
  279. boolean PR_CompileFile (char *string, char *filename);
  280. extern boolean pr_dumpasm;
  281. extern string_t s_file; // filename for function definition
  282. extern def_t def_ret, def_parms[MAX_PARMS];
  283. //=============================================================================
  284. #define MAX_STRINGS 500000
  285. #define MAX_GLOBALS 16384
  286. #define MAX_FIELDS 1024
  287. #define MAX_STATEMENTS 65536
  288. #define MAX_FUNCTIONS 8192
  289. #define MAX_SOUNDS 1024
  290. #define MAX_MODELS 1024
  291. #define MAX_FILES 1024
  292. #define MAX_DATA_PATH 64
  293. extern char strings[MAX_STRINGS];
  294. extern int strofs;
  295. extern dstatement_t statements[MAX_STATEMENTS];
  296. extern int numstatements;
  297. extern int statement_linenums[MAX_STATEMENTS];
  298. extern dfunction_t functions[MAX_FUNCTIONS];
  299. extern int numfunctions;
  300. extern float pr_globals[MAX_REGS];
  301. extern int numpr_globals;
  302. extern char pr_immediate_string[2048];
  303. extern char precache_sounds[MAX_SOUNDS][MAX_DATA_PATH];
  304. extern int precache_sounds_block[MAX_SOUNDS];
  305. extern int numsounds;
  306. extern char precache_models[MAX_MODELS][MAX_DATA_PATH];
  307. extern int precache_models_block[MAX_SOUNDS];
  308. extern int nummodels;
  309. extern char precache_files[MAX_FILES][MAX_DATA_PATH];
  310. extern int precache_files_block[MAX_SOUNDS];
  311. extern int numfiles;
  312. int CopyString (char *str);