aboutsummaryrefslogtreecommitdiff
path: root/gcc/algol68/a68-low-posix.cc
blob: 6b6ae76cf375fb4acb10f71450fe0e59192c3808 (plain)
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* Lowering routines for the POSIX prelude.
   Copyright (C) 2025 Jose E. Marchesi.

   Written by Jose E. Marchesi.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

#define INCLUDE_MEMORY
#include "config.h"
#include "system.h"
#include "coretypes.h"

#include "tree.h"
#include "fold-const.h"
#include "diagnostic.h"
#include "langhooks.h"
#include "tm.h"
#include "function.h"
#include "cgraph.h"
#include "toplev.h"
#include "varasm.h"
#include "predict.h"
#include "stor-layout.h"
#include "tree-iterator.h"
#include "stringpool.h"
#include "print-tree.h"
#include "gimplify.h"
#include "dumpfile.h"
#include "convert.h"

#include "a68.h"

/* Set the exit status of the running process, to be returned to the OS upon
   exit.  */

tree
a68_posix_setexitstatus (void)
{
  return a68_get_libcall (A68_LIBCALL_SET_EXIT_STATUS);
}

/* Number of command line arguments passed to the program.  */

tree
a68_posix_argc (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_ARGC);
}

/* Gets the Nth command line argument passed to the program.  If N is out of
   range the result is an empty string.  */

tree
a68_posix_argv (void)
{
  static tree argv_fndecl;

  if (argv_fndecl == NULL_TREE)
    {
      argv_fndecl
	= a68_low_toplevel_func_decl ("argv",
				      build_function_type_list (CTYPE (M_STRING),
								a68_int_type,
								NULL_TREE));
      announce_function (argv_fndecl);

      tree param = a68_low_func_param (argv_fndecl, "n", a68_int_type);
      DECL_ARGUMENTS (argv_fndecl) = param;

      a68_push_function_range (argv_fndecl, CTYPE (M_STRING),
			       true /* top_level */);

      a68_push_range (M_STRING);
      tree len = a68_lower_tmpvar ("len%", sizetype, size_int (0));
      TREE_ADDRESSABLE (len) = 1;

      tree ptrtochar_type = build_pointer_type (a68_char_type);
      tree elems = a68_lower_tmpvar ("elems%", ptrtochar_type,
				     a68_build_libcall (A68_LIBCALL_POSIX_ARGV,
							ptrtochar_type, 2,
							param,
							fold_build1 (ADDR_EXPR, build_pointer_type (sizetype),
								     len)));
      tree lower_bound = ssize_int (1);
      tree upper_bound = fold_convert (ssizetype, len);
      tree elems_size = fold_build2 (MULT_EXPR, sizetype,
				     len,
				     size_in_bytes (a68_char_type));
      a68_add_stmt (a68_row_value (CTYPE (M_STRING), 1 /* dim */,
				   elems, elems_size,
				   &lower_bound, &upper_bound));
      tree body = a68_pop_range ();
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (argv_fndecl)),
		      argv_fndecl);
}

/* Gets the value of an environment variable, or an empty string if the
   variable is not set.  */

tree
a68_posix_getenv (void)
{
  static tree getenv_fndecl;

  if (getenv_fndecl == NULL_TREE)
    {
      getenv_fndecl
	= a68_low_toplevel_func_decl ("getenv",
				      build_function_type_list (CTYPE (M_STRING),
								CTYPE (M_STRING),
								NULL_TREE));
      announce_function (getenv_fndecl);

      tree param = a68_low_func_param (getenv_fndecl, "varname", CTYPE (M_STRING));
      DECL_ARGUMENTS (getenv_fndecl) = param;

      a68_push_function_range (getenv_fndecl, CTYPE (M_STRING),
			       true /* top_level */);

      a68_push_range (M_STRING);

      tree varname = a68_lower_tmpvar ("varname%", CTYPE (M_STRING),
				       param);

      tree ptrtochar_type = build_pointer_type (a68_char_type);
      tree convelems = a68_lower_tmpvar ("convelems%", ptrtochar_type,
					 build_int_cst (ptrtochar_type, 0));
      TREE_ADDRESSABLE (convelems) = 1;
      tree convelemslen = a68_lower_tmpvar ("convelemslen%", sizetype,
					    size_int (0));
      TREE_ADDRESSABLE (convelemslen) = 1;

      tree call = a68_build_libcall (A68_LIBCALL_POSIX_GETENV,
				     void_type_node, 5,
				     a68_multiple_elements (varname),
				     a68_multiple_num_elems (varname),
				     a68_multiple_stride (varname, size_zero_node),
				     fold_build1 (ADDR_EXPR, build_pointer_type (ptrtochar_type),
						  convelems),
				     fold_build1 (ADDR_EXPR, build_pointer_type (sizetype),
						  convelemslen));
      a68_add_stmt (call);
      tree lower_bound = ssize_int (1);
      tree upper_bound = fold_convert (ssizetype, convelemslen);
      tree convelems_size = fold_build2 (MULT_EXPR, sizetype,
					 convelemslen,
					 size_in_bytes (a68_char_type));
      a68_add_stmt (a68_row_value (CTYPE (M_STRING), 1 /* dim */,
				   convelems, convelems_size,
				   &lower_bound, &upper_bound));
      tree body = a68_pop_range ();
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (getenv_fndecl)),
		      getenv_fndecl);
}

