/indra/lscript/lscript_byteformat.h

https://bitbucket.org/lindenlab/viewer-beta/ · C Header · 556 lines · 500 code · 29 blank · 27 comment · 4 complexity · 8e981be53c78c5bb74eedf8014a3c162 MD5 · raw file

  1. /**
  2. * @file lscript_byteformat.h
  3. * @brief Shared code between compiler and assembler and LSL
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LSCRIPT_BYTEFORMAT_H
  27. #define LL_LSCRIPT_BYTEFORMAT_H
  28. // Data shared between compiler/assembler and lscript execution code
  29. #include "stdtypes.h"
  30. const S32 LSL2_VERSION_NUMBER = 0x0200;
  31. const S32 LSL2_VERSION1_END_NUMBER = 0x0101;
  32. const S32 LSL2_VERSION2_START_NUMBER = 0x0200;
  33. const S32 LSL2_MAJOR_VERSION_ONE = 1;
  34. const S32 LSL2_MAJOR_VERSION_TWO = 2;
  35. const S32 LSL2_CURRENT_MAJOR_VERSION = LSL2_MAJOR_VERSION_TWO;
  36. const S32 TOP_OF_MEMORY = 16384;
  37. typedef enum e_lscript_registers
  38. {
  39. LREG_INVALID,
  40. LREG_IP, // instruction pointer
  41. LREG_VN, // version number
  42. LREG_BP, // base pointer - what local variables are referenced from
  43. LREG_SP, // stack pointer - where the top of the stack is
  44. LREG_HR, // heap register - where in memory does the heap start
  45. LREG_HP, // heap pointer - where is the top of the heap?
  46. LREG_CS, // current state - what state are we currently in?
  47. LREG_NS, // next state - what state are we currently in?
  48. LREG_CE, // current events - what events are waiting to be handled?
  49. LREG_IE, // in event - which event handler are we currently in?
  50. LREG_ER, // event register - what events do we have active handlers for?
  51. LREG_FR, // fault register - which errors are currently active?
  52. LREG_SLR, // sleep register - are we sleeping?
  53. LREG_GVR, // global variable register - where do global variables start
  54. LREG_GFR, // global function register - where do global functions start
  55. LREG_SR, // state register - where do states start
  56. LREG_TM, // top of memory - where is the top of memory
  57. LREG_PR, // parameter register - data passed to script from launcher
  58. LREG_ESR, // energy supply register - how much energy do we have on board?
  59. LREG_NCE, // 64 bit current envents - what events are waiting to be handled?
  60. LREG_NIE, // 64 bit in event - which event handler are we currently in?
  61. LREG_NER, // 64 bit event register - what events do we have active handlers for?
  62. LREG_EOF
  63. } LSCRIPTRegisters;
  64. const S32 gLSCRIPTRegisterAddresses[LREG_EOF] = /* Flawfinder: ignore */
  65. {
  66. 0, // LREG_INVALID
  67. 4, // LREG_IP
  68. 8, // LREG_VN
  69. 12, // LREG_BP
  70. 16, // LREG_SP
  71. 20, // LREG_HR
  72. 24, // LREG_HP
  73. 28, // LREG_CS
  74. 32, // LREG_NS
  75. 36, // LREG_CE
  76. 40, // LREG_IE
  77. 44, // LREG_ER
  78. 48, // LREG_FR
  79. 52, // LREG_SLR
  80. 56, // LREG_GVR
  81. 60, // LREG_GFR
  82. 72, // LREG_SR
  83. 0, // LREG_TM
  84. 64, // LREG_PR
  85. 68, // LREG_ESR
  86. 76, // LREG_NCE
  87. 84, // LREG_NIE
  88. 92, // LREG_NER
  89. };
  90. const char * const gLSCRIPTRegisterNames[LREG_EOF] =
  91. {
  92. "INVALID", // LREG_INVALID
  93. "IP", // LREG_IP
  94. "VN", // LREG_VN
  95. "BP", // LREG_BP
  96. "SP", // LREG_SP
  97. "HR", // LREG_HR
  98. "HP", // LREG_HP
  99. "CS", // LREG_CS
  100. "NS", // LREG_NS
  101. "CE", // LREG_CE
  102. "IE", // LREG_IE
  103. "ER", // LREG_ER
  104. "FR", // LREG_FR
  105. "SLR", // LREG_SLR
  106. "GVR", // LREG_GVR
  107. "GFR", // LREG_GFR
  108. "SR", // LREG_SR
  109. "TM", // LREG_TM
  110. "PR", // LREG_PR
  111. "ESR", // LREG_ESR
  112. "NCE", // LREG_NCE
  113. "NIE", // LREG_NIE
  114. "NER", // LREG_NER
  115. };
  116. typedef enum e_lscript_op_codes
  117. {
  118. LOPC_INVALID,
  119. LOPC_NOOP,
  120. LOPC_POP,
  121. LOPC_POPS,
  122. LOPC_POPL,
  123. LOPC_POPV,
  124. LOPC_POPQ,
  125. LOPC_POPARG,
  126. LOPC_POPIP,
  127. LOPC_POPBP,
  128. LOPC_POPSP,
  129. LOPC_POPSLR,
  130. LOPC_DUP,
  131. LOPC_DUPS,
  132. LOPC_DUPL,
  133. LOPC_DUPV,
  134. LOPC_DUPQ,
  135. LOPC_STORE,
  136. LOPC_STORES,
  137. LOPC_STOREL,
  138. LOPC_STOREV,
  139. LOPC_STOREQ,
  140. LOPC_STOREG,
  141. LOPC_STOREGS,
  142. LOPC_STOREGL,
  143. LOPC_STOREGV,
  144. LOPC_STOREGQ,
  145. LOPC_LOADP,
  146. LOPC_LOADSP,
  147. LOPC_LOADLP,
  148. LOPC_LOADVP,
  149. LOPC_LOADQP,
  150. LOPC_LOADGP,
  151. LOPC_LOADGLP,
  152. LOPC_LOADGSP,
  153. LOPC_LOADGVP,
  154. LOPC_LOADGQP,
  155. LOPC_PUSH,
  156. LOPC_PUSHS,
  157. LOPC_PUSHL,
  158. LOPC_PUSHV,
  159. LOPC_PUSHQ,
  160. LOPC_PUSHG,
  161. LOPC_PUSHGS,
  162. LOPC_PUSHGL,
  163. LOPC_PUSHGV,
  164. LOPC_PUSHGQ,
  165. LOPC_PUSHIP,
  166. LOPC_PUSHBP,
  167. LOPC_PUSHSP,
  168. LOPC_PUSHARGB,
  169. LOPC_PUSHARGI,
  170. LOPC_PUSHARGF,
  171. LOPC_PUSHARGS,
  172. LOPC_PUSHARGV,
  173. LOPC_PUSHARGQ,
  174. LOPC_PUSHE,
  175. LOPC_PUSHEV,
  176. LOPC_PUSHEQ,
  177. LOPC_PUSHARGE,
  178. LOPC_ADD,
  179. LOPC_SUB,
  180. LOPC_MUL,
  181. LOPC_DIV,
  182. LOPC_MOD,
  183. LOPC_EQ,
  184. LOPC_NEQ,
  185. LOPC_LEQ,
  186. LOPC_GEQ,
  187. LOPC_LESS,
  188. LOPC_GREATER,
  189. LOPC_BITAND,
  190. LOPC_BITOR,
  191. LOPC_BITXOR,
  192. LOPC_BOOLAND,
  193. LOPC_BOOLOR,
  194. LOPC_NEG,
  195. LOPC_BITNOT,
  196. LOPC_BOOLNOT,
  197. LOPC_JUMP,
  198. LOPC_JUMPIF,
  199. LOPC_JUMPNIF,
  200. LOPC_STATE,
  201. LOPC_CALL,
  202. LOPC_RETURN,
  203. LOPC_CAST,
  204. LOPC_STACKTOS,
  205. LOPC_STACKTOL,
  206. LOPC_PRINT,
  207. LOPC_CALLLIB,
  208. LOPC_CALLLIB_TWO_BYTE,
  209. LOPC_SHL,
  210. LOPC_SHR,
  211. LOPC_EOF
  212. } LSCRIPTOpCodesEnum;
  213. const U8 LSCRIPTOpCodes[LOPC_EOF] =
  214. {
  215. 0x00, // LOPC_INVALID
  216. 0x00, // LOPC_NOOP
  217. 0x01, // LOPC_POP
  218. 0x02, // LOPC_POPS
  219. 0x03, // LOPC_POPL
  220. 0x04, // LOPC_POPV
  221. 0x05, // LOPC_POPQ
  222. 0x06, // LOPC_POPARG
  223. 0x07, // LOPC_POPIP
  224. 0x08, // LOPC_POPBP
  225. 0x09, // LOPC_POPSP
  226. 0x0a, // LOPC_POPSLR
  227. 0x20, // LOPC_DUP
  228. 0x21, // LOPC_DUPS
  229. 0x22, // LOPC_DUPL
  230. 0x23, // LOPC_DUPV
  231. 0x24, // LOPC_DUPQ
  232. 0x30, // LOPC_STORE
  233. 0x31, // LOPC_STORES
  234. 0x32, // LOPC_STOREL
  235. 0x33, // LOPC_STOREV
  236. 0x34, // LOPC_STOREQ
  237. 0x35, // LOPC_STOREG
  238. 0x36, // LOPC_STOREGS
  239. 0x37, // LOPC_STOREGL
  240. 0x38, // LOPC_STOREGV
  241. 0x39, // LOPC_STOREGQ
  242. 0x3a, // LOPC_LOADP
  243. 0x3b, // LOPC_LOADSP
  244. 0x3c, // LOPC_LOADLP
  245. 0x3d, // LOPC_LOADVP
  246. 0x3e, // LOPC_LOADQP
  247. 0x3f, // LOPC_LOADGP
  248. 0x40, // LOPC_LOADGSP
  249. 0x41, // LOPC_LOADGLP
  250. 0x42, // LOPC_LOADGVP
  251. 0x43, // LOPC_LOADGQP
  252. 0x50, // LOPC_PUSH
  253. 0x51, // LOPC_PUSHS
  254. 0x52, // LOPC_PUSHL
  255. 0x53, // LOPC_PUSHV
  256. 0x54, // LOPC_PUSHQ
  257. 0x55, // LOPC_PUSHG
  258. 0x56, // LOPC_PUSHGS
  259. 0x57, // LOPC_PUSHGL
  260. 0x58, // LOPC_PUSHGV
  261. 0x59, // LOPC_PUSHGQ
  262. 0x5a, // LOPC_PUSHIP
  263. 0x5b, // LOPC_PUSHBP
  264. 0x5c, // LOPC_PUSHSP
  265. 0x5d, // LOPC_PUSHARGB
  266. 0x5e, // LOPC_PUSHARGI
  267. 0x5f, // LOPC_PUSHARGF
  268. 0x60, // LOPC_PUSHARGS
  269. 0x61, // LOPC_PUSHARGV
  270. 0x62, // LOPC_PUSHARGQ
  271. 0x63, // LOPC_PUSHE
  272. 0x64, // LOPC_PUSHEV
  273. 0x65, // LOPC_PUSHEQ
  274. 0x66, // LOPC_PUSHARGE
  275. 0x70, // LOPC_ADD
  276. 0x71, // LOPC_SUB
  277. 0x72, // LOPC_MUL
  278. 0x73, // LOPC_DIV
  279. 0x74, // LOPC_MOD
  280. 0x75, // LOPC_EQ
  281. 0x76, // LOPC_NEQ
  282. 0x77, // LOPC_LEQ
  283. 0x78, // LOPC_GEQ
  284. 0x79, // LOPC_LESS
  285. 0x7a, // LOPC_GREATER
  286. 0x7b, // LOPC_BITAND
  287. 0x7c, // LOPC_BITOR
  288. 0x7d, // LOPC_BITXOR
  289. 0x7e, // LOPC_BOOLAND
  290. 0x7f, // LOPC_BOOLOR
  291. 0x80, // LOPC_NEG
  292. 0x81, // LOPC_BITNOT
  293. 0x82, // LOPC_BOOLNOT
  294. 0x90, // LOPC_JUMP
  295. 0x91, // LOPC_JUMPIF
  296. 0x92, // LOPC_JUMPNIF
  297. 0x93, // LOPC_STATE
  298. 0x94, // LOPC_CALL
  299. 0x95, // LOPC_RETURN
  300. 0xa0, // LOPC_CAST
  301. 0xb0, // LOPC_STACKTOS
  302. 0xb1, // LOPC_STACKTOL
  303. 0xc0, // LOPC_PRINT
  304. 0xd0, // LOPC_CALLLIB
  305. 0xd1, // LOPC_CALLLIB_TWO_BYTE
  306. 0xe0, // LOPC_SHL
  307. 0xe1 // LOPC_SHR
  308. };
  309. typedef enum e_lscript_state_event_type
  310. {
  311. LSTT_NULL,
  312. LSTT_STATE_ENTRY,
  313. LSTT_STATE_EXIT,
  314. LSTT_TOUCH_START,
  315. LSTT_TOUCH,
  316. LSTT_TOUCH_END,
  317. LSTT_COLLISION_START,
  318. LSTT_COLLISION,
  319. LSTT_COLLISION_END,
  320. LSTT_LAND_COLLISION_START,
  321. LSTT_LAND_COLLISION,
  322. LSTT_LAND_COLLISION_END,
  323. LSTT_TIMER,
  324. LSTT_CHAT,
  325. LSTT_REZ,
  326. LSTT_SENSOR,
  327. LSTT_NO_SENSOR,
  328. LSTT_CONTROL,
  329. LSTT_MONEY,
  330. LSTT_EMAIL,
  331. LSTT_AT_TARGET,
  332. LSTT_NOT_AT_TARGET,
  333. LSTT_AT_ROT_TARGET,
  334. LSTT_NOT_AT_ROT_TARGET,
  335. LSTT_RTPERMISSIONS,
  336. LSTT_INVENTORY,
  337. LSTT_ATTACH,
  338. LSTT_DATASERVER,
  339. LSTT_LINK_MESSAGE,
  340. LSTT_MOVING_START,
  341. LSTT_MOVING_END,
  342. LSTT_OBJECT_REZ,
  343. LSTT_REMOTE_DATA,
  344. LSTT_HTTP_RESPONSE,
  345. LSTT_HTTP_REQUEST,
  346. LSTT_EOF,
  347. LSTT_STATE_BEGIN = LSTT_STATE_ENTRY,
  348. LSTT_STATE_END = LSTT_EOF
  349. } LSCRIPTStateEventType;
  350. const U64 LSCRIPTStateBitField[LSTT_EOF] =
  351. {
  352. 0x0000000000000000, // LSTT_NULL
  353. 0x0000000000000001, // LSTT_STATE_ENTRY
  354. 0x0000000000000002, // LSTT_STATE_EXIT
  355. 0x0000000000000004, // LSTT_TOUCH_START
  356. 0x0000000000000008, // LSTT_TOUCH
  357. 0x0000000000000010, // LSTT_TOUCH_END
  358. 0x0000000000000020, // LSTT_COLLISION_START
  359. 0x0000000000000040, // LSTT_COLLISION
  360. 0x0000000000000080, // LSTT_COLLISION_END
  361. 0x0000000000000100, // LSTT_LAND_COLLISION_START
  362. 0x0000000000000200, // LSTT_LAND_COLLISION
  363. 0x0000000000000400, // LSTT_LAND_COLLISION_END
  364. 0x0000000000000800, // LSTT_TIMER
  365. 0x0000000000001000, // LSTT_CHAT
  366. 0x0000000000002000, // LSTT_REZ
  367. 0x0000000000004000, // LSTT_SENSOR
  368. 0x0000000000008000, // LSTT_NO_SENSOR
  369. 0x0000000000010000, // LSTT_CONTROL
  370. 0x0000000000020000, // LSTT_MONEY
  371. 0x0000000000040000, // LSTT_EMAIL
  372. 0x0000000000080000, // LSTT_AT_TARGET
  373. 0x0000000000100000, // LSTT_NOT_AT_TARGET
  374. 0x0000000000200000, // LSTT_AT_ROT_TARGET
  375. 0x0000000000400000, // LSTT_NOT_AT_ROT_TARGET
  376. 0x0000000000800000, // LSTT_RTPERMISSIONS
  377. 0x0000000001000000, // LSTT_INVENTORY
  378. 0x0000000002000000, // LSTT_ATTACH
  379. 0x0000000004000000, // LSTT_DATASERVER
  380. 0x0000000008000000, // LSTT_LINK_MESSAGE
  381. 0x0000000010000000, // LSTT_MOVING_START
  382. 0x0000000020000000, // LSTT_MOVING_END
  383. 0x0000000040000000, // LSTT_OBJECT_REZ
  384. 0x0000000080000000, // LSTT_REMOTE_DATA
  385. 0x0000000100000000LL, // LSTT_HTTP_RESPOSE
  386. 0x0000000200000000LL // LSTT_HTTP_REQUEST
  387. };
  388. inline S32 get_event_handler_jump_position(U64 bit_field, LSCRIPTStateEventType type)
  389. {
  390. S32 count = 0, position = LSTT_STATE_ENTRY;
  391. while (position < type)
  392. {
  393. if (bit_field & 0x1)
  394. {
  395. count++;
  396. }
  397. bit_field >>= 1;
  398. position++;
  399. }
  400. return count;
  401. }
  402. inline S32 get_number_of_event_handlers(U64 bit_field)
  403. {
  404. S32 count = 0, position = 0;
  405. while (position < LSTT_EOF)
  406. {
  407. if (bit_field & 0x1)
  408. {
  409. count++;
  410. }
  411. bit_field >>= 1;
  412. position++;
  413. }
  414. return count;
  415. }
  416. typedef enum e_lscript_types
  417. {
  418. LST_NULL,
  419. LST_INTEGER,
  420. LST_FLOATINGPOINT,
  421. LST_STRING,
  422. LST_KEY,
  423. LST_VECTOR,
  424. LST_QUATERNION,
  425. LST_LIST,
  426. LST_UNDEFINED,
  427. LST_EOF
  428. } LSCRIPTType;
  429. const U8 LSCRIPTTypeByte[LST_EOF] =
  430. {
  431. LST_NULL,
  432. LST_INTEGER,
  433. LST_FLOATINGPOINT,
  434. LST_STRING,
  435. LST_KEY,
  436. LST_VECTOR,
  437. LST_QUATERNION,
  438. LST_LIST,
  439. LST_NULL,
  440. };
  441. const U8 LSCRIPTTypeHi4Bits[LST_EOF] =
  442. {
  443. LST_NULL,
  444. LST_INTEGER << 4,
  445. LST_FLOATINGPOINT << 4,
  446. LST_STRING << 4,
  447. LST_KEY << 4,
  448. LST_VECTOR << 4,
  449. LST_QUATERNION << 4,
  450. LST_LIST << 4,
  451. LST_UNDEFINED << 4,
  452. };
  453. const char * const LSCRIPTTypeNames[LST_EOF] = /*Flawfinder: ignore*/
  454. {
  455. "VOID",
  456. "integer",
  457. "float",
  458. "string",
  459. "key",
  460. "vector",
  461. "quaternion",
  462. "list",
  463. "invalid"
  464. };
  465. const S32 LSCRIPTDataSize[LST_EOF] =
  466. {
  467. 0, // VOID
  468. 4, // integer
  469. 4, // float
  470. 4, // string
  471. 4, // key
  472. 12, // vector
  473. 16, // quaternion
  474. 4, // list
  475. 0 // invalid
  476. };
  477. typedef enum e_lscript_runtime_faults
  478. {
  479. LSRF_INVALID,
  480. LSRF_MATH,
  481. LSRF_STACK_HEAP_COLLISION,
  482. LSRF_BOUND_CHECK_ERROR,
  483. LSRF_HEAP_ERROR,
  484. LSRF_VERSION_MISMATCH,
  485. LSRF_MISSING_INVENTORY,
  486. LSRF_SANDBOX,
  487. LSRF_CHAT_OVERRUN,
  488. LSRF_TOO_MANY_LISTENS,
  489. LSRF_NESTING_LISTS,
  490. LSRF_CLI,
  491. LSRF_EOF
  492. } LSCRIPTRunTimeFaults;
  493. extern const char* LSCRIPTRunTimeFaultStrings[LSRF_EOF]; /*Flawfinder: ignore*/
  494. typedef enum e_lscript_runtime_permissions
  495. {
  496. SCRIPT_PERMISSION_DEBIT,
  497. SCRIPT_PERMISSION_TAKE_CONTROLS,
  498. SCRIPT_PERMISSION_REMAP_CONTROLS,
  499. SCRIPT_PERMISSION_TRIGGER_ANIMATION,
  500. SCRIPT_PERMISSION_ATTACH,
  501. SCRIPT_PERMISSION_RELEASE_OWNERSHIP,
  502. SCRIPT_PERMISSION_CHANGE_LINKS,
  503. SCRIPT_PERMISSION_CHANGE_JOINTS,
  504. SCRIPT_PERMISSION_CHANGE_PERMISSIONS,
  505. SCRIPT_PERMISSION_TRACK_CAMERA,
  506. SCRIPT_PERMISSION_CONTROL_CAMERA,
  507. SCRIPT_PERMISSION_EOF
  508. } LSCRIPTRunTimePermissions;
  509. const U32 LSCRIPTRunTimePermissionBits[SCRIPT_PERMISSION_EOF] =
  510. {
  511. (0x1 << 1), // SCRIPT_PERMISSION_DEBIT,
  512. (0x1 << 2), // SCRIPT_PERMISSION_TAKE_CONTROLS,
  513. (0x1 << 3), // SCRIPT_PERMISSION_REMAP_CONTROLS,
  514. (0x1 << 4), // SCRIPT_PERMISSION_TRIGGER_ANIMATION,
  515. (0x1 << 5), // SCRIPT_PERMISSION_ATTACH,
  516. (0x1 << 6), // SCRIPT_PERMISSION_RELEASE_OWNERSHIP,
  517. (0x1 << 7), // SCRIPT_PERMISSION_CHANGE_LINKS,
  518. (0x1 << 8), // SCRIPT_PERMISSION_CHANGE_JOINTS,
  519. (0x1 << 9), // SCRIPT_PERMISSION_CHANGE_PERMISSIONS
  520. (0x1 << 10),// SCRIPT_PERMISSION_TRACK_CAMERA
  521. (0x1 << 11),// SCRIPT_PERMISSION_CONTROL_CAMERA
  522. };
  523. // http_request string constants
  524. extern const char* URL_REQUEST_GRANTED;
  525. extern const char* URL_REQUEST_DENIED;
  526. extern const U64 LSL_HTTP_REQUEST_TIMEOUT_USEC;
  527. #endif