1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
/********************************************
fcall.c
copyright 1991, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/*$Log: fcall.c,v $
* Revision 1.7 1995/08/27 15:46:47 mike
* change some errmsgs to compile_errors
*
* Revision 1.6 1995/06/09 22:58:24 mike
* cast to shutup solaris cc on comparison of short to ushort
*
* Revision 1.5 1995/06/06 00:18:26 mike
* change mawk_exit(1) to mawk_exit(2)
*
* Revision 1.4 1995/04/21 14:20:14 mike
* move_level variable to fix bug in arglist patching of moved code.
*
* Revision 1.3 1995/02/19 22:15:37 mike
* Always set the call_offset field in a CA_REC (for obscure
* reasons in fcall.c (see comments) there.)
*
* Revision 1.2 1993/07/17 13:22:52 mike
* indent and general code cleanup
*
* Revision 1.1.1.1 1993/07/03 18:58:11 mike
* move source to cvs
*
* Revision 5.4 1993/01/09 19:03:44 mike
* code_pop checks if the resolve_list needs relocation
*
* Revision 5.3 1993/01/07 02:50:33 mike
* relative vs absolute code
*
* Revision 5.2 1993/01/01 21:30:48 mike
* split new_STRING() into new_STRING and new_STRING0
*
* Revision 5.1 1991/12/05 07:55:54 brennan
* 1.1 pre-release
*
*/
#include "mawk.h"
#include "symtype.h"
#include "code.h"
/* This file has functions involved with type checking of
function calls
*/
static FCALL_REC *PROTO(first_pass, (FCALL_REC *)) ;
static CA_REC *PROTO(call_arg_check, (FBLOCK *, CA_REC *,
INST *, unsigned)) ;
static int PROTO(arg_cnt_ok, (FBLOCK *, CA_REC *, unsigned)) ;
static void PROTO(relocate_arglist, (CA_REC *, int, unsigned, int)) ;
static int check_progress ;
/* flag that indicates call_arg_check() was able to type
check some call arguments */
/* type checks a list of call arguments,
returns a list of arguments whose type is still unknown
*/
static CA_REC *
call_arg_check(callee, entry_list, start, line_no)
FBLOCK *callee ;
CA_REC *entry_list ;
INST *start ; /* to locate patch */
unsigned line_no ; /* for error messages */
{
register CA_REC *q ;
CA_REC *exit_list = (CA_REC *) 0 ;
check_progress = 0 ;
/* loop :
take q off entry_list
test it
if OK zfree(q) else put on exit_list */
while (q = entry_list)
{
entry_list = q->link ;
if (q->type == ST_NONE)
{
/* try to infer the type */
/* it might now be in symbol table */
if (q->sym_p->type == ST_VAR)
{
/* set type and patch */
q->type = CA_EXPR ;
start[q->call_offset + 1].ptr = (PTR) q->sym_p->stval.cp ;
}
else if (q->sym_p->type == ST_ARRAY)
{
q->type = CA_ARRAY ;
start[q->call_offset].op = A_PUSHA ;
start[q->call_offset + 1].ptr = (PTR) q->sym_p->stval.array ;
}
else /* try to infer from callee */
{
switch (callee->typev[q->arg_num])
{
case ST_LOCAL_VAR:
q->type = CA_EXPR ;
q->sym_p->type = ST_VAR ;
q->sym_p->stval.cp = ZMALLOC(CELL) ;
q->sym_p->stval.cp->type = C_NOINIT ;
start[q->call_offset + 1].ptr =
(PTR) q->sym_p->stval.cp ;
break ;
case ST_LOCAL_ARRAY:
q->type = CA_ARRAY ;
q->sym_p->type = ST_ARRAY ;
q->sym_p->stval.array = new_ARRAY() ;
start[q->call_offset].op = A_PUSHA ;
start[q->call_offset + 1].ptr =
(PTR) q->sym_p->stval.array ;
break ;
}
}
}
else if (q->type == ST_LOCAL_NONE)
{
/* try to infer the type */
if (*q->type_p == ST_LOCAL_VAR)
{
/* set type , don't need to patch */
q->type = CA_EXPR ;
}
else if (*q->type_p == ST_LOCAL_ARRAY)
{
q->type = CA_ARRAY ;
start[q->call_offset].op = LA_PUSHA ;
/* offset+1 op is OK */
}
else /* try to infer from callee */
{
switch (callee->typev[q->arg_num])
{
case ST_LOCAL_VAR:
q->type = CA_EXPR ;
*q->type_p = ST_LOCAL_VAR ;
/* do not need to patch */
break ;
case ST_LOCAL_ARRAY:
q->type = CA_ARRAY ;
*q->type_p = ST_LOCAL_ARRAY ;
start[q->call_offset].op = LA_PUSHA ;
break ;
}
}
}
/* if we still do not know the type put on the new list
else type check */
if (q->type == ST_NONE || q->type == ST_LOCAL_NONE)
{
q->link = exit_list ;
exit_list = q ;
}
else /* type known */
{
if (callee->typev[q->arg_num] == ST_LOCAL_NONE)
callee->typev[q->arg_num] = q->type ;
else if (q->type != callee->typev[q->arg_num])
compile_error("type error in arg(%d) in call to %s",
q->arg_num + 1, callee->name) ;
ZFREE(q) ;
check_progress = 1 ;
}
} /* while */
return exit_list ;
}
static int
arg_cnt_ok(fbp, q, line_no)
FBLOCK *fbp ;
CA_REC *q ;
unsigned line_no ;
{
if ((int)q->arg_num >= (int)fbp->nargs)
/* casts shutup stupid warning from solaris sun cc */
{
compile_error("too many arguments in call to %s", fbp->name) ;
return 0 ;
}
else return 1 ;
}
FCALL_REC *resolve_list ;
/* function calls whose arg types need checking
are stored on this list */
/* on first pass thru the resolve list
we check :
if forward referenced functions were really defined
if right number of arguments
and compute call_start which is now known
*/
static FCALL_REC *
first_pass(p)
register FCALL_REC *p ;
{
FCALL_REC dummy ;
register FCALL_REC *q = &dummy ; /* trails p */
q->link = p ;
while (p)
{
if (!p->callee->code)
{
/* callee never defined */
compile_error("function %s never defined", p->callee->name) ;
/* delete p from list */
q->link = p->link ;
/* don't worry about freeing memory, we'll exit soon */
}
/* note p->arg_list starts with last argument */
else if (!p->arg_list /* nothing to do */ ||
!p->arg_cnt_checked &&
!arg_cnt_ok(p->callee, p->arg_list, p->line_no))
{
q->link = p->link ; /* delete p */
/* the ! arg_list case is not an error so free memory */
ZFREE(p) ;
}
else
{
/* keep p and set call_start */
q = p ;
switch (p->call_scope)
{
case SCOPE_MAIN:
p->call_start = main_start ;
break ;
case SCOPE_BEGIN:
p->call_start = begin_start ;
break ;
case SCOPE_END:
p->call_start = end_start ;
break ;
case SCOPE_FUNCT:
p->call_start = p->call->code ;
break ;
}
}
p = q->link ;
}
return dummy.link ;
}
/* continuously walk the resolve_list making type deductions
until this list goes empty or no more progress can be made
(An example where no more progress can be made is at end of file
*/
void
resolve_fcalls()
{
register FCALL_REC *p, *old_list, *new_list ;
int progress ; /* a flag */
old_list = first_pass(resolve_list) ;
new_list = (FCALL_REC *) 0 ;
progress = 0 ;
while (1)
{
if (!old_list)
{
/* flop the lists */
old_list = new_list ;
if (!old_list /* nothing left */
|| !progress /* can't do any more */ )
return ;
new_list = (FCALL_REC *) 0 ; progress = 0 ;
}
p = old_list ;
old_list = p->link ;
if (p->arg_list = call_arg_check(p->callee, p->arg_list,
p->call_start, p->line_no))
{
/* still have work to do , put on new_list */
progress |= check_progress ;
p->link = new_list ; new_list = p ;
}
else
{
/* done with p */
progress = 1 ;
ZFREE(p) ;
}
}
}
/* the parser has just reduced a function call ;
the info needed to type check is passed in. If type checking
can not be done yet (most common reason -- function referenced
but not defined), a node is added to the resolve list.
*/
void
check_fcall(callee, call_scope, move_level, call, arg_list, line_no)
FBLOCK *callee ;
int call_scope ;
int move_level ;
FBLOCK *call ;
CA_REC *arg_list ;
unsigned line_no ;
{
FCALL_REC *p ;
if (!callee->code)
{
/* forward reference to a function to be defined later */
p = ZMALLOC(FCALL_REC) ;
p->callee = callee ;
p->call_scope = call_scope ;
p->move_level = move_level ;
p->call = call ;
p->arg_list = arg_list ;
p->arg_cnt_checked = 0 ;
p->line_no = line_no ;
/* add to resolve list */
p->link = resolve_list ; resolve_list = p ;
}
else if (arg_list && arg_cnt_ok(callee, arg_list, line_no))
{
/* usually arg_list disappears here and all is well
otherwise add to resolve list */
if (arg_list = call_arg_check(callee, arg_list,
code_base, line_no))
{
p = ZMALLOC(FCALL_REC) ;
p->callee = callee ;
p->call_scope = call_scope ;
p->move_level = move_level ;
p->call = call ;
p->arg_list = arg_list ;
p->arg_cnt_checked = 1 ;
p->line_no = line_no ;
/* add to resolve list */
p->link = resolve_list ; resolve_list = p ;
}
}
}
/* code_pop() has just moved some code. If this code contains
a function call, it might need to be relocated on the
resolve list too. This function does it.
*/
void
relocate_resolve_list(scope, move_level, fbp, orig_offset, len, delta)
int scope ;
int move_level ;
FBLOCK *fbp ;
int orig_offset ;
unsigned len ;
int delta ; /* relocation distance */
{
FCALL_REC *p = resolve_list ;
while (p)
{
if (scope == p->call_scope && move_level == p->move_level &&
(scope == SCOPE_FUNCT ? fbp == p->call : 1))
{
relocate_arglist(p->arg_list, orig_offset,
len, delta) ;
}
p = p->link ;
}
}
static void
relocate_arglist(arg_list, offset, len, delta)
CA_REC *arg_list ;
int offset ;
unsigned len ;
int delta ;
{
register CA_REC *p ;
if (!arg_list) return ;
p = arg_list ;
/* all nodes must be relocated or none, so test the
first one */
/* Note: call_offset is always set even for args that don't need to
be patched so that this check works. */
if ( p->call_offset < offset || p->call_offset >= offset + len )
return ;
/* relocate the whole list */
do
{
p->call_offset += delta ;
p = p->link ;
}
while (p);
}
/* example where typing cannot progress
{ f(z) }
function f(x) { print NR }
# this is legal, does something useful, but absurdly written
# We have to design so this works
*/
|