OSDN Git Service

* tree.h (build_int_cst): New, sign extended constant.
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-io.c
1 /* IO Code translation/library interface
2    Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "tree-gimple.h"
28 #include <stdio.h>
29 #include "ggc.h"
30 #include "toplev.h"
31 #include "real.h"
32 #include <assert.h>
33 #include <gmp.h>
34 #include "gfortran.h"
35 #include "trans.h"
36 #include "trans-stmt.h"
37 #include "trans-array.h"
38 #include "trans-types.h"
39 #include "trans-const.h"
40
41
42 static GTY(()) tree gfc_pint4_type_node;
43
44 /* Members of the ioparm structure.  */
45
46 static GTY(()) tree ioparm_unit;
47 static GTY(()) tree ioparm_err;
48 static GTY(()) tree ioparm_end;
49 static GTY(()) tree ioparm_eor;
50 static GTY(()) tree ioparm_list_format;
51 static GTY(()) tree ioparm_library_return;
52 static GTY(()) tree ioparm_iostat;
53 static GTY(()) tree ioparm_exist;
54 static GTY(()) tree ioparm_opened;
55 static GTY(()) tree ioparm_number;
56 static GTY(()) tree ioparm_named;
57 static GTY(()) tree ioparm_rec;
58 static GTY(()) tree ioparm_nextrec;
59 static GTY(()) tree ioparm_size;
60 static GTY(()) tree ioparm_recl_in;
61 static GTY(()) tree ioparm_recl_out;
62 static GTY(()) tree ioparm_iolength;
63 static GTY(()) tree ioparm_file;
64 static GTY(()) tree ioparm_file_len;
65 static GTY(()) tree ioparm_status;
66 static GTY(()) tree ioparm_status_len;
67 static GTY(()) tree ioparm_access;
68 static GTY(()) tree ioparm_access_len;
69 static GTY(()) tree ioparm_form;
70 static GTY(()) tree ioparm_form_len;
71 static GTY(()) tree ioparm_blank;
72 static GTY(()) tree ioparm_blank_len;
73 static GTY(()) tree ioparm_position;
74 static GTY(()) tree ioparm_position_len;
75 static GTY(()) tree ioparm_action;
76 static GTY(()) tree ioparm_action_len;
77 static GTY(()) tree ioparm_delim;
78 static GTY(()) tree ioparm_delim_len;
79 static GTY(()) tree ioparm_pad;
80 static GTY(()) tree ioparm_pad_len;
81 static GTY(()) tree ioparm_format;
82 static GTY(()) tree ioparm_format_len;
83 static GTY(()) tree ioparm_advance;
84 static GTY(()) tree ioparm_advance_len;
85 static GTY(()) tree ioparm_name;
86 static GTY(()) tree ioparm_name_len;
87 static GTY(()) tree ioparm_internal_unit;
88 static GTY(()) tree ioparm_internal_unit_len;
89 static GTY(()) tree ioparm_sequential;
90 static GTY(()) tree ioparm_sequential_len;
91 static GTY(()) tree ioparm_direct;
92 static GTY(()) tree ioparm_direct_len;
93 static GTY(()) tree ioparm_formatted;
94 static GTY(()) tree ioparm_formatted_len;
95 static GTY(()) tree ioparm_unformatted;
96 static GTY(()) tree ioparm_unformatted_len;
97 static GTY(()) tree ioparm_read;
98 static GTY(()) tree ioparm_read_len;
99 static GTY(()) tree ioparm_write;
100 static GTY(()) tree ioparm_write_len;
101 static GTY(()) tree ioparm_readwrite;
102 static GTY(()) tree ioparm_readwrite_len;
103 static GTY(()) tree ioparm_namelist_name;
104 static GTY(()) tree ioparm_namelist_name_len;
105 static GTY(()) tree ioparm_namelist_read_mode;
106
107 /* The global I/O variables */
108
109 static GTY(()) tree ioparm_var;
110 static GTY(()) tree locus_file;
111 static GTY(()) tree locus_line;
112
113
114 /* Library I/O subroutines */
115
116 static GTY(()) tree iocall_read;
117 static GTY(()) tree iocall_read_done;
118 static GTY(()) tree iocall_write;
119 static GTY(()) tree iocall_write_done;
120 static GTY(()) tree iocall_x_integer;
121 static GTY(()) tree iocall_x_logical;
122 static GTY(()) tree iocall_x_character;
123 static GTY(()) tree iocall_x_real;
124 static GTY(()) tree iocall_x_complex;
125 static GTY(()) tree iocall_open;
126 static GTY(()) tree iocall_close;
127 static GTY(()) tree iocall_inquire;
128 static GTY(()) tree iocall_iolength;
129 static GTY(()) tree iocall_iolength_done;
130 static GTY(()) tree iocall_rewind;
131 static GTY(()) tree iocall_backspace;
132 static GTY(()) tree iocall_endfile;
133 static GTY(()) tree iocall_set_nml_val_int;
134 static GTY(()) tree iocall_set_nml_val_float;
135 static GTY(()) tree iocall_set_nml_val_char;
136 static GTY(()) tree iocall_set_nml_val_complex;
137 static GTY(()) tree iocall_set_nml_val_log;
138
139 /* Variable for keeping track of what the last data transfer statement
140    was.  Used for deciding which subroutine to call when the data
141    transfer is complete. */
142 static enum { READ, WRITE, IOLENGTH } last_dt;
143
144 #define ADD_FIELD(name, type)                                           \
145   ioparm_ ## name = gfc_add_field_to_struct                             \
146         (&(TYPE_FIELDS (ioparm_type)), ioparm_type,                     \
147          get_identifier (stringize(name)), type)
148
149 #define ADD_STRING(name) \
150   ioparm_ ## name = gfc_add_field_to_struct                             \
151         (&(TYPE_FIELDS (ioparm_type)), ioparm_type,                     \
152          get_identifier (stringize(name)), pchar_type_node);            \
153   ioparm_ ## name ## _len = gfc_add_field_to_struct                     \
154         (&(TYPE_FIELDS (ioparm_type)), ioparm_type,                     \
155          get_identifier (stringize(name) "_len"), gfc_int4_type_node)
156
157
158 /* Create function decls for IO library functions.  */
159
160 void
161 gfc_build_io_library_fndecls (void)
162 {
163   tree ioparm_type;
164
165   gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
166
167 /* Build the st_parameter structure.  Information associated with I/O
168    calls are transferred here.  This must match the one defined in the
169    library exactly. */
170
171   ioparm_type = make_node (RECORD_TYPE);
172   TYPE_NAME (ioparm_type) = get_identifier ("_gfc_ioparm");
173
174   ADD_FIELD (unit, gfc_int4_type_node);
175   ADD_FIELD (err, gfc_int4_type_node);
176   ADD_FIELD (end, gfc_int4_type_node);
177   ADD_FIELD (eor, gfc_int4_type_node);
178   ADD_FIELD (list_format, gfc_int4_type_node);
179   ADD_FIELD (library_return, gfc_int4_type_node);
180
181   ADD_FIELD (iostat, gfc_pint4_type_node);
182   ADD_FIELD (exist, gfc_pint4_type_node);
183   ADD_FIELD (opened, gfc_pint4_type_node);
184   ADD_FIELD (number, gfc_pint4_type_node);
185   ADD_FIELD (named, gfc_pint4_type_node);
186   ADD_FIELD (rec, gfc_pint4_type_node);
187   ADD_FIELD (nextrec, gfc_pint4_type_node);
188   ADD_FIELD (size, gfc_pint4_type_node);
189
190   ADD_FIELD (recl_in, gfc_pint4_type_node);
191   ADD_FIELD (recl_out, gfc_pint4_type_node);
192
193   ADD_FIELD (iolength, gfc_pint4_type_node);
194
195   ADD_STRING (file);
196   ADD_STRING (status);
197
198   ADD_STRING (access);
199   ADD_STRING (form);
200   ADD_STRING (blank);
201   ADD_STRING (position);
202   ADD_STRING (action);
203   ADD_STRING (delim);
204   ADD_STRING (pad);
205   ADD_STRING (format);
206   ADD_STRING (advance);
207   ADD_STRING (name);
208   ADD_STRING (internal_unit);
209   ADD_STRING (sequential);
210
211   ADD_STRING (direct);
212   ADD_STRING (formatted);
213   ADD_STRING (unformatted);
214   ADD_STRING (read);
215   ADD_STRING (write);
216   ADD_STRING (readwrite);
217
218   ADD_STRING (namelist_name);
219   ADD_FIELD (namelist_read_mode, gfc_int4_type_node);
220
221   gfc_finish_type (ioparm_type);
222
223   ioparm_var = build_decl (VAR_DECL, get_identifier (PREFIX("ioparm")),
224                            ioparm_type);
225   DECL_EXTERNAL (ioparm_var) = 1;
226   TREE_PUBLIC (ioparm_var) = 1;
227
228   locus_line = build_decl (VAR_DECL, get_identifier (PREFIX("line")),
229                            gfc_int4_type_node);
230   DECL_EXTERNAL (locus_line) = 1;
231   TREE_PUBLIC (locus_line) = 1;
232
233   locus_file = build_decl (VAR_DECL, get_identifier (PREFIX("filename")),
234                            pchar_type_node);
235   DECL_EXTERNAL (locus_file) = 1;
236   TREE_PUBLIC (locus_file) = 1;
237
238   /* Define the transfer functions.  */
239
240   iocall_x_integer =
241     gfc_build_library_function_decl (get_identifier
242                                      (PREFIX("transfer_integer")),
243                                      void_type_node, 2, pvoid_type_node,
244                                      gfc_int4_type_node);
245
246   iocall_x_logical =
247     gfc_build_library_function_decl (get_identifier
248                                      (PREFIX("transfer_logical")),
249                                      void_type_node, 2, pvoid_type_node,
250                                      gfc_int4_type_node);
251
252   iocall_x_character =
253     gfc_build_library_function_decl (get_identifier
254                                      (PREFIX("transfer_character")),
255                                      void_type_node, 2, pvoid_type_node,
256                                      gfc_int4_type_node);
257
258   iocall_x_real =
259     gfc_build_library_function_decl (get_identifier (PREFIX("transfer_real")),
260                                      void_type_node, 2,
261                                      pvoid_type_node, gfc_int4_type_node);
262
263   iocall_x_complex =
264     gfc_build_library_function_decl (get_identifier
265                                      (PREFIX("transfer_complex")),
266                                      void_type_node, 2, pvoid_type_node,
267                                      gfc_int4_type_node);
268
269   /* Library entry points */
270
271   iocall_read =
272     gfc_build_library_function_decl (get_identifier (PREFIX("st_read")),
273                                      void_type_node, 0);
274
275   iocall_write =
276     gfc_build_library_function_decl (get_identifier (PREFIX("st_write")),
277                                      void_type_node, 0);
278   iocall_open =
279     gfc_build_library_function_decl (get_identifier (PREFIX("st_open")),
280                                      void_type_node, 0);
281
282   iocall_close =
283     gfc_build_library_function_decl (get_identifier (PREFIX("st_close")),
284                                      void_type_node, 0);
285
286   iocall_inquire =
287     gfc_build_library_function_decl (get_identifier (PREFIX("st_inquire")),
288                                      gfc_int4_type_node, 0);
289
290   iocall_iolength =
291     gfc_build_library_function_decl(get_identifier (PREFIX("st_iolength")),
292                                     void_type_node, 0);
293
294   iocall_rewind =
295     gfc_build_library_function_decl (get_identifier (PREFIX("st_rewind")),
296                                      gfc_int4_type_node, 0);
297
298   iocall_backspace =
299     gfc_build_library_function_decl (get_identifier (PREFIX("st_backspace")),
300                                      gfc_int4_type_node, 0);
301
302   iocall_endfile =
303     gfc_build_library_function_decl (get_identifier (PREFIX("st_endfile")),
304                                      gfc_int4_type_node, 0);
305   /* Library helpers */
306
307   iocall_read_done =
308     gfc_build_library_function_decl (get_identifier (PREFIX("st_read_done")),
309                                      gfc_int4_type_node, 0);
310
311   iocall_write_done =
312     gfc_build_library_function_decl (get_identifier (PREFIX("st_write_done")),
313                                      gfc_int4_type_node, 0);
314
315   iocall_iolength_done =
316     gfc_build_library_function_decl (get_identifier (PREFIX("st_iolength_done")),
317                                      gfc_int4_type_node, 0);
318
319   iocall_set_nml_val_int =
320     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_int")),
321                                      void_type_node, 4,
322                                      pvoid_type_node, pvoid_type_node,
323                                      gfc_int4_type_node,gfc_int4_type_node);
324
325   iocall_set_nml_val_float =
326     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_float")),
327                                      void_type_node, 4,
328                                      pvoid_type_node, pvoid_type_node,
329                                      gfc_int4_type_node,gfc_int4_type_node);
330   iocall_set_nml_val_char =
331     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_char")),
332                                      void_type_node, 5,
333                                      pvoid_type_node, pvoid_type_node,
334                                      gfc_int4_type_node, gfc_int4_type_node, 
335                                      gfc_strlen_type_node);
336   iocall_set_nml_val_complex =
337     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_complex")),
338                                      void_type_node, 4,
339                                      pvoid_type_node, pvoid_type_node,
340                                      gfc_int4_type_node,gfc_int4_type_node);
341   iocall_set_nml_val_log =
342     gfc_build_library_function_decl (get_identifier (PREFIX("st_set_nml_var_log")),
343                                      void_type_node, 4,
344                                      pvoid_type_node, pvoid_type_node,
345                                      gfc_int4_type_node,gfc_int4_type_node);
346
347 }
348
349
350 /* Generate code to store an non-string I/O parameter into the
351    ioparm structure.  This is a pass by value.  */
352
353 static void
354 set_parameter_value (stmtblock_t * block, tree var, gfc_expr * e)
355 {
356   gfc_se se;
357   tree tmp;
358
359   gfc_init_se (&se, NULL);
360   gfc_conv_expr_type (&se, e, TREE_TYPE (var));
361   gfc_add_block_to_block (block, &se.pre);
362
363   tmp = build (COMPONENT_REF, TREE_TYPE (var), ioparm_var, var, NULL_TREE);
364   gfc_add_modify_expr (block, tmp, se.expr);
365 }
366
367
368 /* Generate code to store an non-string I/O parameter into the
369    ioparm structure.  This is pass by reference.  */
370
371 static void
372 set_parameter_ref (stmtblock_t * block, tree var, gfc_expr * e)
373 {
374   gfc_se se;
375   tree tmp;
376
377   gfc_init_se (&se, NULL);
378   se.want_pointer = 1;
379
380   gfc_conv_expr_type (&se, e, TREE_TYPE (var));
381   gfc_add_block_to_block (block, &se.pre);
382
383   tmp = build (COMPONENT_REF, TREE_TYPE (var), ioparm_var, var, NULL_TREE);
384   gfc_add_modify_expr (block, tmp, se.expr);
385 }
386
387
388 /* Generate code to store a string and its length into the
389    ioparm structure.  */
390
391 static void
392 set_string (stmtblock_t * block, stmtblock_t * postblock, tree var,
393             tree var_len, gfc_expr * e)
394 {
395   gfc_se se;
396   tree tmp;
397   tree msg;
398   tree io;
399   tree len;
400
401   gfc_init_se (&se, NULL);
402   gfc_conv_expr (&se, e);
403
404   io = build (COMPONENT_REF, TREE_TYPE (var), ioparm_var, var, NULL_TREE);
405   len = build (COMPONENT_REF, TREE_TYPE (var_len), ioparm_var, var_len,
406                NULL_TREE);
407
408   /* Integer variable assigned a format label.  */
409   if (e->ts.type == BT_INTEGER && e->symtree->n.sym->attr.assign == 1)
410     {
411       msg =
412         gfc_build_string_const (37, "Assigned label is not a format label");
413       tmp = GFC_DECL_STRING_LEN (se.expr);
414       tmp = build (LE_EXPR, boolean_type_node,
415                    tmp, convert (TREE_TYPE (tmp), integer_minus_one_node));
416       gfc_trans_runtime_check (tmp, msg, &se.pre);
417       gfc_add_modify_expr (&se.pre, io, GFC_DECL_ASSIGN_ADDR (se.expr));
418       gfc_add_modify_expr (&se.pre, len, GFC_DECL_STRING_LEN (se.expr));
419     }
420   else
421     {
422       gfc_conv_string_parameter (&se);
423       gfc_add_modify_expr (&se.pre, io, fold_convert (TREE_TYPE (io), se.expr));
424       gfc_add_modify_expr (&se.pre, len, se.string_length);
425     }
426
427   gfc_add_block_to_block (block, &se.pre);
428   gfc_add_block_to_block (postblock, &se.post);
429
430 }
431
432
433 /* Set a member of the ioparm structure to one.  */
434 static void
435 set_flag (stmtblock_t *block, tree var)
436 {
437   tree tmp, type = TREE_TYPE (var);
438
439   tmp = build (COMPONENT_REF, type, ioparm_var, var, NULL_TREE);
440   gfc_add_modify_expr (block, tmp, convert (type, integer_one_node));
441 }
442
443
444 /* Add a case to a IO-result switch.  */
445
446 static void
447 add_case (int label_value, gfc_st_label * label, stmtblock_t * body)
448 {
449   tree tmp, value;
450
451   if (label == NULL)
452     return;                     /* No label, no case */
453
454   value = build_int_cst (NULL_TREE, label_value);
455
456   /* Make a backend label for this case.  */
457   tmp = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
458   DECL_CONTEXT (tmp) = current_function_decl;
459
460   /* And the case itself.  */
461   tmp = build_v (CASE_LABEL_EXPR, value, NULL_TREE, tmp);
462   gfc_add_expr_to_block (body, tmp);
463
464   /* Jump to the label.  */
465   tmp = build1_v (GOTO_EXPR, gfc_get_label_decl (label));
466   gfc_add_expr_to_block (body, tmp);
467 }
468
469
470 /* Generate a switch statement that branches to the correct I/O
471    result label.  The last statement of an I/O call stores the
472    result into a variable because there is often cleanup that
473    must be done before the switch, so a temporary would have to
474    be created anyway.  */
475
476 static void
477 io_result (stmtblock_t * block, gfc_st_label * err_label,
478            gfc_st_label * end_label, gfc_st_label * eor_label)
479 {
480   stmtblock_t body;
481   tree tmp, rc;
482
483   /* If no labels are specified, ignore the result instead
484      of building an empty switch.  */
485   if (err_label == NULL
486       && end_label == NULL
487       && eor_label == NULL)
488     return;
489
490   /* Build a switch statement.  */
491   gfc_start_block (&body);
492
493   /* The label values here must be the same as the values
494      in the library_return enum in the runtime library */
495   add_case (1, err_label, &body);
496   add_case (2, end_label, &body);
497   add_case (3, eor_label, &body);
498
499   tmp = gfc_finish_block (&body);
500
501   rc = build (COMPONENT_REF, TREE_TYPE (ioparm_library_return), ioparm_var,
502               ioparm_library_return, NULL_TREE);
503
504   tmp = build_v (SWITCH_EXPR, rc, tmp, NULL_TREE);
505
506   gfc_add_expr_to_block (block, tmp);
507 }
508
509
510 /* Store the current file and line number to variables so that if a
511    library call goes awry, we can tell the user where the problem is.  */
512
513 static void
514 set_error_locus (stmtblock_t * block, locus * where)
515 {
516   gfc_file *f;
517   tree tmp;
518   int line;
519
520   f = where->lb->file;
521   tmp = gfc_build_string_const (strlen (f->filename) + 1, f->filename);
522
523   tmp = gfc_build_addr_expr (pchar_type_node, tmp);
524   gfc_add_modify_expr (block, locus_file, tmp);
525
526   line = where->lb->linenum;
527   gfc_add_modify_expr (block, locus_line, build_int_cst (NULL_TREE, line));
528 }
529
530
531 /* Translate an OPEN statement.  */
532
533 tree
534 gfc_trans_open (gfc_code * code)
535 {
536   stmtblock_t block, post_block;
537   gfc_open *p;
538   tree tmp;
539
540   gfc_init_block (&block);
541   gfc_init_block (&post_block);
542
543   set_error_locus (&block, &code->loc);
544   p = code->ext.open;
545
546   if (p->unit)
547     set_parameter_value (&block, ioparm_unit, p->unit);
548
549   if (p->file)
550     set_string (&block, &post_block, ioparm_file, ioparm_file_len, p->file);
551
552   if (p->status)
553     set_string (&block, &post_block, ioparm_status,
554                 ioparm_status_len, p->status);
555
556   if (p->access)
557     set_string (&block, &post_block, ioparm_access,
558                 ioparm_access_len, p->access);
559
560   if (p->form)
561     set_string (&block, &post_block, ioparm_form, ioparm_form_len, p->form);
562
563   if (p->recl)
564     set_parameter_value (&block, ioparm_recl_in, p->recl);
565
566   if (p->blank)
567     set_string (&block, &post_block, ioparm_blank, ioparm_blank_len,
568                 p->blank);
569
570   if (p->position)
571     set_string (&block, &post_block, ioparm_position,
572                 ioparm_position_len, p->position);
573
574   if (p->action)
575     set_string (&block, &post_block, ioparm_action,
576                 ioparm_action_len, p->action);
577
578   if (p->delim)
579     set_string (&block, &post_block, ioparm_delim, ioparm_delim_len,
580                 p->delim);
581
582   if (p->pad)
583     set_string (&block, &post_block, ioparm_pad, ioparm_pad_len, p->pad);
584
585   if (p->iostat)
586     set_parameter_ref (&block, ioparm_iostat, p->iostat);
587
588   if (p->err)
589     set_flag (&block, ioparm_err);
590
591   tmp = gfc_build_function_call (iocall_open, NULL_TREE);
592   gfc_add_expr_to_block (&block, tmp);
593
594   gfc_add_block_to_block (&block, &post_block);
595
596   io_result (&block, p->err, NULL, NULL);
597
598   return gfc_finish_block (&block);
599 }
600
601
602 /* Translate a CLOSE statement.  */
603
604 tree
605 gfc_trans_close (gfc_code * code)
606 {
607   stmtblock_t block, post_block;
608   gfc_close *p;
609   tree tmp;
610
611   gfc_init_block (&block);
612   gfc_init_block (&post_block);
613
614   set_error_locus (&block, &code->loc);
615   p = code->ext.close;
616
617   if (p->unit)
618     set_parameter_value (&block, ioparm_unit, p->unit);
619
620   if (p->status)
621     set_string (&block, &post_block, ioparm_status,
622                 ioparm_status_len, p->status);
623
624   if (p->iostat)
625     set_parameter_ref (&block, ioparm_iostat, p->iostat);
626
627   if (p->err)
628     set_flag (&block, ioparm_err);
629
630   tmp = gfc_build_function_call (iocall_close, NULL_TREE);
631   gfc_add_expr_to_block (&block, tmp);
632
633   gfc_add_block_to_block (&block, &post_block);
634
635   io_result (&block, p->err, NULL, NULL);
636
637   return gfc_finish_block (&block);
638 }
639
640
641 /* Common subroutine for building a file positioning statement.  */
642
643 static tree
644 build_filepos (tree function, gfc_code * code)
645 {
646   stmtblock_t block;
647   gfc_filepos *p;
648   tree tmp;
649
650   p = code->ext.filepos;
651
652   gfc_init_block (&block);
653
654   set_error_locus (&block, &code->loc);
655
656   if (p->unit)
657     set_parameter_value (&block, ioparm_unit, p->unit);
658
659   if (p->iostat)
660     set_parameter_ref (&block, ioparm_iostat, p->iostat);
661
662   if (p->err)
663     set_flag (&block, ioparm_err);
664
665   tmp = gfc_build_function_call (function, NULL);
666   gfc_add_expr_to_block (&block, tmp);
667
668   io_result (&block, p->err, NULL, NULL);
669
670   return gfc_finish_block (&block);
671 }
672
673
674 /* Translate a BACKSPACE statement.  */
675
676 tree
677 gfc_trans_backspace (gfc_code * code)
678 {
679
680   return build_filepos (iocall_backspace, code);
681 }
682
683
684 /* Translate an ENDFILE statement.  */
685
686 tree
687 gfc_trans_endfile (gfc_code * code)
688 {
689
690   return build_filepos (iocall_endfile, code);
691 }
692
693
694 /* Translate a REWIND statement.  */
695
696 tree
697 gfc_trans_rewind (gfc_code * code)
698 {
699
700   return build_filepos (iocall_rewind, code);
701 }
702
703
704 /* Translate the non-IOLENGTH form of an INQUIRE statement.  */
705
706 tree
707 gfc_trans_inquire (gfc_code * code)
708 {
709   stmtblock_t block, post_block;
710   gfc_inquire *p;
711   tree tmp;
712
713   gfc_init_block (&block);
714   gfc_init_block (&post_block);
715
716   set_error_locus (&block, &code->loc);
717   p = code->ext.inquire;
718
719   if (p->unit)
720     set_parameter_value (&block, ioparm_unit, p->unit);
721
722   if (p->file)
723     set_string (&block, &post_block, ioparm_file, ioparm_file_len, p->file);
724
725   if (p->iostat)
726     set_parameter_ref (&block, ioparm_iostat, p->iostat);
727
728   if (p->exist)
729     set_parameter_ref (&block, ioparm_exist, p->exist);
730
731   if (p->opened)
732     set_parameter_ref (&block, ioparm_opened, p->opened);
733
734   if (p->number)
735     set_parameter_ref (&block, ioparm_number, p->number);
736
737   if (p->named)
738     set_parameter_ref (&block, ioparm_named, p->named);
739
740   if (p->name)
741     set_string (&block, &post_block, ioparm_name, ioparm_name_len, p->name);
742
743   if (p->access)
744     set_string (&block, &post_block, ioparm_access,
745                 ioparm_access_len, p->access);
746
747   if (p->sequential)
748     set_string (&block, &post_block, ioparm_sequential,
749                 ioparm_sequential_len, p->sequential);
750
751   if (p->direct)
752     set_string (&block, &post_block, ioparm_direct,
753                 ioparm_direct_len, p->direct);
754
755   if (p->form)
756     set_string (&block, &post_block, ioparm_form, ioparm_form_len, p->form);
757
758   if (p->formatted)
759     set_string (&block, &post_block, ioparm_formatted,
760                 ioparm_formatted_len, p->formatted);
761
762   if (p->unformatted)
763     set_string (&block, &post_block, ioparm_unformatted,
764                 ioparm_unformatted_len, p->unformatted);
765
766   if (p->recl)
767     set_parameter_ref (&block, ioparm_recl_out, p->recl);
768
769   if (p->nextrec)
770     set_parameter_ref (&block, ioparm_nextrec, p->nextrec);
771
772   if (p->blank)
773     set_string (&block, &post_block, ioparm_blank, ioparm_blank_len,
774                 p->blank);
775
776   if (p->position)
777     set_string (&block, &post_block, ioparm_position,
778                 ioparm_position_len, p->position);
779
780   if (p->action)
781     set_string (&block, &post_block, ioparm_action,
782                 ioparm_action_len, p->action);
783
784   if (p->read)
785     set_string (&block, &post_block, ioparm_read, ioparm_read_len, p->read);
786
787   if (p->write)
788     set_string (&block, &post_block, ioparm_write,
789                 ioparm_write_len, p->write);
790
791   if (p->readwrite)
792     set_string (&block, &post_block, ioparm_readwrite,
793                 ioparm_readwrite_len, p->readwrite);
794
795   if (p->delim)
796     set_string (&block, &post_block, ioparm_delim, ioparm_delim_len,
797                 p->delim);
798
799   if (p->err)
800     set_flag (&block, ioparm_err);
801
802   tmp = gfc_build_function_call (iocall_inquire, NULL);
803   gfc_add_expr_to_block (&block, tmp);
804
805   gfc_add_block_to_block (&block, &post_block);
806
807   io_result (&block, p->err, NULL, NULL);
808
809   return gfc_finish_block (&block);
810 }
811
812
813 static gfc_expr *
814 gfc_new_nml_name_expr (char * name)
815 {
816    gfc_expr * nml_name;
817    nml_name = gfc_get_expr();
818    nml_name->ref = NULL;
819    nml_name->expr_type = EXPR_CONSTANT;
820    nml_name->ts.kind = gfc_default_character_kind ();
821    nml_name->ts.type = BT_CHARACTER;
822    nml_name->value.character.length = strlen(name);
823    nml_name->value.character.string = name;
824
825    return nml_name;
826 }
827
828 static gfc_expr *
829 get_new_var_expr(gfc_symbol * sym)
830 {
831   gfc_expr * nml_var;
832
833   nml_var = gfc_get_expr();
834   nml_var->expr_type = EXPR_VARIABLE;
835   nml_var->ts = sym->ts;
836   if (sym->as)
837     nml_var->rank = sym->as->rank;
838   nml_var->symtree = (gfc_symtree *)gfc_getmem (sizeof (gfc_symtree));
839   nml_var->symtree->n.sym = sym;
840   nml_var->where = sym->declared_at;
841   sym->attr.referenced = 1;
842
843   return nml_var;
844 }
845
846 /* For a scalar variable STRING whose address is ADDR_EXPR, generate a
847    call to iocall_set_nml_val.  For derived type variable, recursively
848    generate calls to iocall_set_nml_val for each leaf field. The leafs
849    have no names -- their STRING field is null, and are interpreted by
850    the run-time library as having only the value, as in the example:
851
852    &foo bzz=1,2,3,4,5/
853
854    Note that the first output field appears after the name of the
855    variable, not of the field name.  This causes a little complication
856    documented below. */
857
858 static void
859 transfer_namelist_element (stmtblock_t * block, gfc_typespec * ts, tree addr_expr, 
860                            tree string, tree string_length)
861 {
862   tree tmp, args, arg2;
863   tree expr;
864
865   assert (POINTER_TYPE_P (TREE_TYPE (addr_expr)));
866
867   if (ts->type == BT_DERIVED)
868     {
869       gfc_component *c;
870       expr = gfc_build_indirect_ref (addr_expr);
871
872       for (c = ts->derived->components; c; c = c->next)
873         {
874           tree field = c->backend_decl;
875           assert (field && TREE_CODE (field) == FIELD_DECL);
876           tmp = build (COMPONENT_REF, TREE_TYPE (field), expr, field, NULL_TREE);
877
878           if (c->dimension)
879             gfc_todo_error ("NAMELIST IO of array in derived type");
880           if (!c->pointer)
881             tmp = gfc_build_addr_expr (NULL, tmp);
882           transfer_namelist_element (block, &c->ts, tmp, string, string_length);
883
884           /* The first output field bears the name of the topmost
885              derived type variable.  All other fields are anonymous
886              and appear with nulls in their string and string_length
887              fields.  After the first use, we set string and
888              string_length to null. */
889           string = null_pointer_node;
890           string_length = integer_zero_node;
891         }
892
893       return;
894     }
895
896   args = gfc_chainon_list (NULL_TREE, addr_expr);
897   args = gfc_chainon_list (args, string);
898   args = gfc_chainon_list (args, string_length);
899   arg2 = build_int_cst (gfc_array_index_type, ts->kind);
900   args = gfc_chainon_list (args,arg2);
901
902   switch (ts->type)
903     {
904     case BT_INTEGER:
905       tmp = gfc_build_function_call (iocall_set_nml_val_int, args);
906       break;
907
908     case BT_CHARACTER:
909       expr = gfc_build_indirect_ref (addr_expr);
910       assert (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE);
911       args = gfc_chainon_list (args,
912                                TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (expr))));
913       tmp = gfc_build_function_call (iocall_set_nml_val_char, args);
914       break;
915
916     case BT_REAL:
917       tmp = gfc_build_function_call (iocall_set_nml_val_float, args);
918       break;
919
920     case BT_LOGICAL:
921       tmp = gfc_build_function_call (iocall_set_nml_val_log, args);
922       break;
923
924     case BT_COMPLEX:
925       tmp = gfc_build_function_call (iocall_set_nml_val_complex, args);
926       break;
927
928     default :
929       internal_error ("Bad namelist IO basetype (%d)", ts->type);
930     }
931
932   gfc_add_expr_to_block (block, tmp);
933 }
934
935 /* Create a data transfer statement.  Not all of the fields are valid
936    for both reading and writing, but improper use has been filtered
937    out by now.  */
938
939 static tree
940 build_dt (tree * function, gfc_code * code)
941 {
942   stmtblock_t block, post_block;
943   gfc_dt *dt;
944   tree tmp;
945   gfc_expr *nmlname, *nmlvar;
946   gfc_namelist *nml;
947   gfc_se se,se2;
948
949   gfc_init_block (&block);
950   gfc_init_block (&post_block);
951
952   set_error_locus (&block, &code->loc);
953   dt = code->ext.dt;
954
955   assert (dt != NULL);
956
957   if (dt->io_unit)
958     {
959       if (dt->io_unit->ts.type == BT_CHARACTER)
960         {
961           set_string (&block, &post_block, ioparm_internal_unit,
962                       ioparm_internal_unit_len, dt->io_unit);
963         }
964       else
965         set_parameter_value (&block, ioparm_unit, dt->io_unit);
966     }
967
968   if (dt->rec)
969     set_parameter_value (&block, ioparm_rec, dt->rec);
970
971   if (dt->advance)
972     set_string (&block, &post_block, ioparm_advance, ioparm_advance_len,
973                 dt->advance);
974
975   if (dt->format_expr)
976     set_string (&block, &post_block, ioparm_format, ioparm_format_len,
977                 dt->format_expr);
978
979   if (dt->format_label)
980     {
981       if (dt->format_label == &format_asterisk)
982         set_flag (&block, ioparm_list_format);
983       else
984         set_string (&block, &post_block, ioparm_format,
985                     ioparm_format_len, dt->format_label->format);
986     }
987
988   if (dt->iostat)
989     set_parameter_ref (&block, ioparm_iostat, dt->iostat);
990
991   if (dt->size)
992     set_parameter_ref (&block, ioparm_size, dt->size);
993
994   if (dt->err)
995     set_flag (&block, ioparm_err);
996
997   if (dt->eor)
998     set_flag(&block, ioparm_eor);
999
1000   if (dt->end)
1001     set_flag(&block, ioparm_end);
1002
1003   if (dt->namelist)
1004     {
1005        if (dt->format_expr || dt->format_label)
1006           fatal_error("A format cannot be specified with a namelist");
1007
1008        nmlname = gfc_new_nml_name_expr(dt->namelist->name);
1009
1010        set_string (&block, &post_block, ioparm_namelist_name,
1011                 ioparm_namelist_name_len, nmlname);
1012
1013        if (last_dt == READ)
1014           set_flag (&block, ioparm_namelist_read_mode);
1015
1016         for (nml = dt->namelist->namelist; nml; nml = nml->next)
1017           {
1018             gfc_init_se (&se, NULL);
1019             gfc_init_se (&se2, NULL);
1020             nmlvar = get_new_var_expr (nml->sym);
1021             nmlname = gfc_new_nml_name_expr (nml->sym->name);
1022             gfc_conv_expr_reference (&se2, nmlname);
1023             gfc_conv_expr_reference (&se, nmlvar);
1024             gfc_evaluate_now (se.expr, &se.pre); 
1025
1026             transfer_namelist_element (&block, &nml->sym->ts, se.expr,
1027                                        se2.expr, se2.string_length);
1028           }
1029     }
1030
1031   tmp = gfc_build_function_call (*function, NULL_TREE);
1032   gfc_add_expr_to_block (&block, tmp);
1033
1034   gfc_add_block_to_block (&block, &post_block);
1035
1036   return gfc_finish_block (&block);
1037 }
1038
1039
1040 /* Translate the IOLENGTH form of an INQUIRE statement.  We treat
1041    this as a third sort of data transfer statement, except that
1042    lengths are summed instead of actually transfering any data.  */
1043
1044 tree
1045 gfc_trans_iolength (gfc_code * code)
1046 {
1047   stmtblock_t block;
1048   gfc_inquire *inq;
1049   tree dt;
1050
1051   gfc_init_block (&block);
1052
1053   set_error_locus (&block, &code->loc);
1054
1055   inq = code->ext.inquire;
1056
1057   /* First check that preconditions are met.  */
1058   assert(inq != NULL);
1059   assert(inq->iolength != NULL);
1060
1061   /* Connect to the iolength variable.  */
1062   if (inq->iolength)
1063     set_parameter_ref (&block, ioparm_iolength, inq->iolength);
1064
1065   /* Actual logic.  */
1066   last_dt = IOLENGTH;
1067   dt = build_dt(&iocall_iolength, code);
1068
1069   gfc_add_expr_to_block (&block, dt);
1070
1071   return gfc_finish_block (&block);
1072 }
1073
1074
1075 /* Translate a READ statement.  */
1076
1077 tree
1078 gfc_trans_read (gfc_code * code)
1079 {
1080
1081   last_dt = READ;
1082   return build_dt (&iocall_read, code);
1083 }
1084
1085
1086 /* Translate a WRITE statement */
1087
1088 tree
1089 gfc_trans_write (gfc_code * code)
1090 {
1091
1092   last_dt = WRITE;
1093   return build_dt (&iocall_write, code);
1094 }
1095
1096
1097 /* Finish a data transfer statement.  */
1098
1099 tree
1100 gfc_trans_dt_end (gfc_code * code)
1101 {
1102   tree function, tmp;
1103   stmtblock_t block;
1104
1105   gfc_init_block (&block);
1106
1107   switch (last_dt)
1108     {
1109     case READ:
1110       function = iocall_read_done;
1111       break;
1112
1113     case WRITE:
1114       function = iocall_write_done;
1115       break;
1116
1117     case IOLENGTH:
1118       function = iocall_iolength_done;
1119       break;
1120
1121     default:
1122       abort ();
1123     }
1124
1125   tmp = gfc_build_function_call (function, NULL);
1126   gfc_add_expr_to_block (&block, tmp);
1127
1128   if (last_dt != IOLENGTH)
1129     {
1130       assert(code->ext.dt != NULL);
1131       io_result (&block, code->ext.dt->err,
1132                  code->ext.dt->end, code->ext.dt->eor);
1133     }
1134
1135   return gfc_finish_block (&block);
1136 }
1137
1138
1139 /* Generate the call for a scalar transfer node.  */
1140
1141 static void
1142 transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr)
1143 {
1144   tree args, tmp, function, arg2, field, expr;
1145   gfc_component *c;
1146   int kind;
1147
1148   kind = ts->kind;
1149   function = NULL;
1150   arg2 = NULL;
1151
1152   switch (ts->type)
1153     {
1154     case BT_INTEGER:
1155       arg2 = build_int_cst (NULL_TREE, kind);
1156       function = iocall_x_integer;
1157       break;
1158
1159     case BT_REAL:
1160       arg2 = build_int_cst (NULL_TREE, kind);
1161       function = iocall_x_real;
1162       break;
1163
1164     case BT_COMPLEX:
1165       arg2 = build_int_cst (NULL_TREE, kind);
1166       function = iocall_x_complex;
1167       break;
1168
1169     case BT_LOGICAL:
1170       arg2 = build_int_cst (NULL_TREE, kind);
1171       function = iocall_x_logical;
1172       break;
1173
1174     case BT_CHARACTER:
1175       arg2 = se->string_length;
1176       function = iocall_x_character;
1177       break;
1178
1179     case BT_DERIVED:
1180       expr = gfc_evaluate_now (addr_expr, &se->pre);
1181       expr = gfc_build_indirect_ref (expr);
1182
1183       for (c = ts->derived->components; c; c = c->next)
1184         {
1185           field = c->backend_decl;
1186           assert (field && TREE_CODE (field) == FIELD_DECL);
1187
1188           tmp = build (COMPONENT_REF, TREE_TYPE (field), expr, field,
1189                        NULL_TREE);
1190
1191           if (c->ts.type == BT_CHARACTER)
1192             {
1193               assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
1194               se->string_length =
1195                 TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tmp)));
1196             }
1197           if (c->dimension)
1198             gfc_todo_error ("IO of arrays in derived types");
1199           if (!c->pointer)
1200             tmp = gfc_build_addr_expr (NULL, tmp);
1201           transfer_expr (se, &c->ts, tmp);
1202         }
1203       return;
1204
1205     default:
1206       internal_error ("Bad IO basetype (%d)", ts->type);
1207     }
1208
1209   args = gfc_chainon_list (NULL_TREE, addr_expr);
1210   args = gfc_chainon_list (args, arg2);
1211
1212   tmp = gfc_build_function_call (function, args);
1213   gfc_add_expr_to_block (&se->pre, tmp);
1214   gfc_add_block_to_block (&se->pre, &se->post);
1215
1216 }
1217
1218
1219 /* gfc_trans_transfer()-- Translate a TRANSFER code node */
1220
1221 tree
1222 gfc_trans_transfer (gfc_code * code)
1223 {
1224   stmtblock_t block, body;
1225   gfc_loopinfo loop;
1226   gfc_expr *expr;
1227   gfc_ss *ss;
1228   gfc_se se;
1229   tree tmp;
1230
1231   gfc_start_block (&block);
1232
1233   expr = code->expr;
1234   ss = gfc_walk_expr (expr);
1235
1236   gfc_init_se (&se, NULL);
1237
1238   if (ss == gfc_ss_terminator)
1239     gfc_init_block (&body);
1240   else
1241     {
1242       /* Initialize the scalarizer.  */
1243       gfc_init_loopinfo (&loop);
1244       gfc_add_ss_to_loop (&loop, ss);
1245
1246       /* Initialize the loop.  */
1247       gfc_conv_ss_startstride (&loop);
1248       gfc_conv_loop_setup (&loop);
1249
1250       /* The main loop body.  */
1251       gfc_mark_ss_chain_used (ss, 1);
1252       gfc_start_scalarized_body (&loop, &body);
1253
1254       gfc_copy_loopinfo_to_se (&se, &loop);
1255       se.ss = ss;
1256     }
1257
1258   gfc_conv_expr_reference (&se, expr);
1259
1260   transfer_expr (&se, &expr->ts, se.expr);
1261
1262   gfc_add_block_to_block (&body, &se.pre);
1263   gfc_add_block_to_block (&body, &se.post);
1264
1265   if (se.ss == NULL)
1266     tmp = gfc_finish_block (&body);
1267   else
1268     {
1269       assert (se.ss == gfc_ss_terminator);
1270       gfc_trans_scalarizing_loops (&loop, &body);
1271
1272       gfc_add_block_to_block (&loop.pre, &loop.post);
1273       tmp = gfc_finish_block (&loop.pre);
1274       gfc_cleanup_loop (&loop);
1275     }
1276
1277   gfc_add_expr_to_block (&block, tmp);
1278
1279   return gfc_finish_block (&block);;
1280 }
1281
1282 #include "gt-fortran-trans-io.h"
1283