Author: kjs Date: Sat Dec 1 06:55:00 2007 New Revision: 23312 Added: trunk/compilers/pirc/new/pirsymbol.c (contents, props changed) trunk/compilers/pirc/new/pirsymbol.h (contents, props changed) Modified: trunk/compilers/pirc/new/pir.y trunk/compilers/pirc/new/pircompunit.c trunk/compilers/pirc/new/pircompunit.h trunk/compilers/pirc/new/pirparser.c trunk/compilers/pirc/new/pirparser.h
Log: [pirc|new] more backend stuff. add files for symbol handling. Modified: trunk/compilers/pirc/new/pir.y ============================================================================== --- trunk/compilers/pirc/new/pir.y (original) +++ trunk/compilers/pirc/new/pir.y Sat Dec 1 06:55:00 2007 @@ -26,6 +26,7 @@ #include "pirparser.h" #include "pircompiler.h" #include "pircompunit.h" +#include "pirsymbol.h" /* prevent inclusion of <unistd.h> on windows */ #define YY_NO_UNISTD_H @@ -219,8 +220,7 @@ string_object invokable opt_ret_cont - -%type <targ> reg + reg pasm_reg target result_target @@ -252,8 +252,10 @@ %type <expr> expression1 expression key + keylist pasm_expression other_op_args + other_op_arg keys %type <ival> has_unique_reg @@ -281,7 +283,7 @@ %type <fixme> assignment_tail assignment_expression - keylist + first_op_arg long_invocation_statement short_return_statement @@ -371,7 +373,7 @@ /* Sub definition */ -sub_definition: ".sub" sub_id { new_sub(lexer, $2); } +sub_definition: ".sub" sub_id { new_subr(lexer, $2); } sub_flags "\n" { set_sub_flag(lexer, $4); } parameters instructions @@ -522,7 +524,7 @@ /* later arguments can be either an expression or a keylist. */ other_op_args: /* empty */ { } - | other_op_args ',' other_op_arg { add_operand(lexer, $1); } + | other_op_args ',' other_op_arg { add_operand(lexer, $3); } ; other_op_arg: pasm_expression @@ -545,11 +547,9 @@ conditional_statement: if_type condition then identifier "\n" - { /**/ + { /* it was "unless", so "invert" the opcode */ if ($1 > 0) { - /* it was "unless", so we need to - "invert" the opcode - */ + invert_instr(lexer); } add_operand(lexer, expr_from_ident($4)); @@ -693,7 +693,7 @@ | string_object ; -invokable: identifier { $$ = target_from_ident($1); } +invokable: identifier { $$ = target_from_ident($1); find_target(lexer, $1);} | TK_SYM_PREG { $$ = reg(PMC_TYPE, $1, !IS_PASM_REG); } | TK_PASM_PREG { $$ = reg(PMC_TYPE, $1, IS_PASM_REG); } ; Modified: trunk/compilers/pirc/new/pircompunit.c ============================================================================== --- trunk/compilers/pirc/new/pircompunit.c (original) +++ trunk/compilers/pirc/new/pircompunit.c Sat Dec 1 06:55:00 2007 @@ -18,6 +18,7 @@ #include <stdio.h> #include "pircompunit.h" #include "pircompiler.h" +#include "pirsymbol.h" #include <assert.h> #include <stdio.h> #include <stdlib.h> @@ -27,6 +28,26 @@ #define out stderr + +void +parse_error(struct lexer_state *lexer, char *message, ...) { + va_list arg_ptr; + va_start(arg_ptr, message); + fprintf(stderr, "Parse error (line %d:%d): ", lexer->line_nr, lexer->line_pos); + vfprintf(stderr, message, arg_ptr); + va_end(arg_ptr); + lexer->parse_errors++; +} + +target * +find_target(struct lexer_state *lexer, char * const name) { + target *t = find_symbol(lexer, name); + if (t == NULL) { /* not declared */ + parse_error(lexer, "symbol '%s' was not declared\n", name); + } + return t; +} + /* Set the lexically enclosing sub for the current sub. @@ -69,7 +90,7 @@ */ void -new_sub(struct lexer_state *lexer, char *subname) { +new_subr(struct lexer_state *lexer, char *subname) { subroutine *newsub = (subroutine *)malloc(sizeof (subroutine)); assert(newsub != NULL); @@ -108,7 +129,9 @@ } - +/* Create a new target node. The node's next pointer is initialized to + itself. + */ target * new_target(pir_type type, char *name) { target *t = (target *)calloc(1, sizeof (target)); @@ -207,10 +230,6 @@ } -void -declare_local(struct lexer_state *lexer, pir_type type, target *list) { - -} @@ -335,6 +354,7 @@ switch (type) { case RHS_BINOP: { + /* x = a binop b -> binop x, a, b */ expression *left_op, *right_op; /* get instruction */ lexer->subs->statements->instr.ins = new_instruction(va_arg(arg_ptr, char *)); @@ -346,13 +366,14 @@ break; } case RHS_SIMPLE: { - /* get only the rhs expression */ + /* x = y -> set x, y */ expression *operand = va_arg(arg_ptr, expression *); + lexer->subs->statements->instr.ins = new_instruction("set"); add_operand(lexer, operand); break; } case RHS_UNOP: { - /* get instruction */ + /* x = unop y -> unop x, y */ expression *operand; lexer->subs->statements->instr.ins = new_instruction(va_arg(arg_ptr, char *)); /* get 1 operand */ @@ -451,7 +472,7 @@ va_list arg_ptr; invocation *inv = new_invocation(); - SET_FLAG(inv->type, type); + inv->type = type; va_start(arg_ptr, type); switch (type) { @@ -494,7 +515,6 @@ target * target_from_ident(char *id) { target *var = new_target(UNKNOWN_TYPE, id); - return var; } @@ -502,7 +522,7 @@ void set_lex_flag(target *t, char *lexname) { - + /* TODO */ } void @@ -523,6 +543,7 @@ void add_operand(struct lexer_state *lexer, expression *operand) { + assert(lexer->subs->statements->instr.ins); if (lexer->subs->statements->instr.ins->operands == NULL) { lexer->subs->statements->instr.ins->operands = operand; } @@ -614,8 +635,9 @@ print_expr(iter->value); iter = iter->next; } - while (iter != args); + while (iter != args->next); } + printf("\n"); } void @@ -627,6 +649,9 @@ /* set iterator to first item */ iter = expr->next; + /* do something with the iterator while it doesn't point + * to the first item. + */ do { print_expr(iter); iter = iter->next; @@ -655,9 +680,10 @@ switch (inv->type) { case CALL_PCC: - printf(" get_results"); + printf(" set_args"); printf("\n"); - printf(" invoke"); + printf(" set_p_pc P%d, %s\n", 0, inv->sub->name); + printf(" invokecc"); break; case CALL_RET: printf(" set_returns"); @@ -691,10 +717,10 @@ fprintf(stderr, "Unknown invocation type (%d)\n", inv->type); exit(EXIT_FAILURE); } - - // if (inv->results) print_target(inv->results); - // if (inv->arguments) print_args(inv->arguments); - +/* + if (inv->results) print_target(inv->results); + if (inv->arguments) print_args(inv->arguments); +*/ puts(""); } Modified: trunk/compilers/pirc/new/pircompunit.h ============================================================================== --- trunk/compilers/pirc/new/pircompunit.h (original) +++ trunk/compilers/pirc/new/pircompunit.h Sat Dec 1 06:55:00 2007 @@ -158,7 +158,7 @@ * return continuation. */ typedef struct target { - pir_type type; + pir_type type; char *name; /* if this is a symbolic variable */ int regno; /* if this is a symbolic register */ int color; /* for register allocation */ @@ -170,7 +170,7 @@ } target; -/* todo: possibly unify expression and argument. */ + /* function arguments or return values, but a return value is just @@ -195,13 +195,11 @@ */ typedef struct invocation { invoke_type type; - target *object; - target *sub; - target *retcc; - - target *results; - argument *arguments; - + target *object; + target *sub; + target *retcc; + target *results; + argument *arguments; } invocation; @@ -289,7 +287,7 @@ void set_sub_outer(struct lexer_state *lexer, char *outersub); void set_sub_vtable(struct lexer_state *lexer, char *vtablename); -void new_sub(struct lexer_state *lexer, char *subname); +void new_subr(struct lexer_state *lexer, char *subname); void set_param_named(target *t, char *alias); void add_instr(struct lexer_state *lexer, char *label, instruction *instr); @@ -340,7 +338,7 @@ void set_invocation_args(invocation *inv, argument *args); void set_invocation_results(invocation *inv, target *results); -void declare_local(struct lexer_state *lexer, pir_type type, target *list); + void set_lex_flag(target *t, char *lexname); void invert_instr(struct lexer_state *lexer); void assign(struct lexer_state *lexer, rhs_type type, ...); Modified: trunk/compilers/pirc/new/pirparser.c ============================================================================== --- trunk/compilers/pirc/new/pirparser.c (original) +++ trunk/compilers/pirc/new/pirparser.c Sat Dec 1 06:55:00 2007 @@ -297,6 +297,7 @@ #include "pirparser.h" #include "pircompiler.h" #include "pircompunit.h" +#include "pirsymbol.h" /* prevent inclusion of <unistd.h> on windows */ #define YY_NO_UNISTD_H @@ -371,7 +372,7 @@ #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef union YYSTYPE -#line 83 "pir.y" +#line 84 "pir.y" { double dval; int ival; @@ -386,7 +387,7 @@ void *fixme; } /* Line 187 of yacc.c. */ -#line 390 "pirparser.c" +#line 391 "pirparser.c" YYSTYPE; # define yystype YYSTYPE /* obsolescent; will be withdrawn */ # define YYSTYPE_IS_DECLARED 1 @@ -399,7 +400,7 @@ /* Line 216 of yacc.c. */ -#line 403 "pirparser.c" +#line 404 "pirparser.c" #ifdef short # undef short @@ -789,17 +790,17 @@ /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint16 yyrline[] = { - 0, 322, 322, 327, 328, 331, 332, 335, 336, 337, - 338, 339, 340, 341, 344, 347, 352, 355, 361, 364, - 365, 368, 369, 374, 375, 374, 381, 382, 385, 386, - 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, - 399, 402, 403, 406, 407, 408, 409, 412, 413, 416, - 421, 422, 428, 429, 429, 434, 435, 436, 439, 440, - 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, - 451, 454, 462, 465, 470, 475, 478, 495, 496, 497, - 500, 501, 502, 503, 504, 508, 508, 512, 513, 516, - 520, 524, 525, 528, 529, 532, 535, 536, 539, 542, - 543, 547, 557, 564, 565, 569, 570, 573, 574, 578, + 0, 324, 324, 329, 330, 333, 334, 337, 338, 339, + 340, 341, 342, 343, 346, 349, 354, 357, 363, 366, + 367, 370, 371, 376, 377, 376, 383, 384, 387, 388, + 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, + 401, 404, 405, 408, 409, 410, 411, 414, 415, 418, + 423, 424, 430, 431, 431, 436, 437, 438, 441, 442, + 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, + 453, 456, 464, 467, 472, 477, 480, 497, 498, 499, + 502, 503, 504, 505, 506, 510, 510, 514, 515, 518, + 522, 526, 527, 530, 531, 534, 537, 538, 541, 544, + 545, 549, 557, 564, 565, 569, 570, 573, 574, 578, 581, 588, 593, 596, 597, 600, 603, 604, 609, 613, 614, 617, 629, 630, 633, 634, 637, 640, 641, 642, 646, 647, 652, 653, 656, 657, 660, 661, 664, 666, @@ -2090,154 +2091,154 @@ switch (yyn) { case 14: -#line 344 "pir.y" +#line 346 "pir.y" { set_pragma(PRAGMA_N_OPERATORS, (yyvsp[(3) - (3)].ival)); ;} break; case 15: -#line 347 "pir.y" +#line 349 "pir.y" { load_library(lexer, (yyvsp[(2) - (2)].sval)); ;} break; case 16: -#line 352 "pir.y" +#line 354 "pir.y" { set_hll((yyvsp[(2) - (4)].sval), (yyvsp[(4) - (4)].sval)); ;} break; case 17: -#line 355 "pir.y" +#line 357 "pir.y" { set_hll_map((yyvsp[(2) - (4)].sval), (yyvsp[(4) - (4)].sval)); ;} break; case 19: -#line 364 "pir.y" +#line 366 "pir.y" { (yyval.fixme) = NULL; ;} break; case 20: -#line 365 "pir.y" +#line 367 "pir.y" { (yyval.fixme) = (yyvsp[(2) - (3)].fixme); ;} break; case 21: -#line 368 "pir.y" +#line 370 "pir.y" { (yyval.fixme) = (yyvsp[(1) - (1)].sval); ;} break; case 22: -#line 369 "pir.y" +#line 371 "pir.y" { ;} break; case 23: -#line 374 "pir.y" - { new_sub(lexer, (yyvsp[(2) - (2)].sval)); ;} +#line 376 "pir.y" + { new_subr(lexer, (yyvsp[(2) - (2)].sval)); ;} break; case 24: -#line 375 "pir.y" +#line 377 "pir.y" { set_sub_flag(lexer, (yyvsp[(4) - (5)].ival)); ;} break; case 28: -#line 385 "pir.y" +#line 387 "pir.y" { (yyval.ival) = 0; ;} break; case 29: -#line 386 "pir.y" +#line 388 "pir.y" { SET_FLAG((yyval.ival), (yyvsp[(1) - (2)].ival)); ;} break; case 30: -#line 389 "pir.y" +#line 391 "pir.y" { (yyval.ival) = SUB_FLAG_ANON; ;} break; case 31: -#line 390 "pir.y" +#line 392 "pir.y" { (yyval.ival) = SUB_FLAG_INIT; ;} break; case 32: -#line 391 "pir.y" +#line 393 "pir.y" { (yyval.ival) = SUB_FLAG_LOAD; ;} break; case 33: -#line 392 "pir.y" +#line 394 "pir.y" { (yyval.ival) = SUB_FLAG_MAIN; ;} break; case 34: -#line 393 "pir.y" +#line 395 "pir.y" { (yyval.ival) = SUB_FLAG_METHOD; ;} break; case 35: -#line 394 "pir.y" +#line 396 "pir.y" { (yyval.ival) = SUB_FLAG_LEX; ;} break; case 36: -#line 395 "pir.y" +#line 397 "pir.y" { (yyval.ival) = SUB_FLAG_POSTCOMP; ;} break; case 37: -#line 396 "pir.y" +#line 398 "pir.y" { (yyval.ival) = SUB_FLAG_IMMEDIATE; ;} break; case 38: -#line 397 "pir.y" +#line 399 "pir.y" { (yyval.ival) = SUB_FLAG_MULTI; ;} break; case 39: -#line 398 "pir.y" +#line 400 "pir.y" { (yyval.ival) = SUB_FLAG_OUTER; set_sub_outer(lexer, (yyvsp[(3) - (4)].sval)); ;} break; case 40: -#line 399 "pir.y" +#line 401 "pir.y" { (yyval.ival) = SUB_FLAG_VTABLE; set_sub_vtable(lexer, (yyvsp[(2) - (2)].sval)); ;} break; case 49: -#line 416 "pir.y" +#line 418 "pir.y" { set_param_flag((yyvsp[(2) - (4)].targ), (yyvsp[(3) - (4)].ival)); IF_NAMED_PARAM_SET_ALIAS((yyvsp[(2) - (4)].targ), (yyvsp[(3) - (4)].ival)); ;} break; case 50: -#line 421 "pir.y" +#line 423 "pir.y" { (yyval.targ) = add_param(lexer, (yyvsp[(1) - (2)].ival), (yyvsp[(2) - (2)].sval)); ;} break; case 51: -#line 422 "pir.y" +#line 424 "pir.y" { (yyval.targ) = add_param_named(lexer, (yyvsp[(1) - (4)].ival), (yyvsp[(4) - (4)].sval), (yyvsp[(2) - (4)].sval)); ;} break; case 53: -#line 429 "pir.y" +#line 431 "pir.y" { new_instr(lexer); ;} break; case 55: -#line 434 "pir.y" +#line 436 "pir.y" { set_label(lexer, (yyvsp[(1) - (2)].sval)); ;} break; case 56: -#line 435 "pir.y" +#line 437 "pir.y" { set_label(lexer, (yyvsp[(1) - (2)].sval)); ;} break; case 71: -#line 454 "pir.y" +#line 456 "pir.y" { if (lexer->parse_errors > MAX_NUM_ERRORS) { fprintf(stderr, "Too many errors. Compilation aborted.\n"); exit(EXIT_FAILURE); /* fix: bail out and free() all memory */ @@ -2247,123 +2248,121 @@ break; case 72: -#line 462 "pir.y" +#line 464 "pir.y" { set_instr(lexer, "null"); add_operand(lexer, expr_from_target((yyvsp[(2) - (3)].targ))); ;} break; case 73: -#line 465 "pir.y" +#line 467 "pir.y" { set_instr(lexer, "null"); add_operand(lexer, expr_from_target((yyvsp[(1) - (4)].targ))); ;} break; case 74: -#line 470 "pir.y" +#line 472 "pir.y" { set_instr(lexer, "get_results"); add_operand(lexer, expr_from_target((yyvsp[(2) - (3)].targ))); ;} break; case 76: -#line 478 "pir.y" +#line 480 "pir.y" { add_operand(lexer, expr_from_target((yyvsp[(1) - (3)].targ))); ;} break; case 77: -#line 495 "pir.y" +#line 497 "pir.y" { assign(lexer, RHS_AUGMENT, (yyvsp[(1) - (2)].sval), (yyvsp[(2) - (2)].expr)); ;} break; case 78: -#line 496 "pir.y" - { assign(lexer, RHS_SETKEYED, (yyvsp[(1) - (3)].fixme), (yyvsp[(3) - (3)].expr)); ;} +#line 498 "pir.y" + { assign(lexer, RHS_SETKEYED, (yyvsp[(1) - (3)].expr), (yyvsp[(3) - (3)].expr)); ;} break; case 79: -#line 497 "pir.y" +#line 499 "pir.y" { /* nothing to do */ ;} break; case 80: -#line 500 "pir.y" +#line 502 "pir.y" { assign(lexer, RHS_UNOP, (yyvsp[(1) - (2)].sval), (yyvsp[(2) - (2)].expr)); ;} break; case 81: -#line 501 "pir.y" +#line 503 "pir.y" { assign(lexer, RHS_SIMPLE, (yyvsp[(1) - (1)].expr)); ;} break; case 82: -#line 502 "pir.y" +#line 504 "pir.y" { assign(lexer, RHS_BINOP, (yyvsp[(2) - (3)].sval), (yyvsp[(1) - (3)].expr), (yyvsp[(3) - (3)].expr)); ;} break; case 83: -#line 503 "pir.y" - { assign(lexer, RHS_GETKEYED, (yyvsp[(1) - (2)].targ), (yyvsp[(2) - (2)].fixme)); ;} +#line 505 "pir.y" + { assign(lexer, RHS_GETKEYED, (yyvsp[(1) - (2)].targ), (yyvsp[(2) - (2)].expr)); ;} break; case 84: -#line 504 "pir.y" +#line 506 "pir.y" { ;} break; case 85: -#line 508 "pir.y" +#line 510 "pir.y" { set_instr(lexer, (yyvsp[(1) - (1)].sval)); ;} break; case 90: -#line 520 "pir.y" +#line 522 "pir.y" { add_operand(lexer, (yyvsp[(1) - (1)].expr)); ;} break; case 91: -#line 524 "pir.y" +#line 526 "pir.y" { ;} break; case 92: -#line 525 "pir.y" - { add_operand(lexer, (yyvsp[(1) - (3)].expr)); ;} +#line 527 "pir.y" + { add_operand(lexer, (yyvsp[(3) - (3)].expr)); ;} break; case 95: -#line 532 "pir.y" - { (yyval.fixme) = (yyvsp[(2) - (3)].expr); ;} +#line 534 "pir.y" + { (yyval.expr) = (yyvsp[(2) - (3)].expr); ;} break; case 96: -#line 535 "pir.y" +#line 537 "pir.y" { (yyval.expr) = (yyvsp[(1) - (1)].expr); ;} break; case 97: -#line 536 "pir.y" +#line 538 "pir.y" { (yyval.expr) = add_key((yyvsp[(1) - (3)].expr), (yyvsp[(3) - (3)].expr)); ;} break; case 99: -#line 542 "pir.y" +#line 544 "pir.y" { (yyval.ival) = 0; ;} break; case 100: -#line 543 "pir.y" +#line 545 "pir.y" { (yyval.ival) = 1; ;} break; case 101: -#line 548 "pir.y" - { /**/ +#line 550 "pir.y" + { /* it was "unless", so "invert" the opcode */ if ((yyvsp[(1) - (5)].ival) > 0) { - /* it was "unless", so we need to - "invert" the opcode - */ + invert_instr(lexer); } add_operand(lexer, expr_from_ident((yyvsp[(4) - (5)].sval))); @@ -2585,7 +2584,7 @@ case 149: #line 696 "pir.y" - { (yyval.targ) = target_from_ident((yyvsp[(1) - (1)].sval)); ;} + { (yyval.targ) = target_from_ident((yyvsp[(1) - (1)].sval)); find_target(lexer, (yyvsp[(1) - (1)].sval));;} break; case 150: @@ -3206,7 +3205,7 @@ /* Line 1267 of yacc.c. */ -#line 3210 "pirparser.c" +#line 3209 "pirparser.c" default: break; } YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); Modified: trunk/compilers/pirc/new/pirparser.h ============================================================================== --- trunk/compilers/pirc/new/pirparser.h (original) +++ trunk/compilers/pirc/new/pirparser.h Sat Dec 1 06:55:00 2007 @@ -242,7 +242,7 @@ #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef union YYSTYPE -#line 83 "pir.y" +#line 84 "pir.y" { double dval; int ival; Added: trunk/compilers/pirc/new/pirsymbol.c ============================================================================== --- (empty file) +++ trunk/compilers/pirc/new/pirsymbol.c Sat Dec 1 06:55:00 2007 @@ -0,0 +1,31 @@ +/* + * $Id$ + * Copyright (C) 2007, The Perl Foundation. + */ +#include "pircompiler.h" +#include "pirsymbol.h" +#include "pircompunit.h" +#include <stdlib.h> + +void +declare_local(struct lexer_state *lexer, pir_type type, target *list) { + +} + +/* + +Return the target node for the symbol or NULL if the symbol +is not defined. + +*/ +target * +find_symbol(struct lexer_state *lexer, char * const name) { + return NULL; +} + +/* + * Local variables: + * c-file-style: "parrot" + * End: + * vim: expandtab shiftwidth=4: + */ Added: trunk/compilers/pirc/new/pirsymbol.h ============================================================================== --- (empty file) +++ trunk/compilers/pirc/new/pirsymbol.h Sat Dec 1 06:55:00 2007 @@ -0,0 +1,25 @@ +/* + * $Id$ + * Copyright (C) 2007, The Perl Foundation. + */ +#ifndef PARROT_PIR_PIRSYMBOL_H_GUARD +#define PARROT_PIR_PIRSYMBOL_H_GUARD + +#include "pircompiler.h" +#include "pircompunit.h" + +void declare_local(struct lexer_state *lexer, pir_type type, target *list); + +target *find_target(struct lexer_state *lexer, char * const name); + +#endif /* PARROT_PIR_PIRSYMBOL_H_GUARD */ + +/* + * Local variables: + * c-file-style: "parrot" + * End: + * vim: expandtab shiftwidth=4: + */ + + +