tree
a68_posix_putchar (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_PUTCHAR);
}

tree
a68_posix_puts (void)
{
  static tree puts_fndecl;

  if (puts_fndecl == NULL_TREE)
    {
      puts_fndecl
	= a68_low_toplevel_func_decl ("puts",
				      build_function_type_list (void_type_node,
								CTYPE (M_STRING),
								NULL_TREE));
      announce_function (puts_fndecl);

      tree param = a68_low_func_param (puts_fndecl, "str", CTYPE (M_STRING));
      DECL_ARGUMENTS (puts_fndecl) = param;

      a68_push_function_range (puts_fndecl, void_type_node,
			       true /* top_level */);

      tree call = a68_build_libcall (A68_LIBCALL_POSIX_PUTS,
				     void_type_node, 3,
				     a68_multiple_elements (param),
				     a68_multiple_num_elems (param),
				     a68_multiple_stride (param, size_zero_node));
      a68_pop_function_range (call);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (puts_fndecl)),
		      puts_fndecl);
}

tree
a68_posix_fconnect (void)
{
  static tree fconnect_fndecl;

  if (fconnect_fndecl == NULL_TREE)
    {
      fconnect_fndecl
	= a68_low_toplevel_func_decl ("fconnect",
				      build_function_type_list (a68_int_type,
								CTYPE (M_STRING),
								a68_bits_type,
								NULL_TREE));
      announce_function (fconnect_fndecl);

      tree host = a68_low_func_param (fconnect_fndecl, "host", CTYPE (M_STRING));
      tree port = a68_low_func_param (fconnect_fndecl, "port", a68_int_type);
      DECL_ARGUMENTS (fconnect_fndecl) = chainon (host, port);

      a68_push_function_range (fconnect_fndecl, a68_int_type,
			       true /* top_level */);


      tree body = a68_build_libcall (A68_LIBCALL_POSIX_FCONNECT,
				     a68_int_type, 4,
				     a68_multiple_elements (host),
				     a68_multiple_num_elems (host),
				     a68_multiple_stride (host, size_zero_node),
				     port);
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fconnect_fndecl)),
		      fconnect_fndecl);
}

tree
a68_posix_fcreate (void)
{
  static tree fcreate_fndecl;

  if (fcreate_fndecl == NULL_TREE)
    {
      fcreate_fndecl
	= a68_low_toplevel_func_decl ("fcreate",
				      build_function_type_list (a68_int_type,
								CTYPE (M_STRING),
								a68_bits_type,
								NULL_TREE));
      announce_function (fcreate_fndecl);

      tree pathname = a68_low_func_param (fcreate_fndecl, "pathname", CTYPE (M_STRING));
      tree mode = a68_low_func_param (fcreate_fndecl, "mode", a68_int_type);
      DECL_ARGUMENTS (fcreate_fndecl) = chainon (pathname, mode);

      a68_push_function_range (fcreate_fndecl, a68_int_type,
			       true /* top_level */);


      tree body = a68_build_libcall (A68_LIBCALL_POSIX_FCREATE,
				     a68_int_type, 4,
				     a68_multiple_elements (pathname),
				     a68_multiple_num_elems (pathname),
				     a68_multiple_stride (pathname, size_zero_node),
				     mode);
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fcreate_fndecl)),
		      fcreate_fndecl);
}

