LLVM API Documentation
00001 /*- 00002 * This code is derived from OpenBSD's libc/regex, original license follows: 00003 * 00004 * Copyright (c) 1992, 1993, 1994 Henry Spencer. 00005 * Copyright (c) 1992, 1993, 1994 00006 * The Regents of the University of California. All rights reserved. 00007 * 00008 * This code is derived from software contributed to Berkeley by 00009 * Henry Spencer. 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright 00017 * notice, this list of conditions and the following disclaimer in the 00018 * documentation and/or other materials provided with the distribution. 00019 * 3. Neither the name of the University nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00029 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00032 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00033 * SUCH DAMAGE. 00034 * 00035 * @(#)regexec.c 8.3 (Berkeley) 3/20/94 00036 */ 00037 00038 /* 00039 * the outer shell of llvm_regexec() 00040 * 00041 * This file includes engine.inc *twice*, after muchos fiddling with the 00042 * macros that code uses. This lets the same code operate on two different 00043 * representations for state sets. 00044 */ 00045 #include <sys/types.h> 00046 #include <stdio.h> 00047 #include <stdlib.h> 00048 #include <string.h> 00049 #include <limits.h> 00050 #include <ctype.h> 00051 #include "regex_impl.h" 00052 00053 #include "regutils.h" 00054 #include "regex2.h" 00055 00056 /* macros for manipulating states, small version */ 00057 /* FIXME: 'states' is assumed as 'long' on small version. */ 00058 #define states1 long /* for later use in llvm_regexec() decision */ 00059 #define states states1 00060 #define CLEAR(v) ((v) = 0) 00061 #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n))) 00062 #define SET1(v, n) ((v) |= (unsigned long)1 << (n)) 00063 #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0) 00064 #define ASSIGN(d, s) ((d) = (s)) 00065 #define EQ(a, b) ((a) == (b)) 00066 #define STATEVARS long dummy /* dummy version */ 00067 #define STATESETUP(m, n) /* nothing */ 00068 #define STATETEARDOWN(m) /* nothing */ 00069 #define SETUP(v) ((v) = 0) 00070 #define onestate long 00071 #define INIT(o, n) ((o) = (unsigned long)1 << (n)) 00072 #define INC(o) ((o) = (unsigned long)(o) << 1) 00073 #define ISSTATEIN(v, o) (((v) & (o)) != 0) 00074 /* some abbreviations; note that some of these know variable names! */ 00075 /* do "if I'm here, I can also be there" etc without branches */ 00076 #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n)) 00077 #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n)) 00078 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0) 00079 /* function names */ 00080 #define SNAMES /* engine.inc looks after details */ 00081 00082 #include "regengine.inc" 00083 00084 /* now undo things */ 00085 #undef states 00086 #undef CLEAR 00087 #undef SET0 00088 #undef SET1 00089 #undef ISSET 00090 #undef ASSIGN 00091 #undef EQ 00092 #undef STATEVARS 00093 #undef STATESETUP 00094 #undef STATETEARDOWN 00095 #undef SETUP 00096 #undef onestate 00097 #undef INIT 00098 #undef INC 00099 #undef ISSTATEIN 00100 #undef FWD 00101 #undef BACK 00102 #undef ISSETBACK 00103 #undef SNAMES 00104 00105 /* macros for manipulating states, large version */ 00106 #define states char * 00107 #define CLEAR(v) memset(v, 0, m->g->nstates) 00108 #define SET0(v, n) ((v)[n] = 0) 00109 #define SET1(v, n) ((v)[n] = 1) 00110 #define ISSET(v, n) ((v)[n]) 00111 #define ASSIGN(d, s) memmove(d, s, m->g->nstates) 00112 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0) 00113 #define STATEVARS long vn; char *space 00114 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \ 00115 if ((m)->space == NULL) return(REG_ESPACE); \ 00116 (m)->vn = 0; } 00117 #define STATETEARDOWN(m) { free((m)->space); } 00118 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates]) 00119 #define onestate long 00120 #define INIT(o, n) ((o) = (n)) 00121 #define INC(o) ((o)++) 00122 #define ISSTATEIN(v, o) ((v)[o]) 00123 /* some abbreviations; note that some of these know variable names! */ 00124 /* do "if I'm here, I can also be there" etc without branches */ 00125 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here]) 00126 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here]) 00127 #define ISSETBACK(v, n) ((v)[here - (n)]) 00128 /* function names */ 00129 #define LNAMES /* flag */ 00130 00131 #include "regengine.inc" 00132 00133 /* 00134 - llvm_regexec - interface for matching 00135 * 00136 * We put this here so we can exploit knowledge of the state representation 00137 * when choosing which matcher to call. Also, by this point the matchers 00138 * have been prototyped. 00139 */ 00140 int /* 0 success, REG_NOMATCH failure */ 00141 llvm_regexec(const llvm_regex_t *preg, const char *string, size_t nmatch, 00142 llvm_regmatch_t pmatch[], int eflags) 00143 { 00144 struct re_guts *g = preg->re_g; 00145 #ifdef REDEBUG 00146 # define GOODFLAGS(f) (f) 00147 #else 00148 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND)) 00149 #endif 00150 00151 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2) 00152 return(REG_BADPAT); 00153 assert(!(g->iflags®EX_BAD)); 00154 if (g->iflags®EX_BAD) /* backstop for no-debug case */ 00155 return(REG_BADPAT); 00156 eflags = GOODFLAGS(eflags); 00157 00158 if (g->nstates <= (long)(CHAR_BIT*sizeof(states1)) && !(eflags®_LARGE)) 00159 return(smatcher(g, string, nmatch, pmatch, eflags)); 00160 else 00161 return(lmatcher(g, string, nmatch, pmatch, eflags)); 00162 }