From 95956bb8cb9513c429b9749426720be94f4cf5a8 Mon Sep 17 00:00:00 2001 From: Michal Krol Date: Fri, 18 Sep 2009 11:19:25 +0200 Subject: glsl/pp: Define a public interface for external modules. Make sl_pp_context struct opaque. Move all public declarations to sl_pp_public.h. --- src/glsl/pp/sl_pp_public.h | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/glsl/pp/sl_pp_public.h (limited to 'src/glsl/pp/sl_pp_public.h') diff --git a/src/glsl/pp/sl_pp_public.h b/src/glsl/pp/sl_pp_public.h new file mode 100644 index 00000000000..b1d92d02a7a --- /dev/null +++ b/src/glsl/pp/sl_pp_public.h @@ -0,0 +1,63 @@ +/************************************************************************** + * + * Copyright 2009 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + +#ifndef SL_PP_PUBLIC_H +#define SL_PP_PUBLIC_H + + +struct sl_pp_context; + + +#include "sl_pp_purify.h" +#include "sl_pp_token.h" + + +struct sl_pp_context * +sl_pp_context_create(void); + +void +sl_pp_context_destroy(struct sl_pp_context *context); + +const char * +sl_pp_context_error_message(const struct sl_pp_context *context); + +const char * +sl_pp_context_cstr(const struct sl_pp_context *context, + int offset); + +int +sl_pp_version(struct sl_pp_context *context, + const struct sl_pp_token_info *input, + unsigned int *version, + unsigned int *tokens_eaten); + +int +sl_pp_process(struct sl_pp_context *context, + const struct sl_pp_token_info *input, + struct sl_pp_token_info **output); + +#endif /* SL_PP_PUBLIC_H */ -- cgit v1.2.3 From abe1f332983e5c70d75b5ae83f06c0dfdd081a26 Mon Sep 17 00:00:00 2001 From: Michal Krol Date: Sat, 21 Nov 2009 20:41:48 +0100 Subject: glsl/pp: Do purification and tokenisation in a single step. --- src/glsl/pp/sl_pp_context.c | 7 + src/glsl/pp/sl_pp_context.h | 11 +- src/glsl/pp/sl_pp_dict.c | 1 + src/glsl/pp/sl_pp_public.h | 4 + src/glsl/pp/sl_pp_token.c | 844 ++++++++++++++++++++++++++++---------------- src/glsl/pp/sl_pp_token.h | 1 + 6 files changed, 561 insertions(+), 307 deletions(-) (limited to 'src/glsl/pp/sl_pp_public.h') diff --git a/src/glsl/pp/sl_pp_context.c b/src/glsl/pp/sl_pp_context.c index 8ce189d955c..134588d9066 100644 --- a/src/glsl/pp/sl_pp_context.c +++ b/src/glsl/pp/sl_pp_context.c @@ -46,6 +46,12 @@ sl_pp_context_create(void) return NULL; } + context->getc_buf = malloc(64 * sizeof(char)); + if (!context->getc_buf) { + sl_pp_context_destroy(context); + return NULL; + } + context->macro_tail = &context->macro; context->if_ptr = SL_PP_MAX_IF_NESTING; context->if_value = 1; @@ -62,6 +68,7 @@ sl_pp_context_destroy(struct sl_pp_context *context) if (context) { free(context->cstr_pool); sl_pp_macro_free(context->macro); + free(context->getc_buf); free(context); } } diff --git a/src/glsl/pp/sl_pp_context.h b/src/glsl/pp/sl_pp_context.h index 6b8cc2f960d..569a2d735b9 100644 --- a/src/glsl/pp/sl_pp_context.h +++ b/src/glsl/pp/sl_pp_context.h @@ -30,6 +30,7 @@ #include "sl_pp_dict.h" #include "sl_pp_macro.h" +#include "sl_pp_purify.h" #define SL_PP_MAX_IF_NESTING 64 @@ -53,10 +54,12 @@ struct sl_pp_context { unsigned int line; unsigned int file; -}; -int -sl_pp_context_add_unique_str(struct sl_pp_context *context, - const char *str); + struct sl_pp_purify_state pure; + + char *getc_buf; + unsigned int getc_buf_size; + unsigned int getc_buf_capacity; +}; #endif /* SL_PP_CONTEXT_H */ diff --git a/src/glsl/pp/sl_pp_dict.c b/src/glsl/pp/sl_pp_dict.c index 82fb9127b50..2dd77a69e90 100644 --- a/src/glsl/pp/sl_pp_dict.c +++ b/src/glsl/pp/sl_pp_dict.c @@ -25,6 +25,7 @@ * **************************************************************************/ +#include "sl_pp_public.h" #include "sl_pp_context.h" #include "sl_pp_dict.h" diff --git a/src/glsl/pp/sl_pp_public.h b/src/glsl/pp/sl_pp_public.h index b1d92d02a7a..8317c7e378a 100644 --- a/src/glsl/pp/sl_pp_public.h +++ b/src/glsl/pp/sl_pp_public.h @@ -45,6 +45,10 @@ sl_pp_context_destroy(struct sl_pp_context *context); const char * sl_pp_context_error_message(const struct sl_pp_context *context); +int +sl_pp_context_add_unique_str(struct sl_pp_context *context, + const char *str); + const char * sl_pp_context_cstr(const struct sl_pp_context *context, int offset); diff --git a/src/glsl/pp/sl_pp_token.c b/src/glsl/pp/sl_pp_token.c index f232dafc682..03f2f09cfd6 100644 --- a/src/glsl/pp/sl_pp_token.c +++ b/src/glsl/pp/sl_pp_token.c @@ -25,12 +25,106 @@ * **************************************************************************/ +#include #include #include +#include "sl_pp_public.h" #include "sl_pp_context.h" #include "sl_pp_token.h" +#define PURE_ERROR 256 + +int +_pure_getc(struct sl_pp_context *context) +{ + char c; + unsigned int current_line; + + if (context->getc_buf_size) { + return context->getc_buf[--context->getc_buf_size]; + } + + if (sl_pp_purify_getc(&context->pure, &c, ¤t_line, context->error_msg, sizeof(context->error_msg)) == 0) { + return PURE_ERROR; + } + return c; +} + + +void +_pure_ungetc(struct sl_pp_context *context, + int c) +{ + assert(c != PURE_ERROR); + + if (context->getc_buf_size == context->getc_buf_capacity) { + context->getc_buf_capacity += 64; + context->getc_buf = realloc(context->getc_buf, context->getc_buf_capacity * sizeof(char)); + assert(context->getc_buf); + } + + context->getc_buf[context->getc_buf_size++] = (char)c; +} + + +struct lookahead_state { + char buf[256]; + unsigned int pos; + struct sl_pp_context *context; +}; + + +static void +_lookahead_init(struct lookahead_state *lookahead, + struct sl_pp_context *context) +{ + lookahead->pos = 0; + lookahead->context = context; +} + + +static unsigned int +_lookahead_tell(const struct lookahead_state *lookahead) +{ + return lookahead->pos; +} + + +static const void * +_lookahead_buf(const struct lookahead_state *lookahead) +{ + return lookahead->buf; +} + + +static void +_lookahead_revert(struct lookahead_state *lookahead, + unsigned int pos) +{ + assert(pos <= lookahead->pos); + + while (lookahead->pos > pos) { + _pure_ungetc(lookahead->context, lookahead->buf[--lookahead->pos]); + } +} + + +static int +_lookahead_getc(struct lookahead_state *lookahead) +{ + int c; + + assert(lookahead->pos < sizeof(lookahead->buf) / sizeof(lookahead->buf[0])); + + c = _pure_getc(lookahead->context); + if (c != PURE_ERROR) { + lookahead->buf[lookahead->pos++] = (char)c; + } + return c; +} + + static int _is_identifier_char(char c) { @@ -40,32 +134,51 @@ _is_identifier_char(char c) static int _tokenise_identifier(struct sl_pp_context *context, - const char **pinput, - struct sl_pp_token_info *info) + struct sl_pp_token_info *out) { - const char *input = *pinput; + int c; char identifier[256]; /* XXX: Remove this artifical limit. */ unsigned int i = 0; - info->token = SL_PP_IDENTIFIER; - info->data.identifier = -1; + out->token = SL_PP_IDENTIFIER; + out->data.identifier = -1; - identifier[i++] = *input++; - while (_is_identifier_char(*input)) { - if (i >= sizeof(identifier) - 1) { - strcpy(context->error_msg, "out of memory"); + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + identifier[i++] = (char)c; + for (;;) { + c = _pure_getc(context); + if (c == PURE_ERROR) { return -1; } - identifier[i++] = *input++; + + if (_is_identifier_char((char)c)) { + if (i >= sizeof(identifier) / sizeof(char) - 1) { + strcpy(context->error_msg, "out of memory"); + _pure_ungetc(context, c); + while (i) { + _pure_ungetc(context, identifier[--i]); + } + return -1; + } + identifier[i++] = (char)c; + } else { + _pure_ungetc(context, c); + break; + } } - identifier[i++] = '\0'; + identifier[i] = '\0'; - info->data.identifier = sl_pp_context_add_unique_str(context, identifier); - if (info->data.identifier == -1) { + out->data.identifier = sl_pp_context_add_unique_str(context, identifier); + if (out->data.identifier == -1) { + while (i) { + _pure_ungetc(context, identifier[--i]); + } return -1; } - *pinput = input; return 0; } @@ -74,12 +187,18 @@ _tokenise_identifier(struct sl_pp_context *context, * Return the number of consecutive decimal digits in the input stream. */ static unsigned int -_parse_float_digits(const char *input) +_parse_float_digits(struct lookahead_state *lookahead) { - unsigned int eaten = 0; + unsigned int eaten; + + for (eaten = 0;; eaten++) { + unsigned int pos = _lookahead_tell(lookahead); + char c = _lookahead_getc(lookahead); - while (input[eaten] >= '0' && input[eaten] <= '9') { - eaten++; + if (c < '0' || c > '9') { + _lookahead_revert(lookahead, pos); + break; + } } return eaten; } @@ -96,29 +215,33 @@ _parse_float_digits(const char *input) * of eaten characters from the input stream. */ static unsigned int -_parse_float_frac(const char *input) +_parse_float_frac(struct lookahead_state *lookahead) { + unsigned int pos; + int c; unsigned int eaten; - if (input[0] == '.') { - eaten = _parse_float_digits(&input[1]); + pos = _lookahead_tell(lookahead); + c = _lookahead_getc(lookahead); + if (c == '.') { + eaten = _parse_float_digits(lookahead); if (eaten) { return eaten + 1; } + _lookahead_revert(lookahead, pos); return 0; } - eaten = _parse_float_digits(input); - if (eaten && input[eaten] == '.') { - unsigned int trailing; - - trailing = _parse_float_digits(&input[eaten + 1]); - if (trailing) { - return eaten + trailing + 1; + _lookahead_revert(lookahead, pos); + eaten = _parse_float_digits(lookahead); + if (eaten) { + c = _lookahead_getc(lookahead); + if (c == '.') { + return eaten + 1 + _parse_float_digits(lookahead); } - return eaten + 1; } + _lookahead_revert(lookahead, pos); return 0; } @@ -133,22 +256,31 @@ _parse_float_frac(const char *input) * of eaten characters from the input stream. */ static unsigned int -_parse_float_exp(const char *input) +_parse_float_exp(struct lookahead_state *lookahead) { + unsigned int pos, pos2; + int c; unsigned int eaten, digits; - if (input[0] != 'e' && input[0] != 'E') { + pos = _lookahead_tell(lookahead); + c = _lookahead_getc(lookahead); + if (c != 'e' && c != 'E') { + _lookahead_revert(lookahead, pos); return 0; } - if (input[1] == '-' || input[1] == '+') { + pos2 = _lookahead_tell(lookahead); + c = _lookahead_getc(lookahead); + if (c == '-' || c == '+') { eaten = 2; } else { + _lookahead_revert(lookahead, pos2); eaten = 1; } - digits = _parse_float_digits(&input[eaten]); + digits = _parse_float_digits(lookahead); if (!digits) { + _lookahead_revert(lookahead, pos); return 0; } @@ -166,86 +298,117 @@ _parse_float_exp(const char *input) * of eaten characters from the input stream. */ static unsigned int -_parse_float(const char *input) +_parse_float(struct lookahead_state *lookahead) { unsigned int eaten; - eaten = _parse_float_frac(input); + eaten = _parse_float_frac(lookahead); if (eaten) { - unsigned int exponent; + unsigned int pos; + int c; - exponent = _parse_float_exp(&input[eaten]); - if (exponent) { - eaten += exponent; - } + eaten += _parse_float_exp(lookahead); - if (input[eaten] == 'f' || input[eaten] == 'F') { + pos = _lookahead_tell(lookahead); + c = _lookahead_getc(lookahead); + if (c == 'f' || c == 'F') { eaten++; + } else { + _lookahead_revert(lookahead, pos); } return eaten; } - eaten = _parse_float_digits(input); + eaten = _parse_float_digits(lookahead); if (eaten) { unsigned int exponent; - exponent = _parse_float_exp(&input[eaten]); + exponent = _parse_float_exp(lookahead); if (exponent) { + unsigned int pos; + int c; + eaten += exponent; - if (input[eaten] == 'f' || input[eaten] == 'F') { + pos = _lookahead_tell(lookahead); + c = _lookahead_getc(lookahead); + if (c == 'f' || c == 'F') { eaten++; + } else { + _lookahead_revert(lookahead, pos); } return eaten; } } + _lookahead_revert(lookahead, 0); return 0; } static unsigned int -_parse_hex(const char *input) +_parse_hex(struct lookahead_state *lookahead) { + int c; unsigned int n; - if (input[0] != '0') { + c = _lookahead_getc(lookahead); + if (c != '0') { + _lookahead_revert(lookahead, 0); return 0; } - if (input[1] != 'x' && input[1] != 'X') { + c = _lookahead_getc(lookahead); + if (c != 'x' && c != 'X') { + _lookahead_revert(lookahead, 0); return 0; } - n = 2; - while ((input[n] >= '0' && input[n] <= '9') || - (input[n] >= 'a' && input[n] <= 'f') || - (input[n] >= 'A' && input[n] <= 'F')) { - n++; + for (n = 2;;) { + unsigned int pos = _lookahead_tell(lookahead); + + c = _lookahead_getc(lookahead); + if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { + n++; + } else { + _lookahead_revert(lookahead, pos); + break; + } } if (n > 2) { return n; } + _lookahead_revert(lookahead, 0); return 0; } static unsigned int -_parse_oct(const char *input) +_parse_oct(struct lookahead_state *lookahead) { + int c; unsigned int n; - if (input[0] != '0') { + c = _lookahead_getc(lookahead); + if (c != '0') { + _lookahead_revert(lookahead, 0); return 0; } - n = 1; - while ((input[n] >= '0' && input[n] <= '7')) { - n++; + for (n = 1;;) { + unsigned int pos = _lookahead_tell(lookahead); + + c = _lookahead_getc(lookahead); + if ((c >= '0' && c <= '7')) { + n++; + } else { + _lookahead_revert(lookahead, pos); + break; + } } return n; @@ -253,12 +416,20 @@ _parse_oct(const char *input) static unsigned int -_parse_dec(const char *input) +_parse_dec(struct lookahead_state *lookahead) { unsigned int n = 0; - while ((input[n] >= '0' && input[n] <= '9')) { - n++; + for (;;) { + unsigned int pos = _lookahead_tell(lookahead); + int c = _lookahead_getc(lookahead); + + if ((c >= '0' && c <= '9')) { + n++; + } else { + _lookahead_revert(lookahead, pos); + break; + } } return n; @@ -267,327 +438,394 @@ _parse_dec(const char *input) static int _tokenise_number(struct sl_pp_context *context, - const char **pinput, - struct sl_pp_token_info *info) + struct sl_pp_token_info *out) { - const char *input = *pinput; + struct lookahead_state lookahead; unsigned int eaten; unsigned int is_float = 0; + unsigned int pos; + int c; char number[256]; /* XXX: Remove this artifical limit. */ - eaten = _parse_float(input); + _lookahead_init(&lookahead, context); + + eaten = _parse_float(&lookahead); if (!eaten) { - eaten = _parse_hex(input); + eaten = _parse_hex(&lookahead); if (!eaten) { - eaten = _parse_oct(input); + eaten = _parse_oct(&lookahead); if (!eaten) { - eaten = _parse_dec(input); + eaten = _parse_dec(&lookahead); } } } else { is_float = 1; } - if (!eaten || _is_identifier_char(input[eaten])) { + if (!eaten) { strcpy(context->error_msg, "expected a number"); return -1; } + pos = _lookahead_tell(&lookahead); + c = _lookahead_getc(&lookahead); + _lookahead_revert(&lookahead, pos); + + if (_is_identifier_char(c)) { + strcpy(context->error_msg, "expected a number"); + _lookahead_revert(&lookahead, 0); + return -1; + } + if (eaten > sizeof(number) - 1) { strcpy(context->error_msg, "out of memory"); + _lookahead_revert(&lookahead, 0); return -1; } - memcpy(number, input, eaten); + assert(_lookahead_tell(&lookahead) == eaten); + + memcpy(number, _lookahead_buf(&lookahead), eaten); number[eaten] = '\0'; if (is_float) { - info->token = SL_PP_FLOAT; - info->data._float = sl_pp_context_add_unique_str(context, number); - if (info->data._float == -1) { + out->token = SL_PP_FLOAT; + out->data._float = sl_pp_context_add_unique_str(context, number); + if (out->data._float == -1) { + _lookahead_revert(&lookahead, 0); return -1; } } else { - info->token = SL_PP_UINT; - info->data._uint = sl_pp_context_add_unique_str(context, number); - if (info->data._uint == -1) { + out->token = SL_PP_UINT; + out->data._uint = sl_pp_context_add_unique_str(context, number); + if (out->data._uint == -1) { + _lookahead_revert(&lookahead, 0); return -1; } } - *pinput = input + eaten; return 0; } int -sl_pp_tokenise(struct sl_pp_context *context, - const char *input, - struct sl_pp_token_info **output) +sl_pp_token_get(struct sl_pp_context *context, + struct sl_pp_token_info *out) { - struct sl_pp_token_info *out = NULL; - unsigned int out_len = 0; - unsigned int out_max = 0; + int c = _pure_getc(context); - for (;;) { - struct sl_pp_token_info info; + switch (c) { + case ' ': + case '\t': + out->token = SL_PP_WHITESPACE; + break; - switch (*input) { - case ' ': - case '\t': - input++; - info.token = SL_PP_WHITESPACE; - break; + case '\n': + out->token = SL_PP_NEWLINE; + break; - case '\n': - input++; - info.token = SL_PP_NEWLINE; - break; + case '#': + out->token = SL_PP_HASH; + break; - case '#': - input++; - info.token = SL_PP_HASH; - break; + case ',': + out->token = SL_PP_COMMA; + break; - case ',': - input++; - info.token = SL_PP_COMMA; - break; + case ';': + out->token = SL_PP_SEMICOLON; + break; - case ';': - input++; - info.token = SL_PP_SEMICOLON; - break; + case '{': + out->token = SL_PP_LBRACE; + break; - case '{': - input++; - info.token = SL_PP_LBRACE; - break; + case '}': + out->token = SL_PP_RBRACE; + break; - case '}': - input++; - info.token = SL_PP_RBRACE; - break; + case '(': + out->token = SL_PP_LPAREN; + break; - case '(': - input++; - info.token = SL_PP_LPAREN; - break; + case ')': + out->token = SL_PP_RPAREN; + break; - case ')': - input++; - info.token = SL_PP_RPAREN; - break; + case '[': + out->token = SL_PP_LBRACKET; + break; - case '[': - input++; - info.token = SL_PP_LBRACKET; - break; + case ']': + out->token = SL_PP_RBRACKET; + break; - case ']': - input++; - info.token = SL_PP_RBRACKET; - break; + case '.': + { + int c2 = _pure_getc(context); - case '.': - if (input[1] >= '0' && input[1] <= '9') { - if (_tokenise_number(context, &input, &info)) { - free(out); + if (c2 == PURE_ERROR) { + return -1; + } + if (c2 >= '0' && c2 <= '9') { + _pure_ungetc(context, c2); + _pure_ungetc(context, c); + if (_tokenise_number(context, out)) { return -1; } } else { - input++; - info.token = SL_PP_DOT; + _pure_ungetc(context, c2); + out->token = SL_PP_DOT; } - break; + } + break; - case '+': - input++; - if (*input == '+') { - input++; - info.token = SL_PP_INCREMENT; - } else if (*input == '=') { - input++; - info.token = SL_PP_ADDASSIGN; - } else { - info.token = SL_PP_PLUS; - } - break; + case '+': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '+') { + out->token = SL_PP_INCREMENT; + } else if (c == '=') { + out->token = SL_PP_ADDASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_PLUS; + } + break; - case '-': - input++; - if (*input == '-') { - input++; - info.token = SL_PP_DECREMENT; - } else if (*input == '=') { - input++; - info.token = SL_PP_SUBASSIGN; - } else { - info.token = SL_PP_MINUS; - } - break; + case '-': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '-') { + out->token = SL_PP_DECREMENT; + } else if (c == '=') { + out->token = SL_PP_SUBASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_MINUS; + } + break; - case '~': - input++; - info.token = SL_PP_BITNOT; - break; + case '~': + out->token = SL_PP_BITNOT; + break; - case '!': - input++; - if (*input == '=') { - input++; - info.token = SL_PP_NOTEQUAL; - } else { - info.token = SL_PP_NOT; - } - break; + case '!': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '=') { + out->token = SL_PP_NOTEQUAL; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_NOT; + } + break; - case '*': - input++; - if (*input == '=') { - input++; - info.token = SL_PP_MULASSIGN; - } else { - info.token = SL_PP_STAR; - } - break; + case '*': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '=') { + out->token = SL_PP_MULASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_STAR; + } + break; - case '/': - input++; - if (*input == '=') { - input++; - info.token = SL_PP_DIVASSIGN; - } else { - info.token = SL_PP_SLASH; - } - break; + case '/': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '=') { + out->token = SL_PP_DIVASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_SLASH; + } + break; - case '%': - input++; - if (*input == '=') { - input++; - info.token = SL_PP_MODASSIGN; - } else { - info.token = SL_PP_MODULO; - } - break; + case '%': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '=') { + out->token = SL_PP_MODASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_MODULO; + } + break; - case '<': - input++; - if (*input == '<') { - input++; - if (*input == '=') { - input++; - info.token = SL_PP_LSHIFTASSIGN; - } else { - info.token = SL_PP_LSHIFT; - } - } else if (*input == '=') { - input++; - info.token = SL_PP_LESSEQUAL; - } else { - info.token = SL_PP_LESS; + case '<': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '<') { + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; } - break; - - case '>': - input++; - if (*input == '>') { - input++; - if (*input == '=') { - input++; - info.token = SL_PP_RSHIFTASSIGN; - } else { - info.token = SL_PP_RSHIFT; - } - } else if (*input == '=') { - input++; - info.token = SL_PP_GREATEREQUAL; + if (c == '=') { + out->token = SL_PP_LSHIFTASSIGN; } else { - info.token = SL_PP_GREATER; + _pure_ungetc(context, c); + out->token = SL_PP_LSHIFT; } - break; + } else if (c == '=') { + out->token = SL_PP_LESSEQUAL; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_LESS; + } + break; - case '=': - input++; - if (*input == '=') { - input++; - info.token = SL_PP_EQUAL; - } else { - info.token = SL_PP_ASSIGN; + case '>': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '>') { + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; } - break; - - case '&': - input++; - if (*input == '&') { - input++; - info.token = SL_PP_AND; - } else if (*input == '=') { - input++; - info.token = SL_PP_BITANDASSIGN; + if (c == '=') { + out->token = SL_PP_RSHIFTASSIGN; } else { - info.token = SL_PP_BITAND; + _pure_ungetc(context, c); + out->token = SL_PP_RSHIFT; } - break; + } else if (c == '=') { + out->token = SL_PP_GREATEREQUAL; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_GREATER; + } + break; - case '^': - input++; - if (*input == '^') { - input++; - info.token = SL_PP_XOR; - } else if (*input == '=') { - input++; - info.token = SL_PP_BITXORASSIGN; - } else { - info.token = SL_PP_BITXOR; - } - break; + case '=': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '=') { + out->token = SL_PP_EQUAL; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_ASSIGN; + } + break; - case '|': - input++; - if (*input == '|') { - input++; - info.token = SL_PP_OR; - } else if (*input == '=') { - input++; - info.token = SL_PP_BITORASSIGN; - } else { - info.token = SL_PP_BITOR; - } - break; + case '&': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '&') { + out->token = SL_PP_AND; + } else if (c == '=') { + out->token = SL_PP_BITANDASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_BITAND; + } + break; - case '?': - input++; - info.token = SL_PP_QUESTION; - break; + case '^': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '^') { + out->token = SL_PP_XOR; + } else if (c == '=') { + out->token = SL_PP_BITXORASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_BITXOR; + } + break; - case ':': - input++; - info.token = SL_PP_COLON; - break; + case '|': + c = _pure_getc(context); + if (c == PURE_ERROR) { + return -1; + } + if (c == '|') { + out->token = SL_PP_OR; + } else if (c == '=') { + out->token = SL_PP_BITORASSIGN; + } else { + _pure_ungetc(context, c); + out->token = SL_PP_BITOR; + } + break; - case '\0': - info.token = SL_PP_EOF; - break; + case '?': + out->token = SL_PP_QUESTION; + break; - default: - if ((*input >= 'a' && *input <= 'z') || - (*input >= 'A' && *input <= 'Z') || - (*input == '_')) { - if (_tokenise_identifier(context, &input, &info)) { - free(out); - return -1; - } - } else if (*input >= '0' && *input <= '9') { - if (_tokenise_number(context, &input, &info)) { - free(out); - return -1; - } - } else { - info.data.other = *input++; - info.token = SL_PP_OTHER; + case ':': + out->token = SL_PP_COLON; + break; + + case '\0': + out->token = SL_PP_EOF; + break; + + case PURE_ERROR: + return -1; + + default: + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') { + _pure_ungetc(context, c); + if (_tokenise_identifier(context, out)) { + return -1; } + } else if (c >= '0' && c <= '9') { + _pure_ungetc(context, c); + if (_tokenise_number(context, out)) { + return -1; + } + } else { + out->data.other = c; + out->token = SL_PP_OTHER; + } + } + + return 0; +} + + +int +sl_pp_tokenise(struct sl_pp_context *context, + const char *input, + const struct sl_pp_purify_options *options, + struct sl_pp_token_info **output) +{ + struct sl_pp_token_info *out = NULL; + unsigned int out_len = 0; + unsigned int out_max = 0; + + sl_pp_purify_state_init(&context->pure, input, options); + + for (;;) { + struct sl_pp_token_info info; + + if (sl_pp_token_get(context, &info)) { + free(out); + return -1; } if (out_len >= out_max) { diff --git a/src/glsl/pp/sl_pp_token.h b/src/glsl/pp/sl_pp_token.h index 29c7571711e..7a8fa2f1b9d 100644 --- a/src/glsl/pp/sl_pp_token.h +++ b/src/glsl/pp/sl_pp_token.h @@ -123,6 +123,7 @@ struct sl_pp_token_info { int sl_pp_tokenise(struct sl_pp_context *context, const char *input, + const struct sl_pp_purify_options *options, struct sl_pp_token_info **output); #endif /* SL_PP_TOKEN_H */ -- cgit v1.2.3 From 91e164b3d0b1d36bfdf369266ae7e1ab396f1ba2 Mon Sep 17 00:00:00 2001 From: Michal Krol Date: Thu, 10 Dec 2009 12:38:22 +0100 Subject: glsl/pp: Add sl_pp_context_add_extension(). This way third parties are able to add supported extension strings. --- src/glsl/pp/sl_pp_context.h | 10 +++++++++ src/glsl/pp/sl_pp_dict.c | 2 -- src/glsl/pp/sl_pp_dict.h | 2 -- src/glsl/pp/sl_pp_extension.c | 49 ++++++++++++++++++++++++++++++++----------- src/glsl/pp/sl_pp_macro.c | 12 +++++++++++ src/glsl/pp/sl_pp_public.h | 5 +++++ 6 files changed, 64 insertions(+), 16 deletions(-) (limited to 'src/glsl/pp/sl_pp_public.h') diff --git a/src/glsl/pp/sl_pp_context.h b/src/glsl/pp/sl_pp_context.h index 569a2d735b9..5e3ae72fdfa 100644 --- a/src/glsl/pp/sl_pp_context.h +++ b/src/glsl/pp/sl_pp_context.h @@ -37,6 +37,13 @@ #define SL_PP_MAX_ERROR_MSG 1024 +#define SL_PP_MAX_EXTENSIONS 16 + +struct sl_pp_extension { + int name; /*< VENDOR_extension_name */ + int name_string; /*< GL_VENDOR_extension_name */ +}; + struct sl_pp_context { char *cstr_pool; unsigned int cstr_pool_max; @@ -46,6 +53,9 @@ struct sl_pp_context { struct sl_pp_macro *macro; struct sl_pp_macro **macro_tail; + struct sl_pp_extension extensions[SL_PP_MAX_EXTENSIONS]; + unsigned int num_extensions; + unsigned int if_stack[SL_PP_MAX_IF_NESTING]; unsigned int if_ptr; unsigned int if_value; diff --git a/src/glsl/pp/sl_pp_dict.c b/src/glsl/pp/sl_pp_dict.c index 2dd77a69e90..062139e6ac0 100644 --- a/src/glsl/pp/sl_pp_dict.c +++ b/src/glsl/pp/sl_pp_dict.c @@ -45,8 +45,6 @@ int sl_pp_dict_init(struct sl_pp_context *context) { ADD_NAME(context, all); - ADD_NAME_STR(context, _GL_ARB_draw_buffers, "GL_ARB_draw_buffers"); - ADD_NAME_STR(context, _GL_ARB_texture_rectangle, "GL_ARB_texture_rectangle"); ADD_NAME(context, require); ADD_NAME(context, enable); diff --git a/src/glsl/pp/sl_pp_dict.h b/src/glsl/pp/sl_pp_dict.h index 49f0e0bf9fa..875217bd309 100644 --- a/src/glsl/pp/sl_pp_dict.h +++ b/src/glsl/pp/sl_pp_dict.h @@ -33,8 +33,6 @@ struct sl_pp_context; struct sl_pp_dict { int all; - int _GL_ARB_draw_buffers; - int _GL_ARB_texture_rectangle; int require; int enable; diff --git a/src/glsl/pp/sl_pp_extension.c b/src/glsl/pp/sl_pp_extension.c index 4148fd9a5a3..67b24404d4d 100644 --- a/src/glsl/pp/sl_pp_extension.c +++ b/src/glsl/pp/sl_pp_extension.c @@ -28,8 +28,34 @@ #include #include #include "sl_pp_process.h" +#include "sl_pp_public.h" +int +sl_pp_context_add_extension(struct sl_pp_context *context, + const char *name, + const char *name_string) +{ + struct sl_pp_extension ext; + + if (context->num_extensions == SL_PP_MAX_EXTENSIONS) { + return -1; + } + + ext.name = sl_pp_context_add_unique_str(context, name); + if (ext.name == -1) { + return -1; + } + + ext.name_string = sl_pp_context_add_unique_str(context, name_string); + if (ext.name_string == -1) { + return -1; + } + + context->extensions[context->num_extensions++] = ext; + return 0; +} + int sl_pp_process_extension(struct sl_pp_context *context, const struct sl_pp_token_info *input, @@ -37,14 +63,7 @@ sl_pp_process_extension(struct sl_pp_context *context, unsigned int last, struct sl_pp_process_state *state) { - int extensions[] = { - context->dict.all, - context->dict._GL_ARB_draw_buffers, - context->dict._GL_ARB_texture_rectangle, - -1 - }; int extension_name = -1; - int *ext; int behavior = -1; struct sl_pp_token_info out; @@ -59,11 +78,17 @@ sl_pp_process_extension(struct sl_pp_context *context, } /* Make sure the extension is supported. */ - out.data.extension = -1; - for (ext = extensions; *ext != -1; ext++) { - if (extension_name == *ext) { - out.data.extension = extension_name; - break; + if (extension_name == context->dict.all) { + out.data.extension = extension_name; + } else { + unsigned int i; + + out.data.extension = -1; + for (i = 0; i < context->num_extensions; i++) { + if (extension_name == context->extensions[i].name_string) { + out.data.extension = extension_name; + break; + } } } diff --git a/src/glsl/pp/sl_pp_macro.c b/src/glsl/pp/sl_pp_macro.c index 29f1229dd7d..05466c9a7c3 100644 --- a/src/glsl/pp/sl_pp_macro.c +++ b/src/glsl/pp/sl_pp_macro.c @@ -163,6 +163,18 @@ sl_pp_macro_expand(struct sl_pp_context *context, return 0; } + /* Replace extension names with 1. + */ + for (j = 0; j < context->num_extensions; j++) { + if (macro_name == context->extensions[j].name) { + if (!mute && _out_number(context, state, 1)) { + return -1; + } + (*pi)++; + return 0; + } + } + /* TODO: For FEATURE_es2_glsl, expand to 1 the following symbols. * GL_ES * GL_FRAGMENT_PRECISION_HIGH diff --git a/src/glsl/pp/sl_pp_public.h b/src/glsl/pp/sl_pp_public.h index 8317c7e378a..20f208975e4 100644 --- a/src/glsl/pp/sl_pp_public.h +++ b/src/glsl/pp/sl_pp_public.h @@ -45,6 +45,11 @@ sl_pp_context_destroy(struct sl_pp_context *context); const char * sl_pp_context_error_message(const struct sl_pp_context *context); +int +sl_pp_context_add_extension(struct sl_pp_context *context, + const char *name, + const char *name_string); + int sl_pp_context_add_unique_str(struct sl_pp_context *context, const char *str); -- cgit v1.2.3 From 22200bcafcc77ecdca0127ac72d68e75e2ad7aee Mon Sep 17 00:00:00 2001 From: Michal Krol Date: Thu, 10 Dec 2009 12:58:21 +0100 Subject: glsl/pp: Add support for user-defined macros. --- src/glsl/pp/sl_pp_context.c | 25 +++++++++++++++++++++++++ src/glsl/pp/sl_pp_context.h | 10 ++++++++++ src/glsl/pp/sl_pp_macro.c | 17 +++++++++++++++++ src/glsl/pp/sl_pp_public.h | 5 +++++ 4 files changed, 57 insertions(+) (limited to 'src/glsl/pp/sl_pp_public.h') diff --git a/src/glsl/pp/sl_pp_context.c b/src/glsl/pp/sl_pp_context.c index 134588d9066..afc1b84d16a 100644 --- a/src/glsl/pp/sl_pp_context.c +++ b/src/glsl/pp/sl_pp_context.c @@ -79,6 +79,31 @@ sl_pp_context_error_message(const struct sl_pp_context *context) return context->error_msg; } +int +sl_pp_context_add_predefined(struct sl_pp_context *context, + const char *name, + const char *value) +{ + struct sl_pp_predefined pre; + + if (context->num_predefined == SL_PP_MAX_PREDEFINED) { + return -1; + } + + pre.name = sl_pp_context_add_unique_str(context, name); + if (pre.name == -1) { + return -1; + } + + pre.value = sl_pp_context_add_unique_str(context, value); + if (pre.value == -1) { + return -1; + } + + context->predefined[context->num_predefined++] = pre; + return 0; +} + int sl_pp_context_add_unique_str(struct sl_pp_context *context, const char *str) diff --git a/src/glsl/pp/sl_pp_context.h b/src/glsl/pp/sl_pp_context.h index 5e3ae72fdfa..d95d29e275c 100644 --- a/src/glsl/pp/sl_pp_context.h +++ b/src/glsl/pp/sl_pp_context.h @@ -39,11 +39,18 @@ #define SL_PP_MAX_EXTENSIONS 16 +#define SL_PP_MAX_PREDEFINED 16 + struct sl_pp_extension { int name; /*< VENDOR_extension_name */ int name_string; /*< GL_VENDOR_extension_name */ }; +struct sl_pp_predefined { + int name; + int value; +}; + struct sl_pp_context { char *cstr_pool; unsigned int cstr_pool_max; @@ -56,6 +63,9 @@ struct sl_pp_context { struct sl_pp_extension extensions[SL_PP_MAX_EXTENSIONS]; unsigned int num_extensions; + struct sl_pp_predefined predefined[SL_PP_MAX_PREDEFINED]; + unsigned int num_predefined; + unsigned int if_stack[SL_PP_MAX_IF_NESTING]; unsigned int if_ptr; unsigned int if_value; diff --git a/src/glsl/pp/sl_pp_macro.c b/src/glsl/pp/sl_pp_macro.c index 05466c9a7c3..08b44c7cbe4 100644 --- a/src/glsl/pp/sl_pp_macro.c +++ b/src/glsl/pp/sl_pp_macro.c @@ -163,6 +163,23 @@ sl_pp_macro_expand(struct sl_pp_context *context, return 0; } + for (j = 0; j < context->num_predefined; j++) { + if (macro_name == context->predefined[j].name) { + if (!mute) { + struct sl_pp_token_info ti; + + ti.token = SL_PP_UINT; + ti.data._uint = context->predefined[j].value; + if (sl_pp_process_out(state, &ti)) { + strcpy(context->error_msg, "out of memory"); + return -1; + } + } + (*pi)++; + return 0; + } + } + /* Replace extension names with 1. */ for (j = 0; j < context->num_extensions; j++) { diff --git a/src/glsl/pp/sl_pp_public.h b/src/glsl/pp/sl_pp_public.h index 20f208975e4..076903649cd 100644 --- a/src/glsl/pp/sl_pp_public.h +++ b/src/glsl/pp/sl_pp_public.h @@ -50,6 +50,11 @@ sl_pp_context_add_extension(struct sl_pp_context *context, const char *name, const char *name_string); +int +sl_pp_context_add_predefined(struct sl_pp_context *context, + const char *name, + const char *value); + int sl_pp_context_add_unique_str(struct sl_pp_context *context, const char *str); -- cgit v1.2.3