tree
a68_posix_fopen (void)
{
  static tree fopen_fndecl;

  if (fopen_fndecl == NULL_TREE)
    {
      fopen_fndecl
	= a68_low_toplevel_func_decl ("fopen",
				      build_function_type_list (a68_int_type,
								CTYPE (M_STRING),
								a68_bits_type,
								NULL_TREE));
      announce_function (fopen_fndecl);

      tree pathname = a68_low_func_param (fopen_fndecl, "pathname", CTYPE (M_STRING));
      tree flags = a68_low_func_param (fopen_fndecl, "flags", a68_int_type);
      DECL_ARGUMENTS (fopen_fndecl) = chainon (pathname, flags);

      a68_push_function_range (fopen_fndecl, a68_int_type,
			       true /* top_level */);


      tree body = a68_build_libcall (A68_LIBCALL_POSIX_FOPEN,
				     a68_int_type, 4,
				     a68_multiple_elements (pathname),
				     a68_multiple_num_elems (pathname),
				     a68_multiple_stride (pathname, size_zero_node),
				     flags);
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fopen_fndecl)),
		      fopen_fndecl);
}

tree
a68_posix_fclose (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_FCLOSE);
}

tree
a68_posix_fsize (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_FSIZE);
}

tree
a68_posix_lseek (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_LSEEK);
}

tree
a68_posix_errno (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_ERRNO);
}

tree
a68_posix_perror (void)
{
  static tree perror_fndecl;

  if (perror_fndecl == NULL_TREE)
    {
      perror_fndecl
	= a68_low_toplevel_func_decl ("perror",
				      build_function_type_list (void_type_node,
								CTYPE (M_STRING),
								NULL_TREE));
      announce_function (perror_fndecl);

      tree str = a68_low_func_param (perror_fndecl, "str", CTYPE (M_STRING));
      DECL_ARGUMENTS (perror_fndecl) = str;

      a68_push_function_range (perror_fndecl, void_type_node,
			       true /* top_level */);

      tree body = a68_build_libcall (A68_LIBCALL_POSIX_PERROR,
				     a68_int_type, 3,
				     a68_multiple_elements (str),
				     a68_multiple_num_elems (str),
				     a68_multiple_stride (str, size_zero_node));
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (perror_fndecl)),
		      perror_fndecl);
}

tree
a68_posix_strerror (void)
{
  static tree strerror_fndecl;

  if (strerror_fndecl == NULL_TREE)
    {
      strerror_fndecl
	= a68_low_toplevel_func_decl ("strerror",
				      build_function_type_list (CTYPE (M_STRING),
								a68_int_type,
								NULL_TREE));
      announce_function (strerror_fndecl);

      tree errnum = a68_low_func_param (strerror_fndecl, "errnum", a68_int_type);
      DECL_ARGUMENTS (strerror_fndecl) = errnum;

      a68_push_function_range (strerror_fndecl, CTYPE (M_STRING),
			       true /* top_level */);

      tree len = a68_lower_tmpvar ("len%", sizetype, size_int (0));
      TREE_ADDRESSABLE (len) = 1;

      tree call = a68_build_libcall (A68_LIBCALL_POSIX_STRERROR,
				     void_type_node, 2,
				     errnum,
				     fold_build1 (ADDR_EXPR, build_pointer_type (sizetype), len));
      tree elems = a68_lower_tmpvar ("elems%", build_pointer_type (a68_char_type), call);

      tree lower_bound = ssize_int (1);
      tree upper_bound = fold_convert (ssizetype, len);
      tree elems_size = fold_build2 (MULT_EXPR, sizetype,
				     len, size_in_bytes (a68_char_type));

      tree body = a68_row_value (CTYPE (M_STRING), 1 /* dim */,
				 elems, elems_size,
				 &lower_bound, &upper_bound);
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (strerror_fndecl)),
		      strerror_fndecl);
}

tree
a68_posix_getchar (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_GETCHAR);
}

tree
a68_posix_fgetc (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_FGETC);
}

tree
a68_posix_fputc (void)
{
  return a68_get_libcall (A68_LIBCALL_POSIX_FPUTC);
}

tree
a68_posix_fputs (void)
{
  static tree fputs_fndecl;

  if (fputs_fndecl == NULL_TREE)
    {
      fputs_fndecl
	= a68_low_toplevel_func_decl ("fputs",
				      build_function_type_list (a68_int_type,
								a68_int_type,
								CTYPE (M_STRING),
								NULL_TREE));
      announce_function (fputs_fndecl);

      tree fd = a68_low_func_param (fputs_fndecl, "fd", a68_int_type);
      tree str = a68_low_func_param (fputs_fndecl, "str", CTYPE (M_STRING));
      DECL_ARGUMENTS (fputs_fndecl) = chainon (fd, str);

      a68_push_function_range (fputs_fndecl, a68_int_type,
			       true /* top_level */);


      tree body = a68_build_libcall (A68_LIBCALL_POSIX_FPUTS,
				     a68_int_type, 4,
				     fd,
				     a68_multiple_elements (str),
				     a68_multiple_num_elems (str),
				     a68_multiple_stride (str, size_zero_node));
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fputs_fndecl)),
		      fputs_fndecl);
}

tree
a68_posix_fgets (void)
{
  static tree fgets_fndecl;

  if (fgets_fndecl == NULL_TREE)
    {
      fgets_fndecl
	= a68_low_toplevel_func_decl ("fgets",
				      build_function_type_list (CTYPE (M_REF_STRING),
								a68_int_type,
								a68_int_type,
								NULL_TREE));
      announce_function (fgets_fndecl);

      tree fd = a68_low_func_param (fgets_fndecl, "fd", a68_int_type);
      tree n = a68_low_func_param (fgets_fndecl, "n", a68_int_type);
      DECL_ARGUMENTS (fgets_fndecl) = chainon (fd, n);

      a68_push_function_range (fgets_fndecl, CTYPE (M_REF_STRING),
			       true /* top_level */);

      tree len = a68_lower_tmpvar ("len%", sizetype, size_int (0));
      TREE_ADDRESSABLE (len) = 1;

      tree call = a68_build_libcall (A68_LIBCALL_POSIX_FGETS,
				     CTYPE (M_REF_STRING), 3,
				     fd, n,
				     fold_build1 (ADDR_EXPR, build_pointer_type (sizetype), len));
      tree elems = a68_lower_tmpvar ("elems%", build_pointer_type (a68_char_type), call);

      tree lower_bound = ssize_int (1);
      tree upper_bound = fold_convert (ssizetype, len);
      tree elems_size = fold_build2 (MULT_EXPR, sizetype,
				     len, size_in_bytes (a68_char_type));
      tree body = a68_row_malloc (CTYPE (M_STRING), 1 /* dim */,
				  elems, elems_size,
				  &lower_bound, &upper_bound);
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fgets_fndecl)),
		      fgets_fndecl);
}

tree
a68_posix_gets (void)
{
  static tree gets_fndecl;

  if (gets_fndecl == NULL_TREE)
    {
      gets_fndecl
	= a68_low_toplevel_func_decl ("gets",
				      build_function_type_list (CTYPE (M_REF_STRING),
								a68_int_type,
								NULL_TREE));
      announce_function (gets_fndecl);

      tree n = a68_low_func_param (gets_fndecl, "n", a68_int_type);
      DECL_ARGUMENTS (gets_fndecl) = n;

      a68_push_function_range (gets_fndecl, CTYPE (M_REF_STRING),
			       true /* top_level */);

      tree len = a68_lower_tmpvar ("len%", sizetype, size_int (0));
      TREE_ADDRESSABLE (len) = 1;

      tree call = a68_build_libcall (A68_LIBCALL_POSIX_GETS,
				     CTYPE (M_REF_STRING), 2,
				     n, fold_build1 (ADDR_EXPR, build_pointer_type (sizetype), len));
      tree elems = a68_lower_tmpvar ("elems%", build_pointer_type (a68_char_type), call);

      tree lower_bound = ssize_int (1);
      tree upper_bound = fold_convert (ssizetype, len);
      tree elems_size = fold_build2 (MULT_EXPR, sizetype,
				     len, size_in_bytes (a68_char_type));
      tree body = a68_row_malloc (CTYPE (M_STRING), 1 /* dim */,
				  elems, elems_size,
				  &lower_bound, &upper_bound);
      a68_pop_function_range (body);
    }

  return fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (gets_fndecl)),
		      gets_fndecl);
}