Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

target-dependent function return values #17

Open
rofl0r opened this issue Dec 2, 2019 · 2 comments
Open

target-dependent function return values #17

rofl0r opened this issue Dec 2, 2019 · 2 comments

Comments

@rofl0r
Copy link

rofl0r commented Dec 2, 2019

hello,

i'm reading MIR.md and found this paragraph:

* MIR functions can have more one result but possible number of results
   and combination of their types are machine-defined.  For example, for x86-64
   the function can have upto six results and return two integer
   values, two float or double values, and two long double values
   in any combination

this decision seems to make mir programs dependent on ARCH it runs on.
i'm wondering whether it would be preferable to modify API such that any number of args can be returned.
the target-specific codegen could then decide how these are returned (optimally the same way C does it when returning a struct).
so a scripting language can freely use MIR without being restricted in this aspect.

of course i may be missing some important aspects you already considered.

interestingly, c2mir compiles the following snippet

struct foo { long long l1, l2, l3, l4; };
struct foo bar(int a1, int a2, int a3) {
        return (struct foo) {.l1 = a1, .l2 = a2, .l3 = a3, .l4 = a3+a2};
}

but using m2c[0] on mir code generated by -S causes assert failure.

[0]:

diff --git a/mir2c/mir2c.c b/mir2c/mir2c.c
index bf9d92f..20454cd 100644
--- a/mir2c/mir2c.c
+++ b/mir2c/mir2c.c
@@ -462,7 +462,7 @@ void MIR_module2c (MIR_context_t ctx, FILE *f, MIR_module_t m) {
 }
 
 /* ------------------------- Small test example ------------------------- */
-#ifdef TEST_MIR2C
+#if defined(TEST_MIR2C)
 
 #include "mir-tests/scan-sieve.h"
 #include "mir-tests/scan-hi.h"
@@ -478,4 +478,27 @@ int main (int argc, const char *argv[]) {
   MIR_finish (ctx);
   return 0;
 }
+
+#elif defined(M2C)
+
+int main (int argc, const char *argv[]) {
+  MIR_module_t m;
+  MIR_context_t ctx = MIR_init ();
+
+  FILE *f = fopen(argv[1], "r");
+  fseeko(f, 0, SEEK_END);
+  off_t l = ftello(f);
+  fseeko(f, 0, SEEK_SET);
+  char *buf = malloc(l+1);
+  assert(fread(buf, 1, l, f) == l);
+  fclose(f);
+  buf[l] = 0;
+  MIR_scan_string (ctx, buf);
+  m = DLIST_TAIL (MIR_module_t, *MIR_get_module_list (ctx));
+  MIR_module2c (ctx, stdout, m);
+  MIR_finish (ctx);
+  return 0;
+}
+
+
 #endif
@vnmakarov
Copy link
Owner

hello,

i'm reading MIR.md and found this paragraph:

* MIR functions can have more one result but possible number of results
   and combination of their types are machine-defined.  For example, for x86-64
   the function can have upto six results and return two integer
   values, two float or double values, and two long double values
   in any combination

this decision seems to make mir programs dependent on ARCH it runs on.
i'm wondering whether it would be preferable to modify API such that any number of args can be returned.
the target-specific codegen could then decide how these are returned (optimally the same way C does it when returning a struct).
so a scripting language can freely use MIR without being restricted in this aspect.

of course i may be missing some important aspects you already considered.

No, you nailed it. I thought about this too because I stated several times that MIR is machine-independent languages. So I am going to implement returning any combinations of values. My plan is to pass other values through the stack. Although there will be still machine-dependence on which values are returned through regs or memory. But if only MIR is used, it should work on any target.

Another thing I mentioned that MIR supports only binary compliment arithmetic. So it will not work on machines w/o it. Fortunately, there are very few such machines. And as I know coming C2x standard will require this too.

interestingly, c2mir compiles the following snippet

struct foo { long long l1, l2, l3, l4; };
struct foo bar(int a1, int a2, int a3) {
        return (struct foo) {.l1 = a1, .l2 = a2, .l3 = a3, .l4 = a3+a2};
}

but using m2c[0] on mir code generated by -S causes assert failure.

Thank you I'll investigate this on this week.

[0]:

diff --git a/mir2c/mir2c.c b/mir2c/mir2c.c
index bf9d92f..20454cd 100644
--- a/mir2c/mir2c.c
+++ b/mir2c/mir2c.c
@@ -462,7 +462,7 @@ void MIR_module2c (MIR_context_t ctx, FILE *f, MIR_module_t m) {
 }
 
 /* ------------------------- Small test example ------------------------- */
-#ifdef TEST_MIR2C
+#if defined(TEST_MIR2C)
 
 #include "mir-tests/scan-sieve.h"
 #include "mir-tests/scan-hi.h"
@@ -478,4 +478,27 @@ int main (int argc, const char *argv[]) {
   MIR_finish (ctx);
   return 0;
 }
+
+#elif defined(M2C)
+
+int main (int argc, const char *argv[]) {
+  MIR_module_t m;
+  MIR_context_t ctx = MIR_init ();
+
+  FILE *f = fopen(argv[1], "r");
+  fseeko(f, 0, SEEK_END);
+  off_t l = ftello(f);
+  fseeko(f, 0, SEEK_SET);
+  char *buf = malloc(l+1);
+  assert(fread(buf, 1, l, f) == l);
+  fclose(f);
+  buf[l] = 0;
+  MIR_scan_string (ctx, buf);
+  m = DLIST_TAIL (MIR_module_t, *MIR_get_module_list (ctx));
+  MIR_module2c (ctx, stdout, m);
+  MIR_finish (ctx);
+  return 0;
+}
+
+
 #endif

@vnmakarov
Copy link
Owner

Thank you I'll investigate this on this week.

MIR2C was not updated when I reworked return-stmt. An early version of MIR_RET always required one return value. I fixed that. But MIR2C will report an error if there is more one return value